Flutter DropDown Widget问题

huangapple go评论75阅读模式
英文:

Flutter DropDown Widget issue

问题

我有一个下拉菜单,我想显示国家,但我想要国家代码作为值。

class CountryDropdown extends StatefulWidget {
  const CountryDropdown({Key? key}) : super(key: key);

  @override
  _CountryDropdownState createState() => _CountryDropdownState();
}

class _CountryDropdownState extends State<CountryDropdown> {
  String _selectedCountry = "Austria";

  List<DropdownMenuItem<String>> get dropdownItems {
    List<DropdownMenuItem<String>> menuItems = [
      const DropdownMenuItem(child: Text("Austria"), value: "au"),
      const DropdownMenuItem(child: Text("Belgium"), value: "be"),
      const DropdownMenuItem(child: Text("Croatia"), value: "hr"),
    ];
    return menuItems;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(10.0),
      child: DropdownButton<String>(
        value: _selectedCountry,
        hint: const Text('Select a country'),
        onChanged: (newValue) {
          setState(() {
            _selectedCountry = newValue!;
          });
        },
        items: dropdownItems,
      ),
    );
  }
}

然而,如果我将列表更改为以下内容,它会起作用。

const DropdownMenuItem(child: Text("Austria"), value: "Austria"),

此外,如果我更改为以下内容,它也会起作用。

const DropdownMenuItem(child: Text("au"), value: "Austria"),

这是怎么回事?

这里是错误的截图:
Flutter DropDown Widget问题

英文:

I have a dropdown which I want to display countries but I want the value to the country code.

class CountryDropdown extends StatefulWidget {
  const CountryDropdown({Key? key}) : super(key: key);

  @override
  _CountryDropdownState createState() =&gt; _CountryDropdownState();
}

class _CountryDropdownState extends State&lt;CountryDropdown&gt; {
  String _selectedCountry = &quot;Austria&quot;;

  List&lt;DropdownMenuItem&lt;String&gt;&gt; get dropdownItems {
    List&lt;DropdownMenuItem&lt;String&gt;&gt; menuItems = [
      const DropdownMenuItem(child: Text(&quot;Austria&quot;), value: &quot;au&quot;),
      const DropdownMenuItem(child: Text(&quot;Belgium&quot;), value: &quot;be&quot;),
      const DropdownMenuItem(child: Text(&quot;Croatia&quot;), value: &quot;hr&quot;),
    ];
    return menuItems;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(10.0),
      child: DropdownButton&lt;String&gt;(
        value: _selectedCountry,
        hint: const Text(&#39;Select a country&#39;),
        onChanged: (newValue) {
          setState(() {
            _selectedCountry = newValue!;
          });
        },
        items: dropdownItems,
      ),
    );

However, if I change the list to this... it works.

const DropdownMenuItem(child: Text(&quot;Austria&quot;), value: &quot;Austria&quot;),

Also, if I change to this it works....

const DropdownMenuItem(child: Text(&quot;au&quot;), value: &quot;Austria&quot;),

What's going on here?

Here's the error:
Flutter DropDown Widget问题

答案1

得分: 1

Sure, here's the translation:

因为您的下拉项中没有任何一个项的值为"Austria",就像您的初始值一样 String _selectedCountry = "Austria"; //更改为"au"

late String _selectedCountry;

@override
void initState() {
  _selectedCountry = "au";
  super.initState();
}
英文:

it is because none of your dropdown item has the value "Austria" like your initial value String _selectedCountry = &quot;Austria&quot;; //chage to &quot;au&quot;

late String _selectedCountry;

@override
void initState() {
  _selectedCountry = &quot;au&quot;;
  super.initState();
}

huangapple
  • 本文由 发表于 2023年5月17日 13:55:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定