gpt4 book ai didi

flutter - 下拉 - 项目 - 禁用?

转载 作者:行者123 更新时间:2023-12-05 00:46:25 25 4
gpt4 key购买 nike

我在 Flutter 中发现了 DropdownButton 小部件,我玩弄了这些项目。我现在的问题是,为什么在 dropdownmenuitem 小部件中没有禁用项目的选项?或者有什么办法吗?

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

最佳答案

https://dartpad.dev/d2b1688b14270bdf56ae952adfac93d2

没有官方方法可以禁用某些 DropdownMenuItem,但是您可以通过在选中时不选择禁用的项目并更改其颜色来模拟这一点。否则,您需要复制 DropdownButton 并自己添加此功能。

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

关于 flutter - 下拉 - 项目 - 禁用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60597380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com