如何在Flutter中创建单选的下拉列表?

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

How to Create a Single Selected Dropdown List in Flutter?

问题

我是一个Flutter初学者。如何在Flutter中创建一个单选的下拉列表?像这样。我尝试过,但没有得到我想要的结果。

  1. String dropdownvalue = 'Bahrain';
  2. Container(
  3. width: 308,
  4. child: DropdownButton(
  5. // 初始值
  6. value: dropdownvalue,
  7. // 下箭头图标
  8. icon: const Icon(Icons.keyboard_arrow_down),
  9. // 项目列表数组
  10. items: ['Bahrain', 'Option 2', 'Option 3', 'Option 4']
  11. .map((String item) {
  12. return DropdownMenuItem(
  13. value: item,
  14. child: Text(item),
  15. );
  16. }).toList(),
  17. // 选择所需选项后,它会将按钮的值更改为所选值
  18. onChanged: (String? newValue) {
  19. setState(() {
  20. dropdownvalue = newValue!;
  21. });
  22. },
  23. ),
  24. );
英文:

I am a flutter beginner. How to Create a Single Selected Dropdown List in Flutter?
Like This.<br>
I tried, but I didn't get what I wanted.

String dropdownvalue = 'Bahrain';

  1. Container(
  2. width: 308,
  3. child: DropdownButton(
  4. // Initial Value
  5. value: dropdownvalue,
  6. // Down Arrow Icon
  7. icon: const Icon(Icons.keyboard_arrow_down),
  8. // Array list of items
  9. items: dropdownvalue.map((String dropdownvalue) {
  10. return DropdownMenuItem(
  11. value: dropdownvalue,
  12. child: Text(dropdownvalue),
  13. );
  14. }).toList(),
  15. // After selecting the desired option,it will
  16. // change button value to selected value
  17. onChanged: (String? newValue) {
  18. setState(() {
  19. dropdownvalue = newValue!;
  20. });
  21. },
  22. ),
  23. ),

答案1

得分: 1

以下是代码部分的中文翻译:

  1. class _YourState extends State<MyHomePage> {
  2. List<String> countries = [
  3. '巴林',
  4. '印度',
  5. '伊拉克',
  6. '美国',
  7. ];
  8. String? dropdownvalue;
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. body: Center(
  13. child: DropdownButton(
  14. // 初始值
  15. value: dropdownvalue,
  16. icon: const Icon(Icons.keyboard_arrow_down),
  17. isExpanded: true,
  18. items: countries.map((String dropdownvalue) {
  19. return DropdownMenuItem(
  20. value: dropdownvalue,
  21. child: Text(dropdownvalue),
  22. );
  23. }).toList(),
  24. hint: Text('国家'),
  25. onChanged: (String? newValue) {
  26. setState(() {
  27. dropdownvalue = newValue!;
  28. });
  29. },
  30. ),
  31. ),
  32. );
  33. }
  34. }
英文:

You should have list of items and selectedItem to manage the list and selection state.

  1. class _YourState extends State&lt;MyHomePage&gt; {
  2. List&lt;String&gt; countries = [
  3. &#39;Bahrain&#39;,
  4. &#39;India&#39;,
  5. &#39;Iraq&#39;,
  6. &#39;America&#39;,
  7. ];
  8. String? dropdownvalue ;
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. body: Center(
  13. child: DropdownButton(
  14. // Initial Value
  15. value: dropdownvalue,
  16. icon: const Icon(Icons.keyboard_arrow_down),
  17. isExpanded: true,
  18. items: countries.map((String dropdownvalue) {
  19. return DropdownMenuItem(
  20. value: dropdownvalue,
  21. child: Text(dropdownvalue),
  22. );
  23. }).toList(),
  24. hint: Text(&#39;Country&#39;),
  25. onChanged: (String? newValue) {
  26. setState(() {
  27. dropdownvalue = newValue!;
  28. });
  29. },
  30. ),
  31. ),
  32. );
  33. }
  34. }

huangapple
  • 本文由 发表于 2023年2月18日 15:20:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491799.html
匿名

发表评论

匿名网友

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

确定