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

问题描述 投票:0回答:1

我想用选定的项目值更新初始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():这是我的带DropDownButton的AlertDialog中的代码的一部分。


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个回答
0
投票

在AlertDialog中,scaffold状态不起作用,所以您必须使用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);
                          });
                        },
                      )
                    ],
                  ),
                ]),
          );
        },

        ),
      ),
);
}
© www.soinside.com 2019 - 2024. All rights reserved.