How to Implement a Checkbox in Flutter: Complete Material Design Guide
To implement a checkbox in Flutter, use the Checkbox widget from the Material library by setting the value property to a boolean and providing an onChanged callback to update the state.
The Checkbox widget is a core Material Design component in the flutter/flutter repository that allows users to toggle boolean values through an interactive square control. Located in packages/flutter/lib/src/material/checkbox.dart, this lightweight StatefulWidget renders a selectable box displaying a check mark when active and integrates seamlessly with Flutter's theming system according to the source code.
Basic Checkbox Implementation
To create a functional checkbox, instantiate the Checkbox widget with a boolean value and an onChanged callback. The widget requires a StatefulWidget parent to manage the checked state, as the checkbox itself holds only immutable configuration and relies on external state management.
import 'package:flutter/material.dart';
class MyCheckboxDemo extends StatefulWidget {
const MyCheckboxDemo({Key? key}) : super(key: key);
@override
State<MyCheckboxDemo> createState() => _MyCheckboxDemoState();
}
class _MyCheckboxDemoState extends State<MyCheckboxDemo> {
bool _isChecked = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Checkbox Example')),
body: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Checkbox(
value: _isChecked,
onChanged: (bool? newValue) {
setState(() => _isChecked = newValue!);
},
activeColor: Colors.green,
checkColor: Colors.white,
),
const Text('Accept terms and conditions'),
],
),
),
);
}
}
Adding Labels with CheckboxListTile
When you need a checkbox with descriptive text, padding, and standard Material Design list layout, use CheckboxListTile. This composite widget, defined in packages/flutter/lib/src/material/checkbox_list_tile.dart, combines a Checkbox with a title, optional subtitle, and secondary widget while maintaining proper touch targets and alignment.
class CheckboxListTileDemo extends StatefulWidget {
const CheckboxListTileDemo({Key? key}) : super(key: key);
@override
State<CheckboxListTileDemo> createState() => _CheckboxListTileDemoState();
}
class _CheckboxListTileDemoState extends State<CheckboxListTileDemo> {
bool _selected = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('CheckboxListTile Example')),
body: ListView(
children: [
CheckboxListTile(
title: const Text('Enable notifications'),
value: _selected,
onChanged: (bool? newValue) {
setState(() => _selected = newValue!);
},
secondary: const Icon(Icons.notifications),
activeColor: Theme.of(context).colorScheme.secondary,
),
],
),
);
}
}
Advanced Configuration Options
Tri-State Support
Set the tristate property to true to enable a third indeterminate state represented by null. When tristate is enabled, the checkbox displays a horizontal dash instead of a check mark when the value is null, cycling through false → true → null on successive taps.
bool? _triState = null;
Checkbox(
tristate: true,
value: _triState,
onChanged: (bool? newValue) {
setState(() => _triState = newValue);
},
);
Custom Colors and Theming
The Checkbox widget pulls default colors from the current ThemeData when custom values are not specified. Specifically, it uses Theme.of(context).unselectedWidgetColor for the empty box and Theme.of(context).colorScheme.secondary for the filled state. Override these defaults using the activeColor parameter for the filled box background and checkColor for the check mark itself.
Internal Architecture and Rendering
The checkbox implementation follows Flutter's layered architecture with clear separation between configuration and rendering. In packages/flutter/lib/src/material/checkbox.dart, the public Checkbox class serves as an immutable configuration container holding properties like value, onChanged, activeColor, and tristate.
The accompanying private _CheckboxState class manages the widget's lifecycle and creates a _RenderCheckbox render object that handles the actual painting of the box and check mark. User interactions flow through an InkWell widget that provides the Material ripple effect and manages focus, hover states, and accessibility through the FocusNode and mouseCursor properties.
Summary
- Import the Material library and use the
Checkboxwidget located inpackages/flutter/lib/src/material/checkbox.dartfor standard boolean toggles. - Wrap checkboxes in
StatefulWidgetimplementations and update state within theonChangedcallback to reflect user interactions. - Use
CheckboxListTilefromcheckbox_list_tile.dartwhen you need integrated labels, subtitles, and list-appropriate padding. - Enable
tristate: trueto support indeterminate states represented bynullvalues and horizontal dash indicators. - Customize appearance with
activeColorandcheckColor, or rely onThemeDatadefaults from the current context.
Frequently Asked Questions
How do I change the checkbox color in Flutter?
Set the activeColor property to change the background color of the checked box, and use checkColor to modify the color of the check mark icon. If these properties are null, the widget defaults to Theme.of(context).colorScheme.secondary for the active background and calculates an appropriate contrast color for the check mark.
What is the difference between Checkbox and CheckboxListTile?
Checkbox is a bare control widget that renders only the square check box, requiring manual layout with other widgets like Text or Row. CheckboxListTile is a higher-level composite widget that combines a checkbox with a title, optional subtitle, and secondary widget in a preconfigured list tile layout with proper padding and touch targets.
Can a Flutter checkbox have three states?
Yes, set the tristate parameter to true to enable support for true, false, and null values. In the null state, the checkbox displays a horizontal dash instead of a check mark, commonly used to indicate a mixed or indeterminate selection state in parent-child checkbox hierarchies.
Where is the Checkbox widget defined in the Flutter source code?
The Checkbox widget is defined in packages/flutter/lib/src/material/checkbox.dart within the Flutter SDK. The related CheckboxListTile widget resides in packages/flutter/lib/src/material/checkbox_list_tile.dart, and both are exported through the main Material library.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →