Flutter: checkBox onChanged

10,572

Instead of checking checkBoxValue & setting onPressed, you should check checkBoxValue inside your on press function then manage navigation. SO first remove this

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

Then update onPressed of floating action button as

onPressed: (){navToHome();},

Then update you navToHome() function

navtoHome(){
  if(checkValue){
    //your navigation code here
  }
}
Share:
10,572

Related videos on Youtube

nypogi
Author by

nypogi

Mobile developer

Updated on June 04, 2022

Comments

  • nypogi
    nypogi over 1 year

    I was trying to implement a checkbox that would disable and enable a button. But I was blocked by this following error:

    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.
    

    Here is my code for building the page:

    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')),
        );
      }
    }
    

    Am I doing something wrong? My checkbox implementation is based on the documentation of checkbox.dart. So I am a little confused here. Help is appreciated. Thank you so much!

  • nypogi
    nypogi about 5 years
    Hi Dhiraj! Thank you for your response. I will try this and update you. Thanks!