Home > database >  child image in visiblity widget flutter
child image in visiblity widget flutter

Time:05-21

i aim to display an image if my variable bool is true else i don't diplay it for that i used the widget visiblity . but it does not work how can fix it ?

Visibility(
            visible: widget.isThereIcon,
            child: const Padding(
              padding: EdgeInsets.only(left: 8.0),
              child: Image.asset(
                'images/OK.png',
                height: MediaQuery.of(context).size.width * .27,
                width: MediaQuery.of(context).size.width * .27,
              ),
            ),
          ),

CodePudding user response:

Make sure widget.isThereIcon is bool variable

And set the value to true if you want to show the image and false if you want to hide it

use statefullWidget and your UI code:

       Scaffold(
          body: Column(
            children: [
              Visibility(
                visible: widget.isThereIcon,
                child: const Padding(
                  padding: EdgeInsets.only(left: 8.0),
                  child: Image.asset(
                    'images/OK.png',
                    height: MediaQuery.of(context).size.width * .27,
                    width: MediaQuery.of(context).size.width * .27,
                  ),
                ),
              ),
              TextButton(
                onPressed: () {
                  setState(() {
                    widget.isThereIcon = !widget.isThereIcon;
                    //if widget.isThereIcon == true => widget.isThereIcon = false
                    //if widget.isThereIcon == false => widget.isThereIcon = true
                  });
                },
                child: Text("show image"),
              ),
            ],
          ),
        ),

CodePudding user response:

is it stateless or statefull ? show how do you change state of visibility? probably you forgot about setState

setState((){
      widget.isThereIcon = true;
});

or

setState((){
      widget.isThereIcon = false;
});
  • Related