Skip to main content

Checkbox

checkbox button

Example

Here is an example of Checkbox widgets wrapped in Row, CheckboxListTile widgets and Checkbox widgets are wrapped in ListTiles.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

static const String _title = 'Checkbox Code Sample';


Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyStatefulWidget(),
),
);
}
}

class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);


State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool _checkboxValue = false;


Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
Checkbox(
value: _checkboxValue,
onChanged: (v) {
setState(() {
_checkboxValue = v!;
});
},
),
Text(
'Checkbox',
style: Theme.of(context).textTheme.subtitle1,
)
],
),
CheckboxListTile(
contentPadding: const EdgeInsets.all(0.0),
title: const Text("CheckboxListTile"),
value: _checkboxValue,
onChanged: (v) {
setState(() {
_checkboxValue = v!;
});
},
controlAffinity: ListTileControlAffinity.leading,
),
ListTile(
title: const Text("ListTile&Checkbox"),
leading: Checkbox(
value: _checkboxValue,
onChanged: (v) {
setState(() {
_checkboxValue = v!;
});
},
),
),
],
),
);
}
}