ListView.builder scroll item by item in flutter

Viewed 4810

I have a horizontal ListView and I want to force the user to scroll one item at a time, how can I achieve that?

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

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}")),
        );
      },
    ),
  );
}
Related