使CheckBoxListTile的下拉按钮处于抖动状态



我想做一些DropdownButtons来过滤数据以显示特定的结果。我做了一个下拉按钮,并插入项目作为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!;
});
},
),
),
),
],
),
],
),
);
}
}

. .有没有办法解决这个问题,或者有什么可以帮助有多个选择选项的下拉按钮?

给你!

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

最新更新