gpt4 book ai didi

checkbox - flutter :复选框 onChanged

转载 作者:IT王子 更新时间:2023-10-29 07:00:17 25 4
gpt4 key购买 nike

我试图实现一个可以禁用和启用按钮的复选框。但是我被以下错误阻止了:

I/flutter (19999): The following assertion was thrown building DisclaimerPage(dirty, state: DisclaimerPageState#24b3d):
I/flutter (19999): setState() or markNeedsBuild() called during build.
I/flutter (19999): This Overlay widget cannot be marked as needing to build because the framework is already in the
I/flutter (19999): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (19999): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (19999): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (19999): Otherwise, the framework might not visit this widget during this build phase.

这是我构建页面的代码:

class DisclaimerPageState extends State<DisclaimerPage> {
bool checkBoxValue = false;

@override
Widget build(BuildContext context) {
var _floatButtonOnPressed;

if (checkBoxValue) {
_floatButtonOnPressed = navToHome();
}

return new Scaffold(
appBar: AppBar(
title: Text("Disclaimer"),
leading: Container(),
),
body: Container(
child: Card(
child: Column(
children: <Widget>[
Text("ADHJASDKHKDAD"),
Row(
children: <Widget>[
Checkbox(
value: checkBoxValue,
onChanged: (bool newValue) {
setState(() {
checkBoxValue = newValue;
});
}),
Text("I understand"),
],
),
],
),
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _floatButtonOnPressed,
label: Text("PROCEED"),
icon: Container(),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}

navToHome() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImmunityHomePage(title: 'ImMUnity')),
);
}
}

我做错了什么吗?我的复选框实现基于 checkbox.dart 的文档。所以我在这里有点困惑。感谢帮助。非常感谢!

最佳答案

而不是检查 checkBoxValue 和设置 onPressed,你应该在你的 on press 函数中检查 checkBoxValue 然后管理导航。所以首先删除这个

if (checkBoxValue) {
_floatButtonOnPressed = navToHome();
}

然后更新 float Action 按钮的onPressed为

onPressed: (){navToHome();},

然后更新你的navToHome()函数

navtoHome(){
if(checkValue){
//your navigation code here
}
}

关于checkbox - flutter :复选框 onChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52240431/

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