Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditionally change background image in flutter

in the home screen of my flutter app, I have a background image which I want to change conditionally based on the users selection of their gender.

 final _gender = [
    const DropdownMenuItem(
      child: Text('Male'),
      value: 1,
    ),
    const DropdownMenuItem(
      child: Text('Female'),
      value: 0,
    ),
  ];
  var  _genderValue =0;

and here is the DropDownButton


DropdownButton(
                dropdownColor: const Color(0xFF195190),
                value: _genderValue,
                items: _gender,style: TextStyle(color: Colors.white),
                onChanged: (val) {
                  setState(() {
                    _genderValue =val as int;
                  });
                },
              ),

And finally this is the background image:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


 Widget build(BuildContext context) {
    return  Container(
        constraints: const BoxConstraints.expand(),
    decoration: const BoxDecoration(
    image: DecorationImage(
    image: AssetImage('assets/images/FemaleN.png'),
    fit: BoxFit.fill)),
     child: Scaffold( 
.........................

Any suggestions on how to do that.

Thank you in advance

>Solution :

Your line of image: attribute will be changed based on the condition.

Like:

image: _genderValue == 0 ? AssetImage('assets/images/FemaleN.png') : AssetImage('assets/images/MaleN.png');

Whenever you will change the dropdown, the value of _genderValue variable will get change and image will be affected.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading