gpt4 book ai didi

android - 如何实现滑动删除 ListView 以从 firestore 中删除数据

转载 作者:IT王子 更新时间:2023-10-29 07:11:46 24 4
gpt4 key购买 nike

我对 flutter 和 dart 还很陌生,所以这可能是一个基本问题。但是,我想知道的是如何在 ListView 中实现滑动删除方法以从 firestore 中删除数据。

我尝试使用 Dissmissible 函数,但我不明白如何显示列表,而且我似乎也不明白如何删除所选数据。

这是我的 Dart 代码

Widget build(BuildContext context) {
return new Scaffold(
resizeToAvoidBottomPadding: false,
appBar: new AppBar(
centerTitle: true,
automaticallyImplyLeading: false,
title: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,children:
<Widget>[
Text("INVENTORY",textAlign: TextAlign.center,) ,new IconButton(
icon: Icon(
Icons.home,
color: Colors.black,
),
onPressed: () {
Navigator.push(
context,
SlideLeftRoute(widget: MyHomePage()),
);
})]),
),body: ListPage(),
);
}
}

class ListPage extends StatefulWidget {
@override
_ListPageState createState() => _ListPageState();
}

class _ListPageState extends State<ListPage> {

Future getPosts() async{
var firestore = Firestore.instance;

QuerySnapshot gn = await
firestore.collection("Inventory").orderBy("Name",descending:
false).getDocuments();

return gn.documents;

}

@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder(
future: getPosts(),
builder: (_, snapshot){
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: Text("Loading"),
);
}else{

return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder:(_, index){
return EachList(snapshot.data[index].data["Name"].toString(),
snapshot.data[index].data["Quantity"]);
});
}
}),
);
}
}


class EachList extends StatelessWidget{
final String details;
final String name;
EachList(this.name, this.details);
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Card(
child:new Container(
padding: EdgeInsets.all(8.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Row(
children: <Widget>[
new CircleAvatar(child: new Text(name[0].toUpperCase()),),
new Padding(padding: EdgeInsets.all(10.0)),
new Text(name, style: TextStyle(fontSize: 20.0),),
],
),
new Text(details, style: TextStyle(fontSize: 20.0))
],
),
),
);
}

}

最佳答案

您应该使用 Dismissible 小部件。我将它用于从 Firestore 检索的收件箱列表。在你的 EachList 中返回这样的东西

return Dismissible(
direction: DismissDirection.startToEnd,
resizeDuration: Duration(milliseconds: 200),
key: ObjectKey(snapshot.documents.elementAt(index)),
onDismissed: (direction) {
// TODO: implement your delete function and check direction if needed
_deleteMessage(index);
},
background: Container(
padding: EdgeInsets.only(left: 28.0),
alignment: AlignmentDirectional.centerStart,
color: Colors.red,
child: Icon(Icons.delete_forever, color: Colors.white,),
),
// secondaryBackground: ...,
child: ...,
);

});

重要提示:为了删除列表项,您还需要从快照列表中删除该项目,而不仅仅是从 firestore 中:

_deleteMessage(index){
// TODO: here remove from Firestore, then update your local snapshot list
setState(() {
snapshot.documents.removeAt(index);
});
}

这里是文档:Implement Swipe to Dismiss

这是 Flutter 团队的视频:Widget of the week - Dismissilbe

关于android - 如何实现滑动删除 ListView 以从 firestore 中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55918425/

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