Want to get full size of a ScrollController in Flutter? You can use _scrollController.position.maxScrollExtent to scroll to the end. Make sure to do this in a post-frame callback so that it will include the new item you just added.
Here is an example of a sliver list that scrolls to the end as more items are added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import 'package:flutter/scheduler.dart'; import 'package:flutter/material.dart'; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Demo', home: new MyHomePage(), ); } } class MyHomePage extends StatefulWidget { State createState() => new MyHomePageState(); } class MyHomePageState extends State<MyHomePage> { ScrollController _scrollController = new ScrollController(); List<Widget> _items = new List.generate(40, (index) { return new Text("item $index"); }); @override Widget build(BuildContext context) { return new Scaffold( floatingActionButton: new FloatingActionButton( child: new Icon(Icons.arrow_downward), onPressed: () { setState(() { _items.add(new Text("item ${_items.length}")); }); SchedulerBinding.instance.addPostFrameCallback((_) { _scrollController.animateTo( _scrollController.position.maxScrollExtent, duration: const Duration(milliseconds: 300), curve: Curves.easeOut, ); }); }, ), body: new CustomScrollView( controller: _scrollController, slivers: [ new SliverAppBar( title: new Text('Sliver App Bar'), ), new SliverList( delegate: new SliverChildBuilderDelegate( (context, index) => _items[index], childCount: _items.length, ), ), ], ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.