我正在尝试在 Flutter 中创建一个下拉按钮。我从我的数据库中获取一个列表,然后我将列表传递给我的dropdownButton 所有工作,数据按预期显示,但是当我从中选择一个元素时,我收到此错误:
There should be exactly one item with [DropdownButton]'s value: Instance of 'Tag'.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 805 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
Run Code Online (Sandbox Code Playgroud)
我尝试将DropdownButton 值设置为 null它可以工作,但随后我看不到所选元素。
这是我的代码:
FutureBuilder<List<Tag>>(
future: _tagDatabaseHelper.getTagList(),
builder: …Run Code Online (Sandbox Code Playgroud) dart drop-down-menu flutter dropdownbutton flutter-futurebuilder
DropdownButton Value does not update even after selecting different items.
Run Code Online (Sandbox Code Playgroud)
如果默认值为空,则显示错误消息,如果我传递任何默认值(非空),则它永远不会更改为其他选定值。
currentCategory 设置为 DropdownButton 的默认值。
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('categories')
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
currentCategory = snapshot.data.documents[0];
return DropdownButtonHideUnderline(
child:
new DropdownButtonFormField<DocumentSnapshot>(
value: currentCategory,
onChanged: (DocumentSnapshot newValue) {
setState(() {
currentCategory = newValue;
});
print(currentCategory.data['name']);
},
onSaved: (DocumentSnapshot newValue) {
setState(() {
currentCategory = newValue;
});
},
items: snapshot.data.documents
.map((DocumentSnapshot document) {
return new DropdownMenuItem<DocumentSnapshot>(
value: document,
child: Text(
document.data['name'],
),
);
}).toList(),
),
);
}),
Run Code Online (Sandbox Code Playgroud)
帮助我解决这个问题。提前致谢。
flutter google-cloud-firestore dropdownbutton stream-builder
我正在处理一个颤振项目,我将一组对象(列表> 数组)从我的集团传递给流构建器。如果我打印它打印得很好的对象,但是当我尝试在 DropdownMenuItem 中映射它们时,它会抛出我提到的错误。因此,如果我在类中创建一个相同格式的虚拟数组并访问它,我不会收到错误消息。不知道我在这里错过了什么,代码如下。
StreamBuilder(
stream: _bLoc.getJsonArray,
builder: (context, snapshot) {
return snapshot.hasData
? new Container(
width: 150,
color: Theme.of(context).primaryColor,
child: new DropdownButton<String>(
items: snapshot.data.map((value) =>
new DropdownMenuItem<String>(
value: value["distance"],
child: new Text(value["distance"]),
)
).toList(),
onChanged: (_) {},
),
)
: Container();
}),
Run Code Online (Sandbox Code Playgroud)
我的json结构如下。
[
{"distance": "12km","price": "200LKR",},
{"distance": "2km","price": "100LKR",},
{"distance": "132km","price": "340LKR",}
]
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个DropdownButton可以用标题将特定项目彼此分开的项目(请参阅下图中所需的输出,标题为绿色)
就像在示例中一样,我想将这些绿色标题添加到现有的 中DropdownButton,但没有找到任何内容告诉我如何将项目以外的其他内容添加到DropdownButton
我也尝试从 with 切换DropdownButton到ListViewwithExpansionTile但我想保留DropdownButton...的行为
是否可以将这些标题添加到DropdownButton items?或者我应该尝试通过其他方式来实现它?我现在陷入困境,不知道如何继续
我想自定义DropDownButton,以便它不呈现DropdownItem. 相反,它应该在从 DropDown 选择项目之前和之后呈现我的自定义布局/小部件。简单来说,我想自定义我的DropDownButton.
谢谢,
[dart]无效的常量值.[dart]常量创建的参数必须是常量表达式.
我想制作DropdownButton,但errorText只接受常量变量.
[dart] Invalid constant value. [dart] Arguments of a constant creation must be constant expressions.
常量变量意味着我不能用其他文本替换.
也许还有其他任何方式来进行DropdownButton验证吗?
String errorGender = null;
var _inputGender = InputDecorator(
decoration: const InputDecoration(labelText: 'Gender', errorText: errorGender),
isEmpty: data['gender'] == null,
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
isDense: true,
value: data['gender'],
onChanged: (value) => setState(() => data['gender'] = value),
items: _gender.map((value) {
return DropdownMenuItem(
value: value,
child: Text(value[0].toUpperCase() + value.substring(1)),
);
}).toList()
)
)
)
);Run Code Online (Sandbox Code Playgroud) 当我触摸 DropdownButton 时,选项会显示在正确的位置,如下图所示。但是,如果我触摸最后一个 TextFormField,然后触摸 DropdownButton,选项会显示得太高,如下图所示。
这是我的代码:
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('App')),
body: ListView(
padding: const EdgeInsets.all(10),
children: <Widget>[
_customTextField(),
_customTextField(),
_customTextField(),
_customTextField(),
_customTextField(),
Padding(
padding: const EdgeInsets.all(10),
child: DropdownButton<String>(
value: '1',
isExpanded: true,
items: const <DropdownMenuItem<String>>[
DropdownMenuItem(value: '1', child: Text('1')),
DropdownMenuItem(value: '2', child: Text('2')),
DropdownMenuItem(value: '3', child: Text('3')),
],
onChanged: (value) {},
),
),
_customTextField(),
_customTextField(),
],
),
);
}
Widget _customTextField() {
return Padding( …Run Code Online (Sandbox Code Playgroud) 在以下示例中, DrowdownButton 包含带有白色文本的灰色背景(由容器框装饰定义)。因此,默认情况下,菜单项都具有白色文本。但是菜单选择列表包含白色背景,因此无法读取项目。有没有办法改变选择列表的背景?
@override
Widget build(BuildContext context) {
String dropdownValue = 'One';
return Scaffold(
body: Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(AppStyles.borderRadius),
color: Colors.grey,
),
padding: EdgeInsets.fromLTRB(8.0,0,8.0,0),
child: DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward, color: Colors.white),
iconSize: 24,
elevation: 16,
style: TextStyle(
color: Colors.white
),
underline: Container(
height: 0,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['One', 'Two', 'Three', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value, …Run Code Online (Sandbox Code Playgroud) 我创建了一个 dropdownButton 来允许用户从下拉列表中进行选择,该列表将通过 API 填充。现在我正在使用我创建的列表进行填充。
目前,该按钮正在显示我的列表中的项目,但在进行选择后,列表不会显示所选项目,但仍显示提示文本。我希望发生的是在做出选择后,下拉按钮显示所选的项目而不是提示文本。
在 onChanged 方法中,我添加了一个 setState,希望将 _selectedValue 变量更新为所选值并将其显示在 dropdownButton 中。
我还在 setState 方法中添加了一条 print 语句,它会触发并显示 value 变量中的值。
这是我的代码。
List<DropdownMenuItem<int>> listItems = [DropdownMenuItem(child: Text("2016"), value: 2016,), DropdownMenuItem(child: Text("2021"), value: 2021,)];
int _selectedValue;
body: DropdownButton(
value: _selectedValue,
items: listItems,
hint: Text("Select Year"),
onChanged: (int value){
setState(() {
_selectedValue = value;
print(value);
});
},
),
Run Code Online (Sandbox Code Playgroud) 我一直致力于提供两个下拉按钮,以便用户轻松选择汽车型号及其各自的品牌。下面是我拥有的静态列表的代码片段。任何反馈都将受到高度赞赏。
final List<String> carModel = [
'Audi',
'BMW',
'Chevrolet',
'Chrysler',
'Daihatsu',
'Ford',
'Hino',
'Honda',
'Isuzu',
'Jaguar',
'Jeep',
'Landrover',
'Lexus',
'Mazda',
'Mercedes',
'Mitsubishi',
'Nissan',
'Peugeot',
'Porsche',
'Subaru',
'Suzuki',
'Toyota',
'UD',
'Volkswagen',
'Volvo'
];
final List<String> audiMake = [
'A3',
'A4',
'A5',
'A6',
'A7',
'A8',
'Q3',
'Q5',
'Q7',
'Q8',
'R8',
'TT',
];
final List<String> bmwMake = [
'1 Series',
'2 Series',
'3 Series',
'4 Series',
'5 Series',
'6 Series',
'7 Series',
'8 Series',
'M2',
'M3',
'M4',
'M5',
'M6',
'X1',
'X2',
'X3',
'X4', …Run Code Online (Sandbox Code Playgroud)