Reputation: 739
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');
}),
],
),
);
Upvotes: 63
Views: 223711
Reputation: 1315
you can try this code it works perfectly
Main.dart
import 'package:flutter/material.dart';
import 'checkbox_in_listview_task-7.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
//home: MyHomePage(title: 'Flutter Demo Home Page'),
home: CheckBoxInListview(),
); } }
checkbox_in_listview_task-7.dart
import 'package:flutter/material.dart';
class GetCheckValue extends StatefulWidget {
@override
GetCheckValueState createState() {
return new GetCheckValueState();
}
}
class GetCheckValueState extends State<GetCheckValue> {
bool _isChecked = true;
String _currText = '';
List<String> text = ["InduceSmile.com", "Flutter.io", "google.com"];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Get check Value Example"),
),
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: Text(_currText,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
)),
),
),
Expanded(
child: Container(
height: 350.0,
child: Column(
children: text
.map((t) => CheckboxListTile(
title: Text(t),
value: _isChecked,
onChanged: (val) {
setState(() {
_isChecked = val;
if (val == true) {
_currText = t;
}
});
},
))
.toList(),
),
)),
],
),
);
}
}
It would give you output something like this
Upvotes: 12
Reputation: 11219
I think many answers missed the fact that the checkbox will update the whole UI on setState which is most likely what we don't want. To avoid this either 1/ use provider or riverpod package or 2/ encapsulate the checkbox into another stateful widget with a callback, see following example:
In the parent class:
@override
Widget build(BuildContext context) {
...
CheckboxWidget(callback: (value) => _rememberPassword = value),
...
}
...
Along with the following child widget:
import 'package:flutter/material.dart';
class CheckboxWidget extends StatefulWidget {
final Function(bool) callback;
const CheckboxWidget({Key? key, required this.callback}) : super(key: key);
@override
_CheckboxWidgetState createState() => _CheckboxWidgetState();
}
class _CheckboxWidgetState extends State<CheckboxWidget> {
bool _checkbox = true;
@override
Widget build(BuildContext context) {
return CheckboxListTile(
title: const Text('Remember Password', style: TextStyle(color: Colors.grey),),
value: _checkbox,
onChanged: (value) {
widget.callback(value!);
setState(() => _checkbox = !_checkbox);},
);
}
}
Upvotes: 2
Reputation: 3685
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
)
Upvotes: 106
Reputation: 9569
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
);
}
}
Upvotes: 33