【问题标题】:Dynamically Change data in a dropdown menu - FLUTTER在下拉菜单中动态更改数据 - FLUTTER
【发布时间】:2021-03-10 05:40:51
【问题描述】:

我有一个类别的水平列表;例如汽车及其下方有一个下拉菜单获取列表中选择的每辆汽车的模型列表。所以我的问题是在我选择例如丰田之后,下拉菜单中充满了模型,例如 - 凯美瑞,Vits,Caldina,Hilux,这很好......但是我选择了一个,例如凯美瑞并且在汽车列表中我选择了另一辆汽车例如 HONDA .. UI 坏了.. 谁能告诉我如何有效地重用相同的下拉菜单,并且即使选择了一项,也只是更改它从中获取的数组列表

【问题讨论】:

    标签: flutter dart dropdown


    【解决方案1】:

    我做了类似的事情:

    对于汽车品牌:

      int selectedBrandItem;
      int selectedModelItem;
      int selectedByYearItem;
      var listTemp;
    Container(
                                    width: _size.width,
                                    height: _size.height* 0.08,
                                    decoration: BoxDecoration(
                                      borderRadius:  BorderRadius.circular(16.0),
                                      border: Border.all(
                                          color: Theme.greyColor[600]
                                      ),
                                    ),
                                    alignment: Alignment.center,
                                    child: Padding(
                                      padding: const EdgeInsets.symmetric(horizontal:10.0),
                                      child: DropdownButton(
                                        hint: Text(allTranslations.text('brand')),
                                        value: selectedBrandItem,
                                        onChanged: (value){
                                          setState(() {
                                            selectedBrandItem = value;
                                             selectedModelItem = null;
                                            selectedByYearItem = null;
                                            
                                          });
                                        },
                                        items: (state.response.brandList == null || state.response.brandList == [])?listTemp:state.response.brandList.map((e) =>
                                            DropdownMenuItem(
                                                value: e.id,
                                                child: Text(
                                                  e.brandName,
                                                  style: TextStyle(
                                                      color: Theme.GreyColor,
                                                      fontWeight: FontWeight.w400,
                                                      fontSize: 16.0
                                                  ),
                                                )
                                            )
                                        ).toList()??[],
                                        isExpanded: true,
                                        icon: Icon(Icons.keyboard_arrow_down, color: Theme.greyColor[500],),
                                        underline: Container(color: Colors.transparent,),
                                      ),
                                    ),
                                  ); 
    

    车型:

    Container(
                                    width: _size.width,
                                    height: _size.height* 0.08,
                                    decoration: BoxDecoration(
                                      borderRadius:  BorderRadius.circular(16.0),
                                      border: Border.all(
                                          color: Theme.greyColor[600]
                                      ),
                                    ),
                                    alignment: Alignment.center,
                                    child: Padding(
                                      padding: const EdgeInsets.symmetric(horizontal:10.0),
                                      child: DropdownButton(
                                        hint: Text(allTranslations.text('model')),
                                        value: selectedModelItem,
                                        onChanged: (value){
                                          setState(() {
                                            selectedModelItem = value;
                                            selectedByYearItem = null;
                                          
                                          });
                                        },
                                        items: state.response.modelList.map((e) =>
                                            DropdownMenuItem(
                                                value: e.id,
                                                child: Text(
                                                  e.modelName,
                                                  style: TextStyle(
                                                      color: Theme.GreyColor,
                                                      fontWeight: FontWeight.w400,
                                                      fontSize: 16.0
                                                  ),
                                                )
                                            )
                                        ).toList()??[],
                                        isExpanded: true,
                                        icon: Icon(Icons.keyboard_arrow_down, color: Theme.greyColor[500],),
                                        underline: Container(color: Colors.transparent,),
                                      ),
                                    ),
                                  );
    

    车年:

    Container(
                                    width: _size.width,
                                    height: _size.height* 0.08,
                                    decoration: BoxDecoration(
                                      borderRadius:  BorderRadius.circular(16.0),
                                      border: Border.all(
                                          color: Theme.greyColor[600]
                                      ),
                                    ),
                                    alignment: Alignment.center,
                                    child: Padding(
                                      padding: const EdgeInsets.symmetric(horizontal:10.0),
                                      child: DropdownButton(
                                        hint: Text(allTranslations.text('year')),
                                        value: selectedByYearItem,
                                        onChanged: (value){
                                          setState(() {
                                            selectedByYearItem = value;
                                            
                                          });
                                        },
                                        items: (state.response.yearList == null || state.response.yearList  == [] )?listTemp:state.response.yearList .map((e) =>
                                            DropdownMenuItem(
                                                value: e.id,
                                                child: Text(
                                                  e.year,
                                                  style: TextStyle(
                                                      color: Theme.GreyColor,
                                                      fontWeight: FontWeight.w400,
                                                      fontSize: 16.0
                                                  ),
                                                )
                                            )
                                        ).toList()??[],
                                        isExpanded: true,
                                        icon: Icon(Icons.keyboard_arrow_down, color: Theme.greyColor[500],),
                                        underline: Container(color: Colors.transparent,),
                                      ),
                                    ),
                                  );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2018-03-02
      • 2013-11-05
      相关资源
      最近更新 更多