小编典典

Flutter-DropdownButtonFormField值未更新

flutter

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

我在班级顶部设置了变量

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后,更新的值才会显示。

如何获得此即时更新?

谢谢


阅读 662

收藏
2020-08-13

共1个答案

小编典典

您需要创建一个新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();
  },
);
2020-08-13