Flutter:无法将DropdownButton选择UI更新到AlertDialog

时间:2019-12-11 11:21:58

标签: flutter flutter-layout

我想用选定的项目值更新初始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);
                      });
                    },
                  ),
                ],
              ),
              

1 个答案:

答案 0 :(得分:3)

在AlertDialog中,支架状态不起作用,因此您必须使用 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);
                          });
                        },
                      )
                    ],
                  ),
                ]),
          );
        },

        ),
      ),
);
}