我创建了一个可滑动的小部件列表视图,其中显示了一些配料及其数量。我面临的问题是我无法找出代码来编辑和删除列表视图中的详细信息,并在列表和firebase本身上对其进行更新。
我尝试使用set state函数并实现了remove at方法,但是我似乎无法从列表或Firebase中删除数据。
import 'package:flutter/material.dart';
import 'package:foodpal/homepage.dart';
import 'package:foodpal/main.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
class Inventory extends StatelessWidget {
int getColorHexFromStr(String colorStr) {
colorStr = "FF" + colorStr;
colorStr = colorStr.replaceAll("#", "");
int val = 0;
int len = colorStr.length;
for (int i = 0; i < len; i++) {
int hexDigit = colorStr.codeUnitAt(i);
if (hexDigit >= 48 && hexDigit <= 57) {
val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
} else if (hexDigit >= 65 && hexDigit <= 70) {
val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
} else if (hexDigit >= 97 && hexDigit <= 102) {
val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
} else {
throw new FormatException("Error occured when converting colour");
}
}
return val;
}
@override
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 EachList2(snapshot.data[index].data["Name"],
snapshot.data[index].data["Quantity"], snapshot, index);
});
}
}),
);
}
}
class EachList2 extends StatefulWidget {
final String details;
final String name;
final AsyncSnapshot snapshot;
final int index;
EachList2(this.name, this.details,this.snapshot,this.index);
@override
_EachList2State createState() => _EachList2State(this.name,
this.details,this.snapshot,this.index);
}
class _EachList2State extends State<EachList2> {
final String details;
final String name;
final AsyncSnapshot snapshot;
final int index;
_EachList2State(this.name, this.details,this.snapshot,this.index);
@override
Widget build(BuildContext context) {
// TODO: implement build
return Slidable(
delegate: new SlidableDrawerDelegate(),
actionExtentRatio: 0.25,
actions: <Widget>[
new IconSlideAction(
caption: 'Edit',
color: Colors.blueAccent,
icon: Icons.edit_attributes,
onTap: () => {
print("Edit")
}
),
new IconSlideAction(
caption: 'Delete',
color: Colors.red,
icon: Icons.delete,
onTap: () => {
setState((){
snapshot.data.removeAt(index);
}
),
}
),
],
/* direction: DismissDirection.startToEnd,
resizeDuration: Duration(milliseconds: 300),
key: ObjectKey(snapshot.data.elementAt(index)),
onDismissed: (direction){
},
background: Container(
padding: EdgeInsets.only(left: 28.0),
alignment: AlignmentDirectional.centerStart,
color: Colors.red,
child: Icon(Icons.delete_forever, color: Colors.white,),
),*/
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))
],
),
),
);
}
}
class SlideLeftRoute extends PageRouteBuilder {
final Widget widget;
SlideLeftRoute({this.widget})
: super(
pageBuilder: (BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
return widget;
},
transitionsBuilder: (BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
return new SlideTransition(
position: new Tween<Offset>(
begin: const Offset(1.0, 0.0),
end: Offset.zero,
).animate(animation),
child: child,
);
}
);
}