Home > Enterprise >  How to hide a widget if a DropdownMenuItem is not selected in flutter
How to hide a widget if a DropdownMenuItem is not selected in flutter

Time:03-22

I'm trying to hide a widget that follows after dropdownMenu only if no value has been selected. Here is my dropdown menu

  Container(
       
               child:  DropdownButtonFormField(
          decoration: InputDecoration(
            
              filled: true,
              hintStyle: TextStyle(color: Colors.white),
              fillColor: Color.fromARGB(0, 0, 0, 0)
            
              ),
          value: dropDownValue,
          onChanged: (String? Value) {
            setState(() {
              dropDownValue = Value!;
            });
          },
        
          items: terms
              .map((cityTitle) => DropdownMenuItem(
                  value: cityTitle, child: Text("$cityTitle")))
              .toList(),
        ),  

And this is how i've tried , but this doesn't work

 dropDownValue  == "" ? 

         
              Container(
              margin: EdgeInsets.all(20),
              child: Text(''),
              
            ):   Container(
              margin: EdgeInsets.all(20),
              child: Text('testing app'),
              
            )

So, i want if no value has been selected an empty container to be displayed else the text " testing app" should be shown

CodePudding user response:

You need to declare the value outside the build function. Try to avoid "" or ? in if. Maybe try using something like string.isEmpty. You widget if is that when dropDownValue is "" then no text should be shown. When dropDownValue != "" then "testing app" should be show.

Make sure to to define a init value of the string outside the build function under the extends State like

String dropDownValue  = ""

CodePudding user response:

i got it , all that time i initialized the dropDownValue instead of living it empty.

  • Related