9

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
  • I need the items to appear as a list, not as a single page(PageView), I need to modify the scroll of the list only Commented Jan 15, 2019 at 15:50

1 Answer 1

22

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}")),
        );
      },
    ),
  );
}
Sign up to request clarification or add additional context in comments.

1 Comment

great answer: physics: PageScrollPhysics(), // in ListView thanks @CopsOnRoad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.