When I add items to the end of a grid view I want the user to see what has been added. Here's an example of what I mean:
The user adds items by pressing the + icon. The problem is that after Item 14 there is no feedback to show that any item has been added. How can I automatically scroll to the last item when it is added to the list?
(Bonus points: how can I scroll to the nth item when it is added somewhere in the middle of the list)
Here is an answer for scrolling to the end of a list view, but if I reverse the order the list then it will look strange to sometimes have 'missing' items on the top row.
Here's my code:
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',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
Widget build(BuildContext context) {
List items = [];
for (int i = 0; i < _counter; i++) {
items.add(new Text("Item $i"));
}
return new Scaffold(
appBar: new AppBar(title: new Text("Scroll To End Test")),
body: new GridView.extent(
primary: true,
maxCrossAxisExtent: 150.0,
children: items,
),
floatingActionButton: new FloatingActionButton(
onPressed: () {
setState(() {
_counter++;
});
},
tooltip: 'Increment',
child: new Icon(Icons.add),
),
);
}
}