For listening and offset control of lists such as ListView
A constructor
ScrollController({double initialScrollOffset = 0.0,// Initial offset this.keepScrollOffset = true,// Whether to save the scrolling position this.debugLabel, })
Commonly used method
- AnimateTo (), scroll to the specified position, you can set the target offset, time and scroll curve
- JumpTo (), scroll to the specified position and teleport
- AddListener (), to add a scroll listener, can get the scroll position.
- Dispose () is called when dispose of widgets to avoid memory leaks
class _MyHomePageState extends State<MyHomePage> {
ScrollController controller = new ScrollController();
bool show = false;
@override
void initState() {
super.initState();
controller.addListener(() {
if (controller.offset > 1000 && !show) {
setState(() {
show = true;
});
} else if (controller.offset <= 1000 && show) {
setState(() {
show = false;
});
}
});
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ConstrainedBox(
constraints: BoxConstraints.expand(),
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('$index'),
);
},
itemCount: 100,
itemExtent: 50,
controller: controller,
),
),
floatingActionButton: !show
? null
: FloatingActionButton(
onPressed: () {
// controller.animateTo(0,
// duration: Duration(seconds: 1), curve: Curves.bounceInOut);
controller.jumpTo(33);
},
child: Icon(Icons.arrow_drop_up),
),
);
}
}
Copy the code