DropdownButton - 设置值:值不包含在用户可以 select 的项目列表中 - Flutter
DropdownButton - Set value: with value NOT contained by The list of items the user can select - Flutter
我有这个下拉按钮
列表中有两项
当我 Select 值 2 时,我希望它显示一个不包含在列表中的值
像这样:
有可能吗?
看起来像这样:
DropdownButton<String>(
value: dropdownValue,
items: <String>[
'Value 1',
'Value 2',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
if (dropdownValue == 'Value 2') {
dropdownValue = 'Value Not in list of items';
}
});
},
),
如您所见,如果我设置 dropdownValue = 'Value Not in list of items';
,我当然会收到错误消息,因为它不在列表中 - 但我希望它能工作:)
另一方面,正如预期的那样有效:
if (dropdownValue == 'Value 2') {
dropdownValue = 'Value 1';
}
因为 'Value 1'
在列表中 - 但我想在单击 Value 2
时显示不在列表中的值
希望有人能帮忙! :)
[为了搜索引擎优化这里是错误]:
Assertion failed:
../…/material/dropdown.dart:880
items == null ||
items.isEmpty ||
value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length ==
1
"There should be exactly one item with [DropdownButton]'s value: Value Not in list of items. \nEither zero or 2 or more [DropdownMenuItem]s were detected with the same value"
我这样做过,
问题是我们需要确保 Item[current] == selected item.
import 'package:flutter/material.dart';
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String dropdownValue = 'Value 1';
List<String> values = [
'Value 1',
'Value 2',
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: DropdownButton<String>(
value: dropdownValue,
onChanged: (String? newValue) {
setState(
() {
if (newValue == 'Value 2') {
values[1] = 'Value Not in list of items';
dropdownValue = 'Value Not in list of items';
}
if (newValue == 'Value 1') {
dropdownValue = 'Value 1';
values[1] = 'Value 2';
}
},
);
},
items: values.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
);
}
}
如果您只想在选择 Value 2
时 显示 一些文本,您可以尝试 hint
小部件。
String hintValue = 'Select Value'; // ---- hint
var dropdownValue;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: [
ElevatedButton(child: Text('Press here'), onPressed: null), // ---- ignore
DropdownButton<String>(
value: dropdownValue,
hint: Text(hintValue), // ---- hint
isExpanded: true,
items: <String>[
'Value 1',
'Value 2',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
// ----- if Value 2 is selected
if (newValue == 'Value 2') {
hintValue = 'Value Not in list of items';
} else
hintValue = '$newValue';
});
},
),
],
));
}
输出
注意 : Documentation 用于使用 hint
小部件
我有这个下拉按钮
列表中有两项
当我 Select 值 2 时,我希望它显示一个不包含在列表中的值
像这样:
有可能吗?
看起来像这样:
DropdownButton<String>(
value: dropdownValue,
items: <String>[
'Value 1',
'Value 2',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
if (dropdownValue == 'Value 2') {
dropdownValue = 'Value Not in list of items';
}
});
},
),
如您所见,如果我设置 dropdownValue = 'Value Not in list of items';
,我当然会收到错误消息,因为它不在列表中 - 但我希望它能工作:)
另一方面,正如预期的那样有效:
if (dropdownValue == 'Value 2') {
dropdownValue = 'Value 1';
}
因为 'Value 1'
在列表中 - 但我想在单击 Value 2
希望有人能帮忙! :)
[为了搜索引擎优化这里是错误]:
Assertion failed:
../…/material/dropdown.dart:880
items == null ||
items.isEmpty ||
value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length ==
1
"There should be exactly one item with [DropdownButton]'s value: Value Not in list of items. \nEither zero or 2 or more [DropdownMenuItem]s were detected with the same value"
我这样做过,
问题是我们需要确保 Item[current] == selected item.
import 'package:flutter/material.dart';
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String dropdownValue = 'Value 1';
List<String> values = [
'Value 1',
'Value 2',
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: DropdownButton<String>(
value: dropdownValue,
onChanged: (String? newValue) {
setState(
() {
if (newValue == 'Value 2') {
values[1] = 'Value Not in list of items';
dropdownValue = 'Value Not in list of items';
}
if (newValue == 'Value 1') {
dropdownValue = 'Value 1';
values[1] = 'Value 2';
}
},
);
},
items: values.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
);
}
}
如果您只想在选择 Value 2
时 显示 一些文本,您可以尝试 hint
小部件。
String hintValue = 'Select Value'; // ---- hint
var dropdownValue;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: [
ElevatedButton(child: Text('Press here'), onPressed: null), // ---- ignore
DropdownButton<String>(
value: dropdownValue,
hint: Text(hintValue), // ---- hint
isExpanded: true,
items: <String>[
'Value 1',
'Value 2',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
// ----- if Value 2 is selected
if (newValue == 'Value 2') {
hintValue = 'Value Not in list of items';
} else
hintValue = '$newValue';
});
},
),
],
));
}
输出
注意 : Documentation 用于使用 hint
小部件