Flutter 如何自动滚动到网格视图的末尾?
当我将项目添加到网格视图的末尾时,我希望用户看到已添加的内容。这里有一个例子来说明我的意思: 用户通过按+图标添加项目。问题是,在第14项之后,没有任何反馈表明添加了任何项目。当最后一项添加到列表中时,如何自动滚动到最后一项Flutter 如何自动滚动到网格视图的末尾?,flutter,Flutter,当我将项目添加到网格视图的末尾时,我希望用户看到已添加的内容。这里有一个例子来说明我的意思: 用户通过按+图标添加项目。问题是,在第14项之后,没有任何反馈表明添加了任何项目。当最后一项添加到列表中时,如何自动滚动到最后一项 (加分:当列表中添加某处时,如何滚动到第n项) ,但如果我颠倒列表的顺序,那么有时在第一行出现“缺少”项看起来会很奇怪 这是我的密码: import 'package:flutter/material.dart'; void main() { runApp(new
(加分:当列表中添加某处时,如何滚动到第n项)
,但如果我颠倒列表的顺序,那么有时在第一行出现“缺少”项看起来会很奇怪 这是我的密码: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),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(新的MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“颤振演示”,
主题:新主题数据(
主样本:颜色。蓝色,
),
主页:新建MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
@凌驾
小部件构建(构建上下文){
列表项=[];
对于(int i=0;i<\u计数器;i++){
添加(新文本(“项目$i”);
}
归还新脚手架(
appBar:new appBar(标题:新文本(“滚动到结束测试”),
正文:新建GridView.extent(
小学:对,
maxCrossAxisExtent:150.0,
儿童:项目,
),
floatingActionButton:新的floatingActionButton(
已按下:(){
设置状态(){
_计数器++;
});
},
工具提示:“增量”,
子:新图标(Icons.add),
),
);
}
}
声明一个滚动控制器,并使用它移动到您想要的点。下面是一个滚动到最后一个元素的示例。请注意,滚动控制器是显式声明的,因此不能发出primary:true
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;
ScrollController _scrollController; // NEW
@override // NEW
void initState() { // NEW
super.initState(); // NEW
_scrollController = new ScrollController( // NEW
initialScrollOffset: 0.0, // NEW
keepScrollOffset: true, // NEW
);
}
void _toEnd() { // NEW
_scrollController.animateTo( // NEW
_scrollController.position.maxScrollExtent, // NEW
duration: const Duration(milliseconds: 500), // NEW
curve: Curves.ease, // NEW
); // NEW
} // NEW
@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"),
actions: <Widget>[ // NEW
new IconButton( // NEW
icon: new Icon(Icons.arrow_downward), onPressed: _toEnd)// NEW
], // NEW
),
body: new GridView.extent(
//primary: true, // REMOVED
maxCrossAxisExtent: 150.0,
children: items,
controller: _scrollController, // NEW
),
floatingActionButton: new FloatingActionButton(
onPressed: () {
setState(() {
_counter++;
});
},
tooltip: 'Increment',
child: new Icon(Icons.add),
),
);
}
}
根据栅格的每行对象数调整此值。我知道这只是一个草图,但我认为它可以引导您达到目的。谢谢!我不知道\u scrollController.position.maxScrollExtent
var cursor = i/(# items) * _scrollController.position.maxScrollExtent