【问题标题】:Flutter: cannot update DropdownButton selection UI into an AlertDialogFlutter:无法将 DropdownButton 选择 UI 更新为 AlertDialog
【发布时间】:2020-04-04 16:06:13
【问题描述】:

我想用所选项目值更新初始DropDownButton显示屏,但由于某种原因,UI在选择项目时不会更新。 实际上,我有一个由 Text() 和 RisedButton() 组成的 ListView,当单击 RaisedButton 时,会显示一个 AlertDialog。 一切都是用 StatefulWidget 构建的。

这是我的 ListView.builder:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_name),
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  child: _getCard(index),
                );
              },
              itemCount: threshs.length,
            ),
          ),
        ],
      ),
    );
  }

getCard(): 创建 UI ListView

_getCard(int index) {
    Thresh thresh = threshs[index];

    return Card(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            thresh.column_value,
            style: TextStyle(fontSize: 25),
          ),
          RaisedButton(
            child: Text("Imposta"),
            onPressed: () {
              (thresh.type_text)
                  ? _typeTrue(thresh.column_value, value: thresh.thresh_min)
                  : _typeFalse(thresh.id, thresh.column_value,
                      threshMin: thresh.thresh_min,
                      threshMax: thresh.thresh_max);
            },
          ),
        ],
      ),
    );
  }

_typeTrue():这是我的 AlertDialog 中带有 DropDownButton 的部分代码。


var selectedValue; // display selected item
static final type = ["!=", "="]; // dropdown items

_typeTrue(String columnValue, {String value}) {
    return showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(columnValue),
        content: Container(
          height: 200,
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("Segno"),
                  DropdownButton(
                    hint: new Text("Seleziona un valore"),
                    value: selectedValue,
                    items: type.map((String newValue) {
                      return DropdownMenuItem(
                        value: newValue,
                        child: new Text(newValue),
                      );
                    }).toList(),
                    onChanged: (String val) {
                      // update value
                      setState(() {
                        selectedValue = val;
                        print(selectedValue);
                      });
                    },
                  ),
                ],
              ),
              

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    在 AlertDialog 中 scaffold state 不起作用,因此您必须使用 StatefulBuilder 提供它自己的状态来更改 AlertDialog 中的状态

    _typeTrue(String columnValue, {String value}) {
    return showDialog(
      context: context,
      builder: (context) =>
          AlertDialog(
            title: Text(columnValue),
            content: StatefulBuilder(builder: (BuildContext context, state) {
              return Container(
                height: 200,
                child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text("Segno"),
                          DropdownButton(
                            hint: new Text("Seleziona un valore"),
                            items: type.map((String newValue) {
                              return DropdownMenuItem(
                                value: newValue,
                                child: new Text(newValue),
                              );
                            }).toList(),
                            value: selectedValue,
                            onChanged: (String val) {
                              state(() {
                                selectedValue = val;
                                print(selectedValue);
                              });
                            },
                          )
                        ],
                      ),
                    ]),
              );
            },
    
            ),
          ),
    );
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 2021-09-02
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2021-05-25
      • 2019-07-01
      • 2021-05-14
      相关资源
      最近更新 更多