0
當我將項目添加到網格視圖的末尾時,我希望用戶看到添加的內容。下面是我的意思的例子:如何自動滾動到網格視圖結束?
用戶通過按下+圖標添加項目。問題是,在項目14之後,沒有反饋顯示任何項目已被添加。如何將它添加到列表中時自動滾動到最後一個項目?
(加分點:我如何可以滾動到當它在列表中的中間位置添加的第n項)
Here is an answer for scrolling to the end of a list view,但如果我顛倒順序列表,然後它會看起來很奇怪有時有頂部一行'缺少'項目。
這裏是我的代碼:
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),
),
);
}
}
謝謝!我不知道_scrollController.position.maxScrollExtent – Mark
YW - 檢查滾動到點的進一步提示 –