There are two types of ScrollPhysics
that I want to apply to my ListView
. When user reaches the bottom of the List, I want BouncingScrollPhysics()
to take place, but when user reaches the top, it shouldn't bounce but rather perform ClampingScrollPhysics()
. How to achieve this?
Screenshot:
// create 2 instance variables
var _controller = ScrollController();
ScrollPhysics _physics = ClampingScrollPhysics();
@override
void initState() {
super.initState();
_controller.addListener(() {
if (_controller.position.pixels <= 56)
setState(() => _physics = ClampingScrollPhysics());
else
setState(() => _physics = BouncingScrollPhysics());
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
controller: _controller,
physics: _physics,
itemCount: 20,
itemBuilder: (_, i) => ListTile(title: Text("Item $i")),
),
);
}
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