简体   繁体   English

ListView.builder在flutter中逐项滚动

[英]ListView.builder scroll item by item in flutter

I have a horizontal ListView and I want to force the user to scroll one item at a time, how can I achieve that? 我有一个水平ListView ,我想强制用户一次滚动一个项目,我怎么能实现这一点?

return Container(
    height: 120.0,
    padding: EdgeInsetsDirectional.only(start: 8.0),
    child: ListView.builder(
           itemBuilder: _buildListItem(),
           scrollDirection: Axis.horizontal,
           itemCount: arrayItems.length,
           ),
);

Use 使用

physics: PageScrollPhysics(), // in ListView

I couldn't get your code. 我无法得到你的代码。 Try this one and make changes accordingly. 尝试这个并相应地进行更改。

List<String> yourArray = ["A", "B", "C", "D"];

@override
Widget build(BuildContext context) {
  double width = MediaQuery.of(context).size.width; 
  return Container(
    height: 100,
    child: ListView.builder(
      physics: PageScrollPhysics(), // this is what you are looking for
      scrollDirection: Axis.horizontal,
      itemCount: yourArray.length,
      itemBuilder: (context, index) {
        return Container(
          color: Colors.grey,
          width: width,
          child: Center(child: Text("Index = ${index}")),
        );
      },
    ),
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM