【发布时间】:2018-04-02 13:11:39
【问题描述】:
当我将项目添加到网格视图的末尾时,我希望用户看到已添加的内容。这是我的意思的一个例子:
用户通过按 + 图标添加项目。问题是在第 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),
),
);
}
}
【问题讨论】:
标签: flutter