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,
),
);
All you have to do is set Global Keys for your widgets and call Scrollable. ensureVisible on the key of your widget you want to scroll to. For this to work your ListView should be a finite List of objects. If you are using ListView.
itemExtent property Null safety SliverFixedExtentList, the sliver used internally when this property is provided. It constrains its box children to have a specific given extent along the main axis. The prototypeItem property, which allows forcing the children's extent to be the same as the given widget.
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}")),
);
},
),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With