Checkbox(
onChanged: (bool value) {
setState(() {
isHighBloodDisease = value;
});
},
value: isHighBloodDisease,
),
这是我的代码,当我单击复选框时,检查不是 appper 但是当我关闭 dropDownList 并重新打开它时,我选择的框被选中,我尝试打印该值,当我单击它时它可以工作打印 true
return SizedBox(
height: 50,
width: 350,
child: DropdownButtonFormField(
iconSize: 50,
iconEnabledColor: white,
decoration: InputDecoration(
hintText: 'الامراض المزمنة',
hintStyle: TextStyle(
fontSize: 23, fontWeight: FontWeight.bold, color: white),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: white,
),
borderRadius: BorderRadius.circular(30.0),
),
),
items: [
DropdownMenuItem(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(
chronicDisease[0]['disease'],
style: textStyle,
),
Checkbox(
value: isHeartDisease,
onChanged: (bool value) {
setState(() {
isHeartDisease = value;
});
},
),
],
),
),
DropdownMenuItem(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(
chronicDisease[4]['disease'],
style: textStyle,
),
Checkbox(
onChanged: (bool value) {
setState(() {
isHighBloodDisease = value;
});
},
value: isHighBloodDisease,
),
],
),
)
].toList(),
onChanged: (value) {},
),
);
}
}
答案 0 :(得分:0)
试试下面的代码片段:
<块引用>为复选框创建一个 statefullwidget:
class CheckboxSelectorPage extends StatefulWidget {
bool isChecked = false;
CheckboxSelectorPage(this.isChecked, {Key key}) : super(key: key);
@override
_CheckboxSelectorPageState createState() => _CheckboxSelectorPageState();
}
class _CheckboxSelectorPageState extends State<CheckboxSelectorPage> {
@override
Widget build(BuildContext context) {
return Checkbox(
onChanged: (bool value) {
widget.isChecked = value;
print("value: $value Is mounted: $mounted");
setState(() {
widget.isChecked = value;
});
},
value: widget.isChecked,
);
}
}
<块引用>
将 Checkbox
小部件替换为:
CheckboxSelectorPage(),
此示例已在 dartpad.dev
中测试答案 1 :(得分:0)
回复您的评论:
<块引用>我有另一个问题,我正在开发一个阿拉伯语应用程序,所以我想要下侧的下拉箭头,我该如何更改对齐方式?
用 DropdownButtonFormField
小部件包裹 Directionality
:
SizedBox(
height: 50,
width: 350,
child: Directionality(
textDirection: TextDirection.rtl,
child: DropdownButtonFormField(
iconSize: 50,
decoration: InputDecoration(
hintText: 'الامراض المزمنة',
有关更多信息,请查看此问题: right-to-left (RTL) in flutter