5

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),
      ),
    );
  }
}
FA_developes
  • 162
  • 9

2 Answers2

4

After some more searching, I found the answer:

void _addItem() {
   setState(() {
     _items.add(++_counter);
   });
   SchedulerBinding.instance.addPostFrameCallback((_) {
     _listViewController.jumpTo(_listViewController.position.maxScrollExtent);
   });
}

Source: https://stackoverflow.com/a/44142234/8722652

FA_developes
  • 162
  • 9
1

Use WidgetsBinding.instance.addPersistentFrameCallback :

https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPersistentFrameCallback.html

  bool added = false;

  void initState() {
    WidgetsBinding.instance.addPersistentFrameCallback(jumpToDown);

    super.initState();
  }

  void _addItem() {
    setState(() {
      _items.add(++_counter);
      added = true;
    });
  }

  void jumpToDown([nothing]) {
    if (added) {
      _listViewController.jumpTo(_listViewController.position.maxScrollExtent);
    }
    added = false;
  }
Kahou
  • 3,200
  • 1
  • 14
  • 21