flutter - 在 AlertDialog 中获取复选框
问题描述
我添加了一个AlertDialog
,其中 aCheckbox
是,但如果我点击Checkbox
,它不会得到一个钩子。我还在Checkbox
下面添加了另一个AlertDialog
,单击它会得到一个钩子。我觉得这有setState()
关系,但我不知道。有人知道解决方案吗?提前致谢
ListTile(
title: Text("Test"),
trailing: Icon(Icons.fitness_center),
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Test"),
content: Column(
children: <Widget>[
Row(
children: <Widget>[
Checkbox(
value: checkBoxValueTheraband,
onChanged: (bool value) {
setState(() {
checkBoxValueTheraband = value;
exerciseChooser();
});
},
),
Text("Theraband"),
],
),),);});})
解决方案
您在showDialog中使用的setState不是它“拥有”的,这意味着它不会重建其中的任何内容,并且实际上会更新“拥有”它的父级的状态。相反,你给它自己的,它有自己的StateSetter setState作为参数。现在,当使用setState时,它将调用构建器并更改此小部件中任何内容的状态。StatefulBuilder
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Column(
children: <Widget>[
Row(
children: <Widget>[
Checkbox(
value: checkBoxValueTheraband,
onChanged: (bool value) {
setState(() {
checkBoxValueTheraband = value;
exerciseChooser();
});
},
),
Text("Theraband"),
]),
]);
}
)
推荐阅读
- php - Wordpress 将 preg_replace 更新为 preg_replace_callback
- docker - 为什么我无法使用 docker swarm 的主 IP 地址从其他节点访问容器?
- websphere-liberty - Open liberty 中是否有等效的 Web 容器自定义属性 com.ibm.ws.webcontainer.suppresserrorpageodrheader="true"?
- javascript - 将 id_parent 添加到 javascript 或 typescript 中的嵌套树数组
- android - Android Studio 每次在 Mac 上都无法更新
- swift - 如何使来自 iOS 应用程序的 MP4 录制视频可在 Chrome 上播放?
- powershell - Powershell regex -replace 匹配的频率比它应该更频繁
- javascript - RegEx 用于电子邮件验证,其中 @ 之前至少需要 2 个字符
- r - ggplot颜色条的标签结束,即使没有数据具有该值
- android - 将 Android kotlin 版本升级到 1.5.0 在构建时抛出错误消息