gpt4 book ai didi

dart - Flutter - DropdownButtonFormField 值未更新

转载 作者:IT王子 更新时间:2023-10-29 06:51:00 25 4
gpt4 key购买 nike

在关闭并重新打开 AlertDialog 框之前,我的下拉按钮值不会更新。

我在类(class)的顶部设置了变量

class _ItemListState extends State<ItemList> {
int _ratingController;
...

}

在类中我有一个打开表单的 AlertDialog,在这里我有 DropdownButtonFormField


AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
controller: _eateryController,
autofocus: true,
decoration:
InputDecoration(labelText: 'Eatery', hintText: 'eg Pizza Hut'),
),
TextField(
controller: _supplierController,
decoration: InputDecoration(
labelText: 'Supplier', hintText: 'eg Deliveroo'),
),
TextField(
controller: _descriptionController,
decoration: InputDecoration(
labelText: 'Description', hintText: 'eg cheese pizza'),
),
DropdownButtonFormField<int>(
value: _ratingController,
items: [1, 2, 3, 4, 5]
.map((label) => DropdownMenuItem(
child: Text(label.toString()),
value: label,
))
.toList(),
hint: Text('Rating'),
onChanged: (value) {
setState(() {
_ratingController = value;
});
},
),
],
),
actions: <Widget>[
FlatButton(
onPressed: () {
_handleSubmit(_eateryController.text, _supplierController.text,
_descriptionController.text, _ratingController);
Navigator.pop(context);
},
child: Text('Save'),
),
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text('Cancel'),
)
],
);

setState 似乎没有动态更新字段值。更新后的值只会在我关闭并重新打开 AlertDialog 后显示。

我怎样才能让它立即更新?

谢谢

最佳答案

您需要创建一个新的 StatefulWidget 类,它应该返回您的 AlertDialog

class MyDialog extends StatefulWidget {
@override
_MyDialogState createState() => _MyDialogState();
}

class _MyDialogState extends State<MyDialog> {
int _ratingController;

@override
Widget build(BuildContext context) {
return AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
controller: _eateryController,
autofocus: true,
decoration:
InputDecoration(labelText: 'Eatery', hintText: 'eg Pizza Hut'),
),
TextField(
controller: _supplierController,
decoration: InputDecoration(
labelText: 'Supplier', hintText: 'eg Deliveroo'),
),
TextField(
controller: _descriptionController,
decoration: InputDecoration(
labelText: 'Description', hintText: 'eg cheese pizza'),
),
DropdownButtonFormField<int>(
value: _ratingController,
items: [1, 2, 3, 4, 5]
.map((label) => DropdownMenuItem(
child: Text(label.toString()),
value: label,
))
.toList(),
hint: Text('Rating'),
onChanged: (value) {
setState(() {
_ratingController = value;
});
},
),
],
),
actions: <Widget>[
FlatButton(
onPressed: () {
_handleSubmit(_eateryController.text, _supplierController.text,
_descriptionController.text, _ratingController);
Navigator.pop(context);
},
child: Text('Save'),
),
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text('Cancel'),
)
],
);
}
}

这样使用

showDialog(
context: context,
builder: (context) {
return MyDialog();
},
);

关于dart - Flutter - DropdownButtonFormField 值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55600319/

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