我有以下代码。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SimpleScreen());
}
}
class SimpleScreen extends StatefulWidget {
@override
_SimpleScreenState createState() => _SimpleScreenState();
}
class _SimpleScreenState extends State<SimpleScreen> {
ItemCls currentValue = ItemCls(name: 'one', price: 1);
List<DropdownMenuItem> _menuItems = <DropdownMenuItem>[
DropdownMenuItem(
child: new Container(
child: new Text("Item#1"),
width: 200.0,
),
value: ItemCls(name: 'one', price: 1)),
DropdownMenuItem(
child: new Container(
child: new Text("Item#2"),
width: 200.0,
),
value: ItemCls(name: 'two', price: 2))
];
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Center(
child: DropdownButton(
value: currentValue,
items: _menuItems,
onChanged: onChanged,
style: Theme.of(context).textTheme.title,
),
));
}
void onChanged(value) {
setState(() {
currentValue = value;
});
// print(value);
}
}
class ItemCls {
final String name;
final double price;
const ItemCls({
@required this.name,
@required this.price,
}) : assert(name != null),
assert(price != null);
}
失败与
最佳答案
原因
实例ItemCls currentValue = ItemCls(name: 'one', price: 1);
不等于value: ItemCls(name: 'one', price: 1)),
他们有不同的地址,并视为不同的值(value)
所以DropdownButton认为选择列表中不存在默认值ItemCls(name: 'one', price: 1)
解决方案
使用List<ItemCls>
包含您的选择,并将currentValue
指向List的第一个值
ItemCls currentValue;
static List<ItemCls> itemList = [
ItemCls(name: 'one', price: 1),
ItemCls(name: 'two', price: 2)
];
...
DropdownMenuItem<ItemCls>(
child: new Container(
child: new Text("Item#1"),
width: 200.0,
),
value: itemList[0]),
...
void initState() {
// TODO: implement initState
currentValue = itemList[0];
}
工作演示
完整的代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SimpleScreen());
}
}
class SimpleScreen extends StatefulWidget {
@override
_SimpleScreenState createState() => _SimpleScreenState();
}
class _SimpleScreenState extends State<SimpleScreen> {
ItemCls currentValue;
static List<ItemCls> itemList = [
ItemCls(name: 'one', price: 1),
ItemCls(name: 'two', price: 2)
];
List<DropdownMenuItem<ItemCls>> _menuItems = <DropdownMenuItem<ItemCls>>[
DropdownMenuItem<ItemCls>(
child: new Container(
child: new Text("Item#1"),
width: 200.0,
),
value: itemList[0]),
DropdownMenuItem<ItemCls>(
child: new Container(
child: new Text("Item#2"),
width: 200.0,
),
value: itemList[1])
];
@override
void initState() {
// TODO: implement initState
currentValue = itemList[0];
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Center(
child: DropdownButton<ItemCls>(
value: currentValue,
items: _menuItems,
onChanged: onChanged,
style: Theme.of(context).textTheme.title,
),
));
}
void onChanged(value) {
setState(() {
currentValue = value;
});
// print(value);
}
}
class ItemCls {
final String name;
final double price;
const ItemCls({
@required this.name,
@required this.price,
}) : assert(name != null),
assert(price != null);
}
关于flutter - 当类用作值而不是字符串时,flutter下拉列表失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59278640/