CodeFloat
CodeFloat

Reputation: 77

Flutter - How to enable TextFormField using Switch button

I disabled the TextFormField and I want to enable it again using the Custom Switch. Package is here and this is the code

bool isEnabled = false;

CustomSwitch(
   activeColor: Colors.greenAccent,
   value: isEnabled,
   onChanged: (value) {
      setState(() {
         isEnabled = value;
      });
   },
),


TextFormField(
   enabled: isEnabled,
   decoration: InputDecoration(labelText: 'Name'),
   
),

Do you have any idea how can I enable/disable the textformfield by using switch button? and it's okay if I use checkbox instead of switch. TIA!

Upvotes: 1

Views: 1566

Answers (1)

Maadhav Sharma
Maadhav Sharma

Reputation: 597

This code snippet is just working fine for me.

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  bool status = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Switch Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            CustomSwitch(
              activeColor: Colors.pinkAccent,
              value: status,
              onChanged: (value) {
                print("VALUE : $value");
                setState(() {
                  status = value;
                });
              },
            ),
            SizedBox(height: 12.0,),
            Text('Value : $status', style: TextStyle(
              color: Colors.black,
              fontSize: 20.0
            ),),
            SizedBox(height: 12.0,),
            TextFormField(
          enabled: status,
          decoration: InputDecoration(
              labelText: 'Name', disabledBorder: InputBorder.none),
        )
          ],
        ),
      ),
    );
  }
}

Upvotes: 2

Related Questions