簡體   English   中英

應該只有一項具有 [DropdownButton] 的值

[英]There should be exactly one item with [DropdownButton]'s value

我正在為可以將數據發布到服務器的電子商務系統構建一個應用程序。 有多個項目類別具有不同的和可自定義的值。 例如,筆記本電腦類別可以具有處理器、內存、存儲大小屬性。 其中一些屬性是文本框(例如模型),而其他屬性需要是下拉列表(例如處理器 - 核心 i7、i5 等...)。 屬性的數量不能固定,因為它取決於類別(可能隨時添加或刪除)

我正在嘗試構建一個表單,該表單將根據屬性類型(AttributeType 列)將此屬性顯示為文本框或下拉列表。 我能夠顯示文本框和下拉列表(成功顯示它們的元素。我遇到的問題是訪問下拉值以創建對服務器的發布請求。

這是代碼

 FutureBuilder<FormListModel>(
                future: _formList,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return ListView.builder(
                      primary: false,
                      scrollDirection: Axis.vertical,
                      shrinkWrap: true,
                      itemCount: snapshot.data.customattributes.length,
                      itemBuilder: (context, index) {
                        var item = snapshot.data.customattributes[index];
                        print(item.name);
                        
                        if (item.AttributeType == 'Text') {
                          return Container(
                            //padding: EdgeInsets.only(top: 8),
                            margin:
                                EdgeInsets.only(top: 15, left: 15, right: 15),
                            child: Column(
                              children: [
                                Form(
                                  child: TextFormField(
                                    controller: model_controller,
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.symmetric(
                                          vertical: 10, horizontal: 10),
                                      labelText: item.name,
                                      border: OutlineInputBorder(
                                        borderSide: BorderSide(
                                            color: Colors.blueAccent),
                                      ),
                                    ),
                                    onChanged: (value) {
                                      //print(model_controller.text);
                                    },
                                  ),
                                ),
                              ],
                            ),
                          );
                        } else if (item.AttributeType == 'Selectlist') {
                          return Container(
                            //padding: EdgeInsets.only(top: 8),
                            margin:
                                EdgeInsets.only(top: 20, left: 15, right: 15),
                            child: Column(
                              children: [
                                Form(
                                  child: InputDecorator(
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.symmetric(
                                          vertical: 12, horizontal: 12),
                                      labelText: item.name,
                                      labelStyle: TextStyle(
                                          fontSize: 20,
                                          color: Colors.blueAccent),
                                      border: const OutlineInputBorder(),
                                    ),
                                    child: DropdownButtonHideUnderline(
                                      child: DropdownButton(
                                        isDense: true,
                                        icon: Icon(Icons.keyboard_arrow_down),
                                        value: selectedAttribute,
                                        onChanged: (newValue) {
                                          setState(() {
                                            selectedAttribute = newValue;
                                          });
                                        },
                                        //decoration: InputDecoration(border: InputBorder.none),
                                        items: item.children
                                            .map<DropdownMenuItem>((items) {
                                          return DropdownMenuItem<String>(
                                            child: Row(
                                              children: [
                                                Padding(
                                                  padding:
                                                      EdgeInsets.only(top: 7),
                                                  child: Text(items.element),
                                                ),
                                              ],
                                            ),
                                            value: items.element,
                                          );
                                        }).toList(),
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          );
                        } else {
                          return null;
                        }
                      },
                    );
                  } else {
                    return Container();
                  }
                }),

這是我們從服務器獲取的 json 文件,用於使用 ListView.builder 創建表單

{
    "category": {
        "CategoryName": "Laptops",
        "CategoryID": 34
    },
    "customattributes": [
        {
            "Name": "Model",
            "AttributeType": "Text",
            "AttributeID": 7
        },
        {
            "Name": "Processor",
            "AttributeType": "Selectlist",
            "AttributeID": 2,
            "Children": [
                {
                    "Element": "Intel Core i3"
                },
                {
                    "Element": "Intel Core i5"
                },
                {
                    "Element": "Intel Core i7"
                }
            ]
        },
        {
            "Name": "Storage Size",
            "AttributeType": "Selectlist",
            "AttributeID": 1,
            "Children": [                
                {
                    "Element": "1TB"
                },
                {
                    "Element": "2TB"
                },
                {
                    "Element": "2.5TB"
                }
            ]
        },
        {
            "Name": "RAM",
            "AttributeType": "Selectlist",
            "AttributeID": 3,
            "Children": [
                {
                    "Element": "12GB"
                },
                {
                    "Element": "16GB"
            ]
        }
    ],
    
}

這是表格

在此處輸入圖像描述

當我 select 下拉列表中的任何值時,我收到一條錯誤消息,提示應該只有一項具有 [DropdownButton] 的值

在此處輸入圖像描述

  1. 那么如何訪問每個下拉列表的值,以便向服務器發出 http 發布請求。 請記住,每個類別的下拉菜單(屬性)的數量是不同的。 第二個問題,不是那么重要,但有沒有一種方法可以使用 json 文件中屬性的“名稱”列為每個下拉列表分配一個名稱。

在 onChanged 方法中更改下拉值時,嘗試將其他下拉值設置為 null

問題在於分配的值和下拉列表的項目列表。

請注意,下拉菜單的值只能是下拉菜單項中的值。 對於自定義值,您必須比較所選項目的值並將項目的索引作為當前值。 希望你能理解我的擔憂。

按照下面的代碼:

value : getSelectedValue(yourValue);
...
getSelectedValue(yourValue){
    //add this line of code
    if(yourValue == null) return null;
    for(value in dropDownItems){
        if(value.id == yourValue.id) return value;
    }
    return null;
}

我也有同樣的問題...我設法通過檢查我的列表是否為空來解決它..如果是我確實將值設置為 null 否則選擇列表中的第一項

value: Provider.of<DepositProvider>(context)
                          .paymentMethods.isNotEmpty ?Provider.of<DepositProvider>(context)
                          .paymentMethods.first : null,

暫無
暫無

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

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