gpt4 book ai didi

flutter - Flutter-DropdownButton onChanged返回null

转载 作者:行者123 更新时间:2023-12-03 03:31:03 41 4
gpt4 key购买 nike

我正在创建一个下拉列表,但是每当我单击一个值并尝试在onChanged部分中打印该值时,它都会输出null,这是我做错了什么?谢谢。这是我的代码:

  List<UserFontModel> _fonts = [
UserFontModel(fontFamily: 'Regular', fontWeight: FontWeight.w400),
UserFontModel(fontFamily: 'Bold', fontWeight: FontWeight.w700),
UserFontModel(fontFamily: 'Medium', fontWeight: FontWeight.w500),
UserFontModel(fontFamily: 'Light', fontWeight: FontWeight.w300),
UserFontModel(fontFamily: 'Thin', fontWeight: FontWeight.w100),
];

String _selectedFontStyle;

new DropdownButton<String>(
value: _selectedFontStyle,
hint: Text('Style'),
items: _fonts.map((fonts) => DropdownMenuItem<String> (
child: Container(
width: MediaQuery.of(context).size.width * 0.2,
child: Text(fonts.fontFamily, style: TextStyle(fontWeight: fonts.fontWeight),),
),
)).toList(),
onChanged: (String _) {
print(_);
},
),
当我选择一个项目时,会显示:
flutter: null
有人可以帮忙吗?

最佳答案

请试试这个。

List<UserFontModel> _fonts = [
UserFontModel(fontFamily: 'Regular', fontWeight: FontWeight.w400),
UserFontModel(fontFamily: 'Bold', fontWeight: FontWeight.w700),
UserFontModel(fontFamily: 'Medium', fontWeight: FontWeight.w500),
UserFontModel(fontFamily: 'Light', fontWeight: FontWeight.w300),
UserFontModel(fontFamily: 'Thin', fontWeight: FontWeight.w100),
];

String _selectedFontStyle;

DropdownButton<String>(
hint: Text("Style"),
value: _selectedFontStyle,
onChanged: (String Value) {
setState(() {
_selectedFontStyle = Value;
});
},
items: _fonts.map((fonts) {
return DropdownMenuItem<String>(
value: fonts.fontFamily,
child: new Container(
width: MediaQuery.of(context).size.width * 0.2,
child: Text(fonts.fontFamily, style: TextStyle(fontWeight: fonts.fontWeight),),
)
);
}).toList(),
),

关于flutter - Flutter-DropdownButton onChanged返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63845550/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com