1

Is there a way to render a list then scroll to the bottom.

I understand you can manually scroll to the bottom using ScrollController when a new item is added (like in this question: Programmatically scrolling to the end of a ListView), but how should I automatically scroll to the bottom of the list when the list is built without adding a new item.

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage();

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Column(
        children: <Widget>[
          new Expanded(
            child: new ListView.builder(
              itemCount: 200,
              itemBuilder: (context, index) {
                return new ListTile(
                  title: new Text("title $index"),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
S.D.
  • 5,197
  • 9
  • 38
  • 64

1 Answers1

1

You get this behavior if you use reverse: true

child: new ListView.builder(
  reverse: true

https://docs.flutter.io/flutter/widgets/ListView/ListView.builder.html

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Not great if you want your items to come in chronological order (as in the order the user added them). Getting a list to just start at the bottom seems quite difficult. – DarkNeuron Jul 29 '19 at 08:45
  • 1
    @DarkNeuron did you find nay solution for this issue? I want to scroll to the bottom of the list after it gets data rendered after firebase using streambuilder. – Jay Mungara Nov 04 '19 at 06:50
  • @JayMungara We had the same requirement. We ended up using a hacking timeout. Not a solution I recommend. – DarkNeuron Nov 04 '19 at 10:03
  • 1
    If you reverse the order of the items in the collection and use `reverse: true` you should get the desired behavior. – Günter Zöchbauer Nov 04 '19 at 11:08