下拉菜单重置值(颤振) [英] Reset value on dropdown (Flutter)

查看:91
本文介绍了下拉菜单重置值(颤振)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个下拉菜单,分别是州和城市,基本上,当用户选择一个州时,它将自动设置州的城市值。当我选择州时,会出现城市的下拉列表,在我选择城市后,如果我想将州更改回去,则会出现错误

I have two dropdown which is states and cities, basically when user choose a state, it will automatically set the value of cities for the states. When i choose the state, the dropdown for cities appear, and after i choose the cities, if i want to change the state back, it will give error

另一个异常被抛出:'package:flutter / src / material / dropdown.dart':断言失败:行513位置15:'items == null || value == null || items.where((DropdownMenuItem item)=> item。 value == value).length == 1':不正确。

"Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 513 pos 15: 'items == null || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true."

List state = [
"Kuala Lumpur",
"Selangor",
"Johor",
"Kedah",
"Kelantan",
"Melaka",
"Negeri Sembilan",
"Pahang",
"Penang",
"Perak",
"Perlis",
"Sabah",
"Sarawak",
"Terengganu"

 ];

List kl = [
"Ampang Hilir",
"Bandar Damai Perdana",
"Bandar Menjalara",
"Bandar Tasik Selatan",
"Bangsar",
"Bangsar South",];

List sel = [
"Ampang",
"Ara Damansara",
"Balakong",
"Bandar Bukit Raja",
"Bandar Kinrara",
"Bandar Puteri Puchong",
"Bandar Sunway",
"Bandar Utama",];

@override


void initState() {
    super.initState();

_dropDownMenuStates = getDropDownMenuState();

}

List<DropdownMenuItem<String>> getDropDownMenuState() {
List<DropdownMenuItem<String>> state1 = new List();
for (String statelist in state) {
  state1.add(
      new DropdownMenuItem(value: statelist, child: new Text(statelist)));
}
return state1;


}

List<DropdownMenuItem<String>> getDropDownMenuKL() {
List<DropdownMenuItem<String>> kl1 = new List();
for (String kllist in kl) {
  kl1.add(new DropdownMenuItem(value: kllist, child: new Text(kllist)));
}
return kl1;


 }

  List<DropdownMenuItem<String>> getDropDownMenuSEL() {
    List<DropdownMenuItem<String>> sel1 = new List();
    for (String sellist in sel) {
      sel1.add(new DropdownMenuItem(value: sellist, child: new Text(sellist)));
    }
    return sel1;
  }

    Expanded(
  child: PhysicalModel(
      borderRadius:
          new BorderRadius.circular(50.0),
      color: Colors.white,
      child: new Container(
          padding: EdgeInsets.only(
              left: 10.0, right: 10.0),
          height: 40.0,
          decoration: new BoxDecoration(
              borderRadius:
                  new BorderRadius
                      .circular(50.0),
              border: new Border.all(
                width: 3.0,
                color: Colors.grey[300],
              )),
          child: new FittedBox(
            fit: BoxFit.contain,
            child: DropdownButton(
              hint: new Text(
                  allTranslations
                      .text('city')),
              value: _currentCity,
              items: _dropDownMenuCity,
              onChanged:
                  changedDropDownCity,
            ),
          ))),
),

SizedBox(
  width: 10.0,
),
Expanded(
  child: PhysicalModel(
      borderRadius:
          new BorderRadius.circular(50.0),
      color: Colors.white,
      child: new Container(
          padding: EdgeInsets.only(
              left: 10.0, right: 10.0),
          height: 40.0,
          decoration: new BoxDecoration(
              borderRadius:
                  new BorderRadius
                      .circular(50.0),
              border: new Border.all(
                width: 3.0,
                color: Colors.grey[300],
              )),
          child: new FittedBox(
            fit: BoxFit.contain,
            child: DropdownButton(
              hint: new Text(
                  allTranslations
                      .text('state')),
              value: _currentState,
              items: _dropDownMenuStates,
              onChanged:
                  changedDropDownState,
            ),
          ))),
),

void changedDropDownState(String selectedState) {
setState(() {
  _currentState = selectedState;
  if (selectedState.toString() == "Kuala Lumpur") {
    _dropDownMenuCity = getDropDownMenuKL();
  } else if (selectedState.toString() == "Selangor") {
    _dropDownMenuCity = getDropDownMenuSEL();}


});


}

  void changedDropDownCity(String selectedCity) {
    setState(() {
      _currentCity = selectedCity;
    });
  }


推荐答案

您需要清除 _currentCity ,然后设置新的城市列表。
是因为 DropdownButton 项中等待有效的(_currentCity)(_dropDownMenuCity)。

You need clear _currentCity before set a new list of Cities. Because the DropdownButton wait a valid value (_currentCity) in items (_dropDownMenuCity).

void changedDropDownState(String selectedState) {
setState(() {
  // <<<
  _dropDownMenuCity = null;
  _currentCity = null;
  // <<<

  _currentState = selectedState;

  if (selectedState.toString() == "Kuala Lumpur") {
    _dropDownMenuCity = getDropDownMenuKL();
  } else if (selectedState.toString() == "Selangor") {
    _dropDownMenuCity = getDropDownMenuSEL();
  }
});

}

https://gist.github.com/dyegovieira/a2f78d241090a77939100e380987b8a1

这篇关于下拉菜单重置值(颤振)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆