Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement CheckBox in Flutter?

Here I have mentioned my code of checkbox. I am new to flutter, So I have to implement it for Remember me functionality.

Code:

 Container(  padding: EdgeInsets.all(10.0),   child: Column(     children: <Widget>[      new Checkbox(value: checkBoxValue,           activeColor: Colors.green,           onChanged:(bool newValue){         setState(() {           checkBoxValue = newValue;         });        Text('Remember me');           }),     ],   ), ); 
like image 231
Dharam Dutt Mishra Avatar asked Oct 15 '18 09:10

Dharam Dutt Mishra


People also ask

How do you implement check boxes in flutter?

Checkbox in flutter is a material design widget. It is always used in the Stateful Widget as it does not maintain a state of its own. We can use its onChanged property to interact or modify other widgets in the flutter app.


2 Answers

If you need a Checkbox with a label then you can use a CheckboxListTile:

CheckboxListTile(   title: Text("title text"),   value: checkedValue,   onChanged: (newValue) {     setState(() {       checkedValue = newValue;     });   },   controlAffinity: ListTileControlAffinity.leading,  //  <-- leading Checkbox ) 
like image 124
Nikhat Shaikh Avatar answered Oct 02 '22 16:10

Nikhat Shaikh


I'm not sure I understand your problem correctly, but if it was how to bind functionality to the Checkbox, this State of a StatefulWidget should serve as a minimal working example for you:

class _MyWidgetState extends State<MyWidget> {   bool rememberMe = false;    void _onRememberMeChanged(bool newValue) => setState(() {     rememberMe = newValue;      if (rememberMe) {       // TODO: Here goes your functionality that remembers the user.     } else {       // TODO: Forget the user     }   });    @override   Widget build(BuildContext context) {     return Checkbox(       value: rememberMe,       onChanged: _onRememberMeChanged     );   } } 
like image 27
Marcel Avatar answered Oct 02 '22 17:10

Marcel