簡體   English   中英

Flutter:無法將 DropdownButton 選擇 UI 更新為 AlertDialog

[英]Flutter: cannot update DropdownButton selection UI into an AlertDialog

我想使用所選項目值更新初始DropDownButton顯示,但由於某種原因,UI在選擇項目時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);
                      });
                    },
                  ),
                ],
              ),
              

在 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);
                          });
                        },
                      )
                    ],
                  ),
                ]),
          );
        },

        ),
      ),
);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM