我想从链接到Firebase的列表中识别选定的项目,然后单击“保存”,将它们保存到Firebase的另一个列表中。每次用户选择一个项目时,我都试图保存,但是效果不佳。
Image containing the listed items
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; class CheckBoxList extends StatefulWidget { @override _CheckBoxListState createState() => _CheckBoxListState(); }
class _CheckBoxListState extends State<CheckBoxList> {
List<bool> _itens = List();
_loadingItens(){
_itens = [];
_itens.add(false);
_itens.add(false);
_itens.add(false);
_itens.add(false);
_itens.add(false);
}
Future getList() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("adicionais").getDocuments();
return qn.documents;
}
@override
void initState() {
_loadingItens();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("CheckBoxList"),
),
body: Container(
child: Column(
children: <Widget>[
Expanded(
child: FutureBuilder(
future: getList(),
// ignore: missing_return
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return ListView.builder(
padding: EdgeInsets.all(0.0),
itemCount: 4,
itemBuilder: (_, index) {
return ListBody(
children: <Widget>[Text("Loading...")],
);
});
} else {
return ListView.builder(
padding: EdgeInsets.all(0.0),
itemCount: snapshot.data.length,
itemBuilder: (_, index) {
String nome = snapshot.data[index].data["nome"];
String valor = snapshot.data[index].data["valor"];
return CheckboxListTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(nome),
Text("\$ $valor")
],
),
value: _itens[index],
onChanged: (value) {
setState(() {
_itens[index] = value;
});
});
});
}
}))
],
),
),
bottomNavigationBar: Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border(
top: BorderSide(
color: Colors.grey.shade200,
width: 1.0,
),
)),
child: Padding(
padding: EdgeInsets.only(left:16, right:16, top: 16, bottom: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(),
child: GestureDetector(
child: Container(
width: (MediaQuery.of(context).size.width * 0.90),
height: 60,
decoration: BoxDecoration(
color: Color(0xff0269BA),
borderRadius: BorderRadius.all(Radius.circular(10))),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 8, right: 8),
child: Text("Save",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.w500,
))),
],
),
),
onTap: () {
},
),
)
],
)),
),
);
}
}