微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

DropdownButton - 设置值:值不包含在用户可以选择的项目列表中 - Flutter

如何解决DropdownButton - 设置值:值不包含在用户可以选择的项目列表中 - Flutter

我有这个下拉按钮

enter image description here

列表中有两个项目

enter image description here

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

像这样:

enter image description here

有可能吗?

它看起来像这样:

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';
            }
          });
        },),


enter image description here

如您所见,如果我设置 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"

解决方法

我已经这样做了, 问题是我们需要确保 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(),),);
  }
}

,

如果您只想在选择 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',].map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,);
              }).toList(),onChanged: (newValue) {
                setState(() {
                  // ----- if Value 2 is selected
                  if (newValue == 'Value 2') {
                    hintValue = 'Value Not in list of items';
                  } else
                    hintValue = '$newValue';  
                });
              },],));
  }

输出

enter image description here

注意Documentation 用于使用 hint 小部件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。