android - 在颤动中制作 CheckBoxListTile 的 DropDownButton
问题描述
我想制作一些过滤数据以显示特定结果的下拉按钮。我制作了 DropdownButton 并将项目插入为 DropdownMenuItem,它的子项是 CheckboxListTile ..
但不幸的是,它向我显示了这个错误
There should be exactly one item with [DropdownButton]'s value: checked.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 882 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
这是我的代码:
import 'package:flutter/material.dart';
class SendingMessages extends StatefulWidget {
const SendingMessages({Key? key}) : super(key: key);
@override
_SendingMessagesState createState() => _SendingMessagesState();
}
class _SendingMessagesState extends State<SendingMessages> {
String checkBoxDropped = 'Azaz';
bool isChecked = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'إرسال إشعارات',
style: TextStyle(fontSize: 24.0),
),
centerTitle: true,
),
body: ListView(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(padding: EdgeInsets.only(top: 20.0)),
Container(
child: Center(
child: DropdownButton<String>(
icon: const Icon(Icons.arrow_circle_down_sharp),
items: [
DropdownMenuItem(
child: CheckboxListTile(
value: isChecked,
onChanged: (newValue) {
isChecked = newValue!;
},
),
value: 'Azaz',
),
DropdownMenuItem(
child: CheckboxListTile(
value: isChecked,
onChanged: (newValue) {
isChecked = newValue!;
},
),
value: 'Sarmada',
),
],
value: checkBoxDropped,
onChanged: (value) {
setState(() {
checkBoxDropped = value!;
});
},
),
),
),
],
),
],
),
);
}
}
所以.. 有没有办法解决这个问题,或者任何可以帮助具有多个选择选项的 DropDownButton 的东西?
解决方案
给你!,这应该可以解决问题,
class SendingMessages extends StatefulWidget {
const SendingMessages({Key? key}) : super(key: key);
@override
_SendingMessagesState createState() => _SendingMessagesState();
}
class _SendingMessagesState extends State<SendingMessages> {
List<String> items = ['Azaz', 'Sarmada'];
String? _selectedItem;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'إرسال إشعارات',
style: TextStyle(fontSize: 24.0),
),
centerTitle: true,
),
body: ListView(children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(padding: EdgeInsets.only(top: 20.0)),
Container(
child: Center(
child: DropdownButton<String>(
icon: const Icon(Icons.arrow_circle_down_sharp),
value: _selectedItem,
items:
items.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Container(
width: 100,
child: Text(
value,
style: TextStyle(
fontSize: 15,
),
)),
);
}).toList(),
onChanged: (String? value) {
if (value != null) {
setState(() {
_selectedItem = value;
});
}
}),
))
])
]));
}
}
推荐阅读
- sql - 将多个 CSV 文件插入到一个 oracle 数据库中
- java - 为什么我的 android 程序在 4.4.3 版本上运行良好但在 10.0 上运行不正常
- json - Helm3 - 将 json 文件读入 configmap 会产生一个字符串?
- python - KeyError:'未找到蜘蛛:蜘蛛'
- windows-installer - 安装程序升级将错误的文件日期应用于需要重新启动的保留文件
- java - 从 Scala 代码调用 java 泛型 Java 方法时类型不匹配
- git - `git branch --contains` 不显示发起提交的分支
- reactjs - 打印带有数组值的卡片
- python - 保存 sklearn 管道的中间结果
- swift - SwiftUI - 导航链接 - 第二次不起作用