Home > Enterprise >  How to enable and disable a textfield by checking and unchecking a Radio button in flutter
How to enable and disable a textfield by checking and unchecking a Radio button in flutter

Time:11-01

i want to enable and disable a textfield when i click on a radio button in flutter. so when the user enabales the radiobutton the text field is enable and vise versa. `

ListTile(
                            title: const Text('Per Kilometer Policy'),
                            leading: Radio<SingingCharacter>(
                              value: SingingCharacter.unchecked,
                              groupValue: _character,
                              fillColor: MaterialStateColor.resolveWith(
                                  (states) => Colors.black),
                              onChanged: (SingingCharacter? isKiloChecked) {
                                setState(() {
                                  _character = isKiloChecked;
                                });
                              },
                            ),
                          TextFormField(
                    enabled: _kilometerButtonDisable,
                    onSaved: (Value) => print(kiloMeter),
                    decoration: InputDecoration(
                      hintStyle: TextStyle(
                        fontFamily: "Proxima Nova",
                        fontWeight: FontWeight.w300,
                      ),
                      border: InputBorder.none,
                      labelStyle: TextStyle(
                        color: Color(0xffFAFAFA),
                      ),
                    ),
                    inputFormatters: [
                      FilteringTextInputFormatter.allow(RegExp(r"[0-9] |\s"))
                    ],
                    controller: kiloMeter,
                    validator: (value) {
                      if (value != null && value.isEmpty || value != 1000) {
                        return 'Please enter your Kilometer';
                      }
                      return null;
                    },
                  ),



`

CodePudding user response:

return Column(
  children: <Widget>[
    Radio(
      value: 1,
      groupValue: _radioValue,
      onChanged: (value) {
        setState(() {
          _radioValue = value;
        });
      },
    ),
    TextField(
      enabled: _radioValue == 1 ? true : false,
    ),
  ],
);

CodePudding user response:

you can do as follows

TextFormField(
     enabled: _character==valueToEnable,
     ...
)

Where in this expression _character==valueToEnable you can replace the value or the values to enable the checkbox (null, list of values, etc)

CodePudding user response:

You can wrap your TextFormField inside AbsorbPointer and handle absorb property like:

bool _disable = false; // set this to false initially

AbsorbPointer(
  absorbing: _disable, // true makes TextFormField disabled and vice-versa
  child: TextFormField(),
)

for further assistance check out : checkout this

  • Related