Flutter - 如何在提交表单时将选定的 DropDownMenu 索引保存到 FireStore
Flutter - How can I save the selected index of DropDownMenu to FireStore when the form is submitted
这是我的下拉列表代码
Container(
decoration: BoxDecoration(
color: Colors.lightBlueAccent,
//border: ,
borderRadius: BorderRadius.circular(20),
),
//color: Colors.white,
margin:
EdgeInsets.only(left: 50, right: 50),
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection("Choose Platform")
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
const Text("Loading.....");
else {
List<DropdownMenuItem>
chooseplatform = [];
for (int i = 0;
i <
snapshot.data.documents
.length;
i++) {
DocumentSnapshot snap =
snapshot.data.documents[i];
chooseplatform.add(
DropdownMenuItem(
child: Text(
snap.documentID,
style: TextStyle(
color: Colors.black),
),
value: "${snap.documentID}",
),
);
}
return Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
DropdownButton(
items: chooseplatform,
onChanged:
(choosingplatform) {
final snackBar = SnackBar(
content: Text(
'Selected Platform is $choosingplatform',
style: TextStyle(
color: Color(
0xff1ffcb7)),
),
);
Scaffold.of(context)
.showSnackBar(
snackBar);
setState(() {
chosenPlatform =
choosingplatform;
});
},
value: chosenPlatform,
isExpanded: false,
hint: new Text(
"Choose Platform",
style: TextStyle(
color: Colors.black),
),
),
],
);
}
})),
这是凸起按钮的代码
RaisedButton(
padding:
EdgeInsets.only(left: 10, right: 10),
onPressed: () async {
Firestore.instance.runTransaction(
(Transaction transaction) async {
final CollectionReference reference =
Firestore.instance
.collection('Tournaments');
await reference
.document('Fifa Tournaments')
.collection('Fifa Tourneys')
.add({
'tourneyname':
tourneynameController.text,
'tourneygame':
tourneydateController.text,
});
tourneynameController.clear();
tourneydateController.clear();
});
Navigator.of(context).pop();
},
child: Text(
'Create Tournament',
style: TextStyle(fontSize: 16),
),
color: const Color(0xff1ffcb7),
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(18.0),
side: BorderSide(color: Colors.white),
),
),
现在,我只能保存 TextFormFields 的值,不能保存下拉列表。
简而言之,我现在正在做的是从集合中检索值并将它们显示在下拉菜单中,但我想做的是检索值,然后将选定的值保存在另一个名为 'Fifa tourneys' 的集合中,当表格已提交。
谢谢!
已解决!
我所要做的就是将 selectedIndex 的变量保存到我的 firestore 集合中。
onPressed: () async {
Firestore.instance.runTransaction(
(Transaction transaction) async {
final CollectionReference reference =
Firestore.instance
.collection('Tournaments');
await reference
.document('Fifa Tournaments')
.collection('Fifa Tourneys')
.add({
'tourneyname':
tourneynameController.text,
'tourneygame':
tourneydateController.text,
'tourneyplatform': chosenPlatform,// this is the one
});
tourneynameController.clear();
tourneydateController.clear();
chosenPlatform.clear();
});
Navigator.of(context).pop();
},
这是我的下拉列表代码
Container(
decoration: BoxDecoration(
color: Colors.lightBlueAccent,
//border: ,
borderRadius: BorderRadius.circular(20),
),
//color: Colors.white,
margin:
EdgeInsets.only(left: 50, right: 50),
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection("Choose Platform")
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
const Text("Loading.....");
else {
List<DropdownMenuItem>
chooseplatform = [];
for (int i = 0;
i <
snapshot.data.documents
.length;
i++) {
DocumentSnapshot snap =
snapshot.data.documents[i];
chooseplatform.add(
DropdownMenuItem(
child: Text(
snap.documentID,
style: TextStyle(
color: Colors.black),
),
value: "${snap.documentID}",
),
);
}
return Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
DropdownButton(
items: chooseplatform,
onChanged:
(choosingplatform) {
final snackBar = SnackBar(
content: Text(
'Selected Platform is $choosingplatform',
style: TextStyle(
color: Color(
0xff1ffcb7)),
),
);
Scaffold.of(context)
.showSnackBar(
snackBar);
setState(() {
chosenPlatform =
choosingplatform;
});
},
value: chosenPlatform,
isExpanded: false,
hint: new Text(
"Choose Platform",
style: TextStyle(
color: Colors.black),
),
),
],
);
}
})),
这是凸起按钮的代码
RaisedButton(
padding:
EdgeInsets.only(left: 10, right: 10),
onPressed: () async {
Firestore.instance.runTransaction(
(Transaction transaction) async {
final CollectionReference reference =
Firestore.instance
.collection('Tournaments');
await reference
.document('Fifa Tournaments')
.collection('Fifa Tourneys')
.add({
'tourneyname':
tourneynameController.text,
'tourneygame':
tourneydateController.text,
});
tourneynameController.clear();
tourneydateController.clear();
});
Navigator.of(context).pop();
},
child: Text(
'Create Tournament',
style: TextStyle(fontSize: 16),
),
color: const Color(0xff1ffcb7),
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(18.0),
side: BorderSide(color: Colors.white),
),
),
现在,我只能保存 TextFormFields 的值,不能保存下拉列表。 简而言之,我现在正在做的是从集合中检索值并将它们显示在下拉菜单中,但我想做的是检索值,然后将选定的值保存在另一个名为 'Fifa tourneys' 的集合中,当表格已提交。 谢谢!
已解决! 我所要做的就是将 selectedIndex 的变量保存到我的 firestore 集合中。
onPressed: () async {
Firestore.instance.runTransaction(
(Transaction transaction) async {
final CollectionReference reference =
Firestore.instance
.collection('Tournaments');
await reference
.document('Fifa Tournaments')
.collection('Fifa Tourneys')
.add({
'tourneyname':
tourneynameController.text,
'tourneygame':
tourneydateController.text,
'tourneyplatform': chosenPlatform,// this is the one
});
tourneynameController.clear();
tourneydateController.clear();
chosenPlatform.clear();
});
Navigator.of(context).pop();
},