In my App, I want to scroll to the bottom of a ListView after adding an Item. The problem is that it scrolls down before adding the Item and therefore leaves one item hidden. Is there a way to call the scroll function after the item has rendered?
Example code:
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _items = [];
var _counter = 0;
final _listViewController = ScrollController();
void _addItem() {
setState(() {
_items.add(++_counter);
});
_listViewController.jumpTo(_listViewController.position.maxScrollExtent);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: ListView(
padding: const EdgeInsets.all(16.0),
controller: _listViewController,
children: [
..._items.map((i) => ListTile(title: Text(i.toString()))),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _addItem,
child: Icon(Icons.add),
),
);
}
}