【问题标题】:DropdownButton - Set value: with value NOT contained by The list of items the user can select - FlutterDropdownButton - 设置值:值不包含在用户可以选择的项目列表中 - Flutter
【发布时间】:2021-09-20 21:13:08
【问题描述】:

我有这个下拉按钮

列表中有两个项目

当我选择值 2 时,我希望它显示列表中未包含的值

像这样:

有可能吗?

看起来像这样:

DropdownButton<String>(
        value: dropdownValue,
        items: <String>[
          'Value 1',
          'Value 2',
        ].map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
        onChanged: (String? newValue) {
          setState(() {
            dropdownValue = newValue!;

            if (dropdownValue == 'Value 2') {
              dropdownValue = 'Value Not in  list of items';
            }
          });
        },
      ),


如您所见,如果我设置 dropdownValue = 'Value Not in list of items';,我当然会收到错误,因为它不在列表中 - 但我希望它能够工作 :)

另一方面,正如预期的那样,这是可行的:

if (dropdownValue == 'Value 2') {
  dropdownValue = 'Value 1';
}

因为'Value 1' 在列表中 - 但我想在单击Value 2 时显示不在列表中的值

希望有人能帮忙! :)






[为了 SEO,这里是错误]:

Assertion failed:
../…/material/dropdown.dart:880
items == null ||
              items.isEmpty ||
              value == null ||
              items.where((DropdownMenuItem<T> item) {
                    return item.value == value;
                  }).length ==
                  1
"There should be exactly one item with [DropdownButton]'s value: Value Not in  list of items. \nEither zero or 2 or more [DropdownMenuItem]s were detected with the same value"

【问题讨论】:

    标签: flutter dropdownbutton flutter-dropdownbutton


    【解决方案1】:

    我已经这样做了, 问题是我们需要确保Item[current] == selected item.

    import 'package:flutter/material.dart';
    
    class MyStatefulWidget extends StatefulWidget {
      const MyStatefulWidget({Key? key}) : super(key: key);
    
      @override
      State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      String dropdownValue = 'Value 1';
    
      List<String> values = [
        'Value 1',
        'Value 2',
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: DropdownButton<String>(
            value: dropdownValue,
            onChanged: (String? newValue) {
              setState(
                () {
                  if (newValue == 'Value 2') {
                    values[1] = 'Value Not in  list of items';
                    dropdownValue = 'Value Not in  list of items';
                  }
                  if (newValue == 'Value 1') {
                    dropdownValue = 'Value 1';
                    values[1] = 'Value 2';
                  }
                },
              );
            },
            items: values.map<DropdownMenuItem<String>>((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );
            }).toList(),
          ),
        );
      }
    }
    
    

    【讨论】:

    • 这样我们正在修改列表values,我们真的想要这样的改变吗?
    • 这是一个很好的观点,我忘记了。我更喜欢你的回答。
    • 我不得不说我也喜欢这个答案,它可能很有用,但确实@abdev 的答案对我的问题更正确
    【解决方案2】:

    如果您只想在选择Value 2显示一些文本,您可以尝试hint 小部件。

      String hintValue = 'Select Value'; // ---- hint
      var dropdownValue;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text(widget.title),
            ),
            body: Column(
              children: [
                ElevatedButton(child: Text('Press here'), onPressed: null), // ---- ignore
                DropdownButton<String>(
                  value: dropdownValue,
                  hint: Text(hintValue), // ---- hint
                  isExpanded: true,
                  items: <String>[
                    'Value 1',
                    'Value 2',
                  ].map<DropdownMenuItem<String>>((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  }).toList(),
                  onChanged: (newValue) {
                    setState(() {
                      // ----- if Value 2 is selected
                      if (newValue == 'Value 2') {
                        hintValue = 'Value Not in list of items';
                      } else
                        hintValue = '$newValue';  
                    });
                  },
                ),
              ],
            ));
      }
    

    输出

    注意Documentation 用于使用 hint 小部件

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 2020-09-17
      • 2021-09-02
      • 2021-01-27
      • 2020-05-07
      • 1970-01-01
      • 2021-05-25
      • 2022-11-03
      • 1970-01-01
      相关资源
      最近更新 更多