DropDownButton 所选项目在 UI 中没有变化
DropDownButton selected item isn't changing in UI
我要在我的 DropDownButton
中添加的对象:
class Car {
int id;
String make;
Car(this.id, this.make);
static List<Car> getCars() {
var cars = new List<Car>();
cars.add(Car(1, "Ford"));
cars.add(Car(2, "Toyota"));
cars.add(Car(3, "BMW"));
return cars;
}
}
构建 DropDown(StatefulWidget 状态 class):
class _MyHomePageState extends State<MyHomePage> {
Car _selectedCar;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(child: getDropDown()));
}
Widget getDropDown() {
var cars = Car.getCars();
this._selectedCar = cars.first; // Default to first value in list.
var items = cars.map((car) {
return new DropdownMenuItem<Car>(
value: car,
child: new Text(car.make),
);
}).toList();
return DropdownButton<Car>(
value: this._selectedCar,
onChanged: (Car car) {
setState(() {
this._selectedCar = car;
});
},
items: items);
}
}
DropDownButton
正确显示第一项 selected,但是当我 select 另一个项目时 UI 永远不会更新以显示新项目为 selected.
您只需要初始化列表一次,因为如果您在每次抽取时都初始化新列表,将不会匹配 DropDown List 值。
在此处找到工作示例:Gist
尝试在 initState()
方法而不是 getDropdown()
方法中初始化 _selectedCar
变量。
根据您发布的代码,自从调用 build()
方法以来,每次调用 setState()
时都会重新初始化 _selectedCar
变量。
您还提到在尝试第一个答案中的解决方案时出现以下错误:
I/flutter ( 5072): 'package:flutter/src/material/dropdown.dart':
Failed assertion: line 560 pos 15: 'items == null || I/flutter (
5072): items.isEmpty || value == null ||
items.where((DropdownMenuItem item) => item.value == I/flutter (
5072): value).length == 1': is not true.
这很可能是因为您的下拉列表中有不止一项获得了相同的值。
一个可能的修复方法是使用 Car
对象的 id
参数作为下拉菜单 value
而不是整个对象,因为 id
是唯一的对于每个对象。有关此错误的更多详细信息,请参见 .
我要在我的 DropDownButton
中添加的对象:
class Car {
int id;
String make;
Car(this.id, this.make);
static List<Car> getCars() {
var cars = new List<Car>();
cars.add(Car(1, "Ford"));
cars.add(Car(2, "Toyota"));
cars.add(Car(3, "BMW"));
return cars;
}
}
构建 DropDown(StatefulWidget 状态 class):
class _MyHomePageState extends State<MyHomePage> {
Car _selectedCar;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(child: getDropDown()));
}
Widget getDropDown() {
var cars = Car.getCars();
this._selectedCar = cars.first; // Default to first value in list.
var items = cars.map((car) {
return new DropdownMenuItem<Car>(
value: car,
child: new Text(car.make),
);
}).toList();
return DropdownButton<Car>(
value: this._selectedCar,
onChanged: (Car car) {
setState(() {
this._selectedCar = car;
});
},
items: items);
}
}
DropDownButton
正确显示第一项 selected,但是当我 select 另一个项目时 UI 永远不会更新以显示新项目为 selected.
您只需要初始化列表一次,因为如果您在每次抽取时都初始化新列表,将不会匹配 DropDown List 值。
在此处找到工作示例:Gist
尝试在 initState()
方法而不是 getDropdown()
方法中初始化 _selectedCar
变量。
根据您发布的代码,自从调用 build()
方法以来,每次调用 setState()
时都会重新初始化 _selectedCar
变量。
您还提到在尝试第一个答案中的解决方案时出现以下错误:
I/flutter ( 5072): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 560 pos 15: 'items == null || I/flutter ( 5072): items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == I/flutter ( 5072): value).length == 1': is not true.
这很可能是因为您的下拉列表中有不止一项获得了相同的值。
一个可能的修复方法是使用 Car
对象的 id
参数作为下拉菜单 value
而不是整个对象,因为 id
是唯一的对于每个对象。有关此错误的更多详细信息,请参见