Flutter 颤振-下拉按钮一经更改返回空值
我正在创建一个下拉列表,但每当我单击一个值并尝试在onChanged部分中打印它时,它会打印空值,我做错了什么?谢谢这是我的密码:Flutter 颤振-下拉按钮一经更改返回空值,flutter,dart,Flutter,Dart,我正在创建一个下拉列表,但每当我单击一个值并尝试在onChanged部分中打印它时,它会打印空值,我做错了什么?谢谢这是我的密码: List<UserFontModel> _fonts = [ UserFontModel(fontFamily: 'Regular', fontWeight: FontWeight.w400), UserFontModel(fontFamily: 'Bold', fontWeight: FontWeight.w700), Us
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(_);
},
),
有人能帮忙吗?DropdownMenuItem返回它的值字段。因此,您需要使用child来增加价值 像这样试试
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),),
),
value: fonts.fontFamily, // add this line
)).toList(),
onChanged: (String _) {
print(_);
},
),
新建下拉按钮(
值:_selectedFontStyle,
提示:文本('Style'),
项目:_font.map((字体)=>DropdownMenuItem(
子:容器(
宽度:MediaQuery.of(context).size.width*0.2,
子项:文本(fonts.fontFamily,样式:TextStyle(fontWeight:fonts.fontWeight)),
),
值:fonts.fontFamily,//添加此行
)).toList(),
onChanged:(字符串){
印刷品();;
},
),
这对我很有用:
new DropdownButton<UserFontModel>(
value: _fonts[0],
hint: Text('Style'),
items: _fonts
.map<DropdownMenuItem<UserFontModel>>((UserFontModel font) {
return DropdownMenuItem<UserFontModel>(
value: font,
child: Text(
font.fontFamily,
style: TextStyle(fontWeight: font.fontWeight),
),
);
}).toList(),
onChanged: (UserFontModel _) {
print(_.fontFamily);
},
),
新建下拉按钮(
值:_字体[0],
提示:文本('Style'),
项目:_字体
.map((UserFontModel字体){
返回下拉菜单项(
值:字体,
子:文本(
font.fontFamily,
样式:TextStyle(fontWeight:font.fontWeight),
),
);
}).toList(),
一旦更改:(UserFontModel){
打印(字体系列);
},
),
请试试这个
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(),
),
List\u字体=[
UserFontModel(fontFamily:“常规”,fontWeight:fontWeight.w400),
UserFontModel(fontFamily:'Bold',fontWeight:fontWeight.w700),
UserFontModel(fontFamily:“中等”,fontWeight:fontWeight.w500),
UserFontModel(fontFamily:'Light',fontWeight:fontWeight.w300),
UserFontModel(fontFamily:'Thin',fontWeight:fontWeight.w100),
];
字符串_selectedFontStyle;
下拉按钮(
提示:文本(“样式”),
值:_selectedFontStyle,
onChanged:(字符串值){
设置状态(){
_selectedFontStyle=值;
});
},
项目:_font.map((字体){
返回下拉菜单项(
值:fonts.fontFamily,
子容器:新容器(
宽度:MediaQuery.of(context).size.width*0.2,
子项:文本(fonts.fontFamily,样式:TextStyle(fontWeight:fonts.fontWeight)),
)
);
}).toList(),
),
非常感谢,这是帮助我的工具,不客气:)
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(),
),