Flutter 颤振-下拉菜单-项目-禁用?

Flutter 颤振-下拉菜单-项目-禁用?,flutter,Flutter,我在Flatter中发现了DropdownButton小部件,并对这些项目进行了研究。我现在的问题是,为什么在dropdownmenuitem小部件中没有禁用项目的选项?还是有办法做到这一点 我的想法是,我有多个下拉列表,具有相同的项目列表,如果我在下拉列表中选择一个项目,则应该在其他项目中禁用它。有什么想法吗? 没有官方方法禁用某些下拉菜单项,但您可以通过在选中时不选择已禁用的项并更改其颜色来模拟此功能。 否则,您需要复制下拉按钮,并自己添加此功能 import 'package:flutte

我在Flatter中发现了DropdownButton小部件,并对这些项目进行了研究。我现在的问题是,为什么在dropdownmenuitem小部件中没有禁用项目的选项?还是有办法做到这一点

我的想法是,我有多个下拉列表,具有相同的项目列表,如果我在下拉列表中选择一个项目,则应该在其他项目中禁用它。有什么想法吗?

没有官方方法禁用某些
下拉菜单项
,但您可以通过在选中时不选择已禁用的项并更改其颜色来模拟此功能。 否则,您需要复制
下拉按钮
,并自己添加此功能

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

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

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  final disabledItems  = ['Free', 'Four'];

  String dropdownValue = 'One';

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      icon: Icon(Icons.arrow_downward),
      iconSize: 24,
      elevation: 16,
      style: TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String newValue) {
        if (!disabledItems.contains(newValue)) {
          setState(() {
            dropdownValue = newValue;
          });
        }
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(
            value,
            style: TextStyle(
              color: disabledItems.contains(value) ? Colors.grey : null,
            ),
          ),
        );
      }).toList(),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
静态常量字符串_title='颤振代码示例';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
家:脚手架(
appBar:appBar(标题:常量文本(_title)),
正文:中(
子项:MyStatefulWidget(),
),
),
);
}
}
类MyStatefulWidget扩展了StatefulWidget{
MyStatefulWidget({Key}):超级(Key:Key);
@凌驾
_MyStatefulWidgetState createState()=>\u MyStatefulWidgetState();
}
类_MyStatefulWidgetState扩展状态{
最终禁用项=[“自由”,“四];
字符串dropdownValue='One';
@凌驾
小部件构建(构建上下文){
返回下拉按钮(
value:dropdownValue,
图标:图标(图标。向下箭头),
iconSize:24,
海拔:16,
样式:TextStyle(颜色:Colors.deepPurple),
下划线:容器(
身高:2,
颜色:颜色。深紫色,
),
onChanged:(字符串newValue){
如果(!disabledItems.contains(newValue)){
设置状态(){
dropdownValue=newValue;
});
}
},
物品:[“一”、“二”、“免费”、“四”]
.map((字符串值){
返回下拉菜单项(
价值:价值,
子:文本(
价值
样式:TextStyle(
颜色:disabledItems.包含(值)?颜色。灰色:null,
),
),
);
}).toList(),
);
}
}

没有官方方法禁用某些
下拉菜单项
,但您可以通过在选中时不选择已禁用的项并更改其颜色来模拟此功能。 否则,您需要复制
下拉按钮
,并自己添加此功能

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

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

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  final disabledItems  = ['Free', 'Four'];

  String dropdownValue = 'One';

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      icon: Icon(Icons.arrow_downward),
      iconSize: 24,
      elevation: 16,
      style: TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String newValue) {
        if (!disabledItems.contains(newValue)) {
          setState(() {
            dropdownValue = newValue;
          });
        }
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(
            value,
            style: TextStyle(
              color: disabledItems.contains(value) ? Colors.grey : null,
            ),
          ),
        );
      }).toList(),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
静态常量字符串_title='颤振代码示例';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
家:脚手架(
appBar:appBar(标题:常量文本(_title)),
正文:中(
子项:MyStatefulWidget(),
),
),
);
}
}
类MyStatefulWidget扩展了StatefulWidget{
MyStatefulWidget({Key}):超级(Key:Key);
@凌驾
_MyStatefulWidgetState createState()=>\u MyStatefulWidgetState();
}
类_MyStatefulWidgetState扩展状态{
最终禁用项=[“自由”,“四];
字符串dropdownValue='One';
@凌驾
小部件构建(构建上下文){
返回下拉按钮(
value:dropdownValue,
图标:图标(图标。向下箭头),
iconSize:24,
海拔:16,
样式:TextStyle(颜色:Colors.deepPurple),
下划线:容器(
身高:2,
颜色:颜色。深紫色,
),
onChanged:(字符串newValue){
如果(!disabledItems.contains(newValue)){
设置状态(){
dropdownValue=newValue;
});
}
},
物品:[“一”、“二”、“免费”、“四”]
.map((字符串值){
返回下拉菜单项(
价值:价值,
子:文本(
价值
样式:TextStyle(
颜色:disabledItems.包含(值)?颜色。灰色:null,
),
),
);
}).toList(),
);
}
}

稍有不同的版本:

创建所有项,但必须禁用的项具有onTap=>null&&Color=grey

您可以将此解决方案与@humazed合并,以检查元素是否包含在排除列表中


字符串selectedValue;
...
下拉按钮窗体字段(
装饰:输入装饰(
图标:常量图标(图标.类别),
labelText:“禁用的下拉菜单”),
项目:['a','b','c'].map((值){
//正常选项
如果(值!=“c”){
返回下拉菜单项(
价值:价值,
子项:新文本(值,
样式:新文本样式(颜色:Colors.black)),
);
//残疾人士(“c”)
}否则{
返回下拉菜单项(
子项:文本(值,样式:TextStyle(颜色:Colors.grey)),
onTap:()=>null,
);
}
}).toList(),
值:selectedValue,
onChanged:(字符串newValue){
设置状态(){
selectedValue=newValue;
});
},
),

将onTap()=>设置为null并禁用菜单选项不是很好吗?就像一个被禁用的按钮一样。我已经发送了一个关于Flatter的github的改进请求,但版本略有不同:

创建所有项,但必须禁用的项具有onTap=>null&&Color=grey

您可以将此解决方案与@humazed合并,以检查元素是否包含在排除列表中


字符串selectedValue;
...
下拉按钮窗体字段(
装饰:输入装饰(
图标:常量图标(Icons.cat