簡體   English   中英

選擇新值后 Flutter DropDownButton 值不會改變

[英]Flutter DropDownButton value not changing after selecting a new value

我一直在嘗試制作一個外部 UI,用戶可以使用它對雲中的數據庫 (dynamodb) 進行某些更改。 當我選擇一個新值時,我希望它顯示用戶想要進行的更改,而不實際更改數據庫。 僅當我按下應用欄上的按鈕時才會保存更改。 此外,當我使用 setState 重建按鈕時,雲上的值不會更改,它還會更改列中所有按鈕的值(沒有 setState 也能正常工作)。 當我按下保存圖標時,我提供的代碼會更改數據庫,但下拉按鈕的值保持不變,除非我刷新頁面。 如果我沒有足夠清楚地解釋我的問題,我深表歉意,這是我第一次在 Stackoverflow 上發帖,我仍在學習如何使用 flutter 和 aws amplify。

body: InteractiveViewer(
    constrained: false,
    child: DataTable(
        columns: [
          DataColumn(label: Text('Apt #')),
          DataColumn(label: Text('Type')),
          DataColumn(label: Text('Availability')),
          DataColumn(label: Text('Price')),
          DataColumn(label: Text('Area'))
        ],
        rows: aprts.map<DataRow>((element) { //aprts is a list that contains apartment objects.
          return DataRow(cells: [
            DataCell(Text(element.aptNum.toString())),
            DataCell(Text(element.type)),
            DataCell(DropdownButton<String>( /// this is the part im having problems wi
              value: element.availabily, // gets the value for the availability attribute for the element and stores it into value.
              onChanged: (String newValue) {
                availValue = newValue; //stores the newValue in a global variable that is used in a function that is used toactually make changes to the database.
                tempAvail = element;
                
              },
              items: <String>['AVAILABLE', 'SOLD', 'RESERVED']
                  .map((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
            )), // end of problem. 
            DataCell(TextField(
              controller: TextEditingController()
                ..text = element.price.toString(),
              onChanged: (text) {
                aptPrice = text;
                tempPrice = element;
              },
            )),
            DataCell(TextField(
              controller: TextEditingController()..text = element.area,
              onChanged: (text) {
                aptArea = text; 
                tempArea = element;
              },
            )),
          ]);
        }).toList()),
  ),

應用程序的樣子。 按下按鈕后

onChanged: (String newValue) {
                setState(() {
                   availValue = newValue; 
                   tempAvail = element;
                  }
                 )
                },

因為對於 UI 中的每個更改,您都必須調用setState((){})

暫無
暫無

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

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