Home > Back-end >  how to hide TextFormField based on a value of Radio Button OR DropdownButtonFormField in Flutter
how to hide TextFormField based on a value of Radio Button OR DropdownButtonFormField in Flutter

Time:02-12

I'm new to Flutter and Dart language and I'm trying so hard to learn it by myself.

So, my question is I have a DropdownButtonFormField with 2 values (private chat and phone number) :

Widget menu(){
  return DropdownButtonFormField(
    decoration: InputDecoration(
      border: OutlineInputBorder(),
      labelText: 'how to contact with the buyer?',
      labelStyle: TextStyle(
          fontSize: 16,
          color: Colors.grey,
          fontFamily: 'Almarai'
        ),
    ),
  items: <String>['phone number', 'private chat'].map((String value) {
    return DropdownMenuItem<String>(
      value: value,
      child: Text(value),
    );
  }).toList(),
  onChanged: (String newValue) {
    setState(() {
      dropdownvalue = newValue;
    });
  },
);
}

And I have this TextFormField that I want it to be hidden if the (private chat) option is selected :

 Widget phonenum(){
    return
          TextFormField( 
            maxLength: 10,
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'phone number',
              labelStyle: TextStyle(
                fontSize: 16,
                color: Colors.grey,
                fontFamily: 'Almarai'
              ),
            ),
          );
  }

How can I achieve that please? And also, how can I achieve it using RadioButton rather than DropdownButtonFormField ? Because I still didn't decide which one is more suitable with my project flow.

CodePudding user response:

You can set an if else statement in front of the textfield widget. Here for example it only shows Text "Mr ABC" if wishOnePerson is true. You can set a bool, and switch its value with setState(). When true show when false hide.

Column(
  children: <Widget>[
    Text('Good Morning'), // Always visible
    if (wishOnePerson) Text(' Mr ABC'), // Only visible if condition is true
  ],
) 

CodePudding user response:

 Widget phonenum(){

    if(dropdownvalue= 'phone number'){
        return TextFormField( 
          maxLength: 10,
          decoration: InputDecoration(
            border: OutlineInputBorder(),
            labelText: 'phone number',
            labelStyle: TextStyle(
              fontSize: 16,
              color: Colors.grey,
              fontFamily: 'Almarai'
            ),
          ),
        );
    }else{
           return Container();
    
          }
  }
  • Related