[英]DropdownButton not showing up in second page of flutter app
我的 DropdownButton 不会显示在 flutter 应用程序的第二页上。
这是在 ConversionRates 类(第 2 页)中实现的 DropdownButton 代码:
Widget buildDropDownButton(String currencyCategory) {
return DropdownButton(
value: currencyCategory,
items: converter.currencies.keys
.map((String value) => DropdownMenuItem(
value: value,
child: Row(children: <Widget>[
Text(value),
])))
.toList(),
onChanged: (String value) {
if (currencyCategory == converter.fromCurrency) {
setState(() {
converter.onFromChanged(value);
});
} else {
setState(() {
converter.onToChanged(value);
});
}
});
这是类 ConversionRates(第 2 页)的其余部分的代码:
class ConversionRates extends StatefulWidget {
Map<String, double> currencies;
ConversionRates({Key key, @required this.currencies}) : super(key: key);
@override
ConversionRatesState createState() => ConversionRatesState(currencies);
}
class ConversionRatesState extends State<ConversionRates> {
final Converter converter = new Converter.defaultConstructor();
String _currentItemSelected = "SEK";
Map<string, double> currencies;
ConversionRatesState(this.currencies);
@override
void initState() {
super.initState();
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueAccent,
appBar: AppBar(
backgroundColor: Colors.black87,
title: Center(
child: Text(
"Conversion Rates",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
fontSize: 35.0),
)),
),
body: converter.currencies.keys == null
? Center(child: CircularProgressIndicator())
: Stack(children: [
Container(
color: Colors.black26,
alignment: Alignment.center,
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Center(
child: Stack(children: [
Positioned(
top: 40,
left: 70,
child: Container(
color: Colors.transparent,
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
ListTile(
title: TextField(),
trailing: buildDropDownButton(
converter.fromCurrency)),
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: "SEK: " +
converter.currencies["SEK"]
.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0)),
TextSpan(
text: "\n\nUSD: " +
converter.currencies["USD"]
.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0)),
TextSpan(
text: "\n\nEUR: " +
converter.currencies["EUR"]
.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0)),
TextSpan(
text: "\n\nGBP: " +
converter.currencies["GBP"]
.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0)),
TextSpan(
text: "\n\nCNY: " +
converter.currencies["CNY"]
.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0)),
TextSpan(
text: "\n\nJPY: " +
converter.currencies["JPY"]
.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0)),
TextSpan(
text: "\n\nKRW: " +
converter.currencies["KRW"]
.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0)),
],
))
])))
])))
]));
}
我从 page1 收到的数据是 map<string,double> 货币。 问题是,当我在第二页上没有下拉按钮时,我可以访问货币地图中的数据。 所以这里的数据传输不是问题。 但是,一旦我尝试制作 DropdownButton,我就无法再访问货币中的数据。 我也没有得到任何下拉按钮。 我尝试以不同的方式解决这个问题,但我最终要么没有任何显示在第二页(除了栏和脚手架),如 Textspan 或 DropdownButton 要么我结束了模拟器错误说:
NoSuchMethodError:在 null 上调用了 getter 'keys'。
所以我认为问题可能出在状态管理上,但我真的不知道如何正确管理它。 我还创建了一个实例转换器,以便我可以从 Converter 类访问一些数据。 我还尝试将货币映射更改为映射 <dynamic,dynamic> 货币,但这并没有解决问题。
有任何想法吗?
您的转换器货币列表也为空。 你需要检查一下。 它看起来像这样:
converter.currencies?.keys == null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.