颤动:checkBox onChanged

问题描述 投票:1回答:1

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

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的文档。所以我在这里有点困惑。感谢帮助。非常感谢!

checkbox dart flutter
1个回答
1
投票

您应该检查on press功能中的checkBoxValue,然后管理导航,而不是检查checkBoxValue并设置onPressed。首先删除它

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

然后将onPressed of floating action按钮更新为

onPressed: (){navToHome();},

然后更新你的navToHome()功能

navtoHome(){
  if(checkValue){
    //your navigation code here
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.