gpt4 book ai didi

dart - 如何在 Flutter 中清除 AnimatedList 中的所有项目

转载 作者:IT老高 更新时间:2023-10-28 12:40:19 25 4
gpt4 key购买 nike

我试图在 AnimatedList 上预先生成所有更新操作,就像我为 Android 中的 RecyclerView 适配器所做的那样。但是当我要清除所有数据时,我不知道该怎么做。

如果我这样做

setState(() {
_data.clear();
});

那么backing data被清除了但是GlobalKey的当前状态仍然不知道变化,并且上面没有clear()方法。

补充代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: Text('Update AnimatedList data')),
body: BodyWidget(),
),
);
}
}

class BodyWidget extends StatefulWidget {
@override
BodyWidgetState createState() {
return new BodyWidgetState();
}
}

class BodyWidgetState extends State<BodyWidget> {

// the GlobalKey is needed to animate the list
final GlobalKey<AnimatedListState> _listKey = GlobalKey();

// backing data
List<String> _data = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
SizedBox(
height: 400,
child: AnimatedList(
key: _listKey,
initialItemCount: _data.length,
itemBuilder: (context, index, animation) {
return _buildItem(_data[index], animation);
},
),
),
RaisedButton(
child: Text(
'Clear all items',
style: TextStyle(fontSize: 20),
),
onPressed: () {
_clearAllItems();
},
)
],
);
}

Widget _buildItem(String item, Animation animation) {
return SizeTransition(
sizeFactor: animation,
child: Card(
child: ListTile(
title: Text(
item,
style: TextStyle(fontSize: 20),
),
),
),
);
}

void _clearAllItems() {
_data.clear();
// no such method
_listKey.currentState.clear();
}

}

最佳答案

您必须手动删除 _listKey.currentState 中的项目

示例代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: Text('Update AnimatedList data')),
body: BodyWidget(),
),
);
}
}

class BodyWidget extends StatefulWidget {
@override
BodyWidgetState createState() {
return new BodyWidgetState();
}
}

class BodyWidgetState extends State<BodyWidget> {
// the GlobalKey is needed to animate the list
final GlobalKey<AnimatedListState> _listKey = GlobalKey();

// backing data
List<String> _data = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
SizedBox(
height: 400,
child: AnimatedList(
key: _listKey,
initialItemCount: _data.length,
itemBuilder: (context, index, animation) {
return _buildItem(_data[index], animation);
},
),
),
RaisedButton(
child: Text(
'Clear all items',
style: TextStyle(fontSize: 20),
),
onPressed: () {
_clearAllItems();
},
)
],
);
}

Widget _buildItem(String item, Animation animation) {
return SizeTransition(
sizeFactor: animation,
child: Card(
child: ListTile(
title: Text(
item,
style: TextStyle(fontSize: 20),
),
),
),
);
}

void _clearAllItems() {
for (var i = 0; i <= _data.length - 1; i++) {
_listKey.currentState.removeItem(0,
(BuildContext context, Animation<double> animation) {
return Container();
});
}
_data.clear();
}
}

关于dart - 如何在 Flutter 中清除 AnimatedList 中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54376474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com