Home > Net >  How to open another widget by clicking checkbox?
How to open another widget by clicking checkbox?

Time:09-02

I want to create such logic: when checkbox is true, it returns widget with Image, then it false, image disappears. I have tried this code:

bool checkBoxValue = true;

Checkbox(
value: checkBoxValue,
onChanged: (bool? value) {
 setState(() {
checkBoxValue = value ?? true;
if (value == true) {
Image.network('https://imgd.aeplcdn.com/370x208/n/cw/ec/40432/scorpio-n-exterior-right-front-three-quarter-15.jpeg?isig=0&q=75')
}else if(value == false){
        return;}
});

CodePudding user response:

you may try out with below code

Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Checkbox(
                value: checkBoxValue,
                onChanged: (bool value) {
                  checkBoxValue = value;
                  setState(() {
                    
                  });
                }),
            checkBoxValue
                ? Image.network(
                    'https://imgd.aeplcdn.com/370x208/n/cw/ec/40432/scorpio-n-exterior-right-front-three-quarter-15.jpeg?isig=0&q=75')
                : Container()
          ],
        )

CodePudding user response:

You can use Visibility widget.

It has a parameter called visible and it shows the widget if visible is true.

Example:

Visibility(
  visible: checkBoxValue,
  child: _theWidgetThatYouWantToShow(), // put your widget here
),

Or, without Visibility, you can do this:

Column(
  children: [
    CheckBox(...), // your checkbox
    if (checkBoxValue)
      _theWidgetThatYouWantToShow(), // put your widget here
  ],
),
  • Related