在Flutter中,ListView.Builder中的DropDowns不会显示值

时间:2020-10-25 13:44:03

标签: flutter dart dropdown

我在ModalBottomSheet中有一个ListView.Builder。 在ListView中,我正在实现下拉列表。每个列表项都有两个。第二个下拉列表的值取决于第一个下拉列表的选定值。

当我在每个下拉列表中选择值时,打印品会显示我在控制台中选择的内容,但不会更新要在UI中显示的值。

我在这里做错了什么?有人可以帮我吗?我为此花了一整天。

在这里,我将提供构建器方法

 //drop down variables
  List<String> _categoryList = new List<String>();
  List<String> _foodsList = new List<String>();

  //reference the dropdown lists
  List<String> _selectedCat;
  List<String> _selectedFood;


ListView.builder(
                        itemCount: 5,
                        itemBuilder: (BuildContext context, int index) {
                          _selectedCat = new List(5) ;
                          _selectedFood = new List(5) ;
                          return Container(
                            margin: EdgeInsets.symmetric(
                                vertical: 4.0, horizontal: 4.0),
                            width: MediaQuery.of(context).size.width * 0.95,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.all(Radius.circular(
                                  10.0,
                                )),
                                color: Color(0xFF01816B),
                                shape: BoxShape.rectangle),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 4.0, vertical: 4.0),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Expanded(
                                        flex: 3,
                                        child: Text(
                                          "Category",
                                          style: kSubContentStyleWhiteLight,
                                        ),
                                      ),
                                      Expanded(
                                        flex: 3,
                                        child: Text(
                                          "Food Type",
                                          style: kSubContentStyleWhiteLight,
                                        ),
                                      ),
                                      Expanded(
                                        flex: 2,
                                        child: Text(
                                          "Quantity",
                                          style: kSubContentStyleWhiteLight,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 4.0, vertical: 4.0),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Expanded(
                                        flex: 2,
                                        child: Container(
                                          width:
                                          MediaQuery.of(context).size.width /
                                              3,
                                          padding: EdgeInsets.symmetric(
                                              vertical: 2.0, horizontal: 4.0),
                                          decoration: BoxDecoration(
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(
                                                    10.0,
                                                  )),
                                              color: Color(0xFF03B898),
                                              shape: BoxShape.rectangle),
                                          child: DropdownButton<String>( 
                                            value:_selectedCat[index],
                                            isDense: false,
                                            isExpanded: true,
                                            hint: Text(
                                              'Category',
                                              style: kSubContentStyleWhitSmaller,
                                            ),
                                            style:
                                            kSubContentStyleBlackLightSmall,
                                            onChanged:
                                                (String newVal){
                                              setState(() {
                                                _selectedFood[index] = null;
                                                _selectedCat[index] = newVal;
                                                print(_selectedCat[index]);
                                                _foodsList.clear();
                                                for(int i=0; i < _allCatList.length; i++){
                                                  if(_allCatList[i].name == _selectedCat[index] ){ 
                                                    foodTypeList = _allCatList[i].foods;
                                                    //
                                                    for(int j = 0; j<foodTypeList.length; j++){
                                                      _foodsList.add(foodTypeList[j].name);
                                                    }
                                                  }
                                                }
                                              });
                                            },
                                            items: _categoryList.map((cat) { 
                                              return new DropdownMenuItem<String>( 
                                                child: new Text(cat),
                                                value: cat,
                                              );
                                            }).toList(),
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        width: 2.0,
                                      ),
                                      Expanded(
                                        flex: 4,
                                        child: Container(
                                          width:
                                          MediaQuery.of(context).size.width /
                                              3,
                                          padding: EdgeInsets.symmetric(
                                              vertical: 2.0, horizontal: 4.0),
                                          decoration: BoxDecoration(
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(
                                                    10.0,
                                                  )),
                                              color: Color(0xFF03B898),
                                              shape: BoxShape.rectangle),
                                          child: DropdownButton<String>( 
                                            value: _selectedFood[index],
                                            isDense: false,
                                            isExpanded: true,
                                            hint: Text(
                                              'FoodType',
                                              style: kSubContentStyleWhitSmaller,
                                            ),
                                            style:
                                            kSubContentStyleBlackLightSmall,
                                            onChanged:
                                                (String newVal){
                                              setState(() {
                                                _selectedFood[index] = newVal;
                                                print(_selectedFood[index]);
                                              });
                                            },
                                            items: _foodsList.map((fdt) { //foodTypeList
                                              return new DropdownMenuItem<String>( // <CATM.Food>(
                                                //<CATM.Food>(
                                                child: new Text(fdt),
                                                value: fdt,
                                              );
                                            }).toList(),
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        width: 2.0,
                                      ),
                                      Expanded(
                                        flex: 2,
                                        child: Container(
                                          width:
                                          MediaQuery.of(context).size.width /
                                              3,
                                          padding: EdgeInsets.symmetric(
                                              vertical: 2.0, horizontal: 4.0),
                                          decoration: BoxDecoration(
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(
                                                    10.0,
                                                  )),
                                              color: Color(0xFF03B898),
                                              shape: BoxShape.rectangle),
                                          alignment: Alignment.center,
                                          child: TextFormField(
                                            inputFormatters: [
                                              new LengthLimitingTextInputFormatter(
                                                  3),
                                            ],
                                            keyboardType: TextInputType.number,
                                            autofocus: false,
                                            validator: validateNumberDecimal,
                                            onSaved: (String value) {
                                            },
                                            style: kSubContentStyleWhitSmaller,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          );
                        },
                      ),

0 个答案:

没有答案