S.D.
S.D.

Reputation: 5867

Render list items then scroll to the bottom

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"),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

Upvotes: 1

Views: 1060

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657871

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

Upvotes: 1

Related Questions