I'm creating a page which has a list of items. I want to scroll to the bottom of the page after the list has rendered. The trouble is that I am using a FutureBuilder to fetch the list. If I use WidgetsBinding.addPostFrameCallback
this seems to get called before the build()
method completes (maybe because it is still in the process of fetching data).
Is there a way to figure out that a Future has completed, the list has rendered and only then trigger the scroll to bottom?
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) => _postInit());
super.initState();
}
Future<void> _postInit() async {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: _getList(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch(snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return Center(child: SizedBox(height: 100, width: 100, child: new CircularProgressIndicator()));
break;
default:
if(snapshot.hasError) {
return Container();
} else {
return _buildListView(snapshot.data);
}
}
}
)
);
}
Widget _buildListView(list) {
return Column(
children: <Widget>[
Expanded(
child: ListView(
controller: _scrollController,
shrinkWrap: true,
children: list,
),
),
],
);
}