【发布时间】:2020-06-30 07:56:54
【问题描述】:
这适用于 DropdownButtonHideUnderline,但不适用于 DropdownButtonFormField。我想要使用 DropdownButtonFormField 获得的 inputDecoration,但是当我更改项目时,此代码在运行时失败。
我要么需要修复它以使用 DropdownButtonFormField 运行,要么我应该找到一种方法将 inputDecoration 添加到 DropdownButtonHideUnderline;
在运行时出现的错误是:
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
Map data = {
'Project 1': ['Entrance', 'Main Hallway', 'Kitchen'],
'Project 2': ['Patio', 'Dining Room'],
};
class _MyHomePageState extends State<MyHomePage> {
String _project;
String _room;
List<String> _roomList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[
DropdownButtonFormField(
decoration: InputDecoration(labelText: 'Project'),
value: _project,
onChanged: (value) {
setState(() {
_project = value;
_room = null;
_roomList = data[_project];
});
},
items: data.keys.map((item) {
return DropdownMenuItem(
child: Text(item),
value: item,
);
})?.toList() ??
[],
),
DropdownButtonFormField(
decoration: InputDecoration(labelText: 'Room'),
value: _room,
onChanged: (value) {
setState(() {
_room = value;
print(_project);
print(_room);
});
},
items: _roomList.map((item) {
return DropdownMenuItem(
child: Text(item),
value: item,
);
})?.toList() ??
[],
),
],
),
));
}
}
【问题讨论】: