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'); }), ], ), );
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.
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 )
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 ); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With