When developing Flutter apps, achieving platform-specific UI components enhances the user experience. One such component is the CupertinoCheckBox
, which replicates the macOS-style checkbox. This blog post demonstrates how to implement a tristate CupertinoCheckBox
with smooth interactions and customizable options.
What is a CupertinoCheckBox?
The CupertinoCheckBox
is a Flutter widget styled for macOS and iOS. It provides a simple and elegant checkbox with native-style animations, offering support for tristate values (true
, false
, and null
). This makes it versatile for scenarios requiring additional states beyond binary options.
Setting Up Your Flutter Project
Ensure your Flutter project is ready before integrating the CupertinoCheckBox
. To create a new project:
flutter create cupertino_checkbox_demo
cd cupertino_checkbox_demo
Add the cupertino_icons
package in your pubspec.yaml
:
dependencies:
cupertino_icons: ^1.0.0
flutter:
sdk: flutter
Run flutter pub get
to fetch dependencies.
Example Code: Implementing a Tristate CupertinoCheckBox
Below is an example of a CupertinoCheckBox
integrated with a tristate value. The checkbox switches between true
, false
, and null
on tap.
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class CupertinoCheckBoxDemo extends StatefulWidget { const CupertinoCheckBoxDemo({super.key}); @override _CupertinoCheckBoxDemoState createState() => _CupertinoCheckBoxDemoState(); } class _CupertinoCheckBoxDemoState extends State<CupertinoCheckBoxDemo> { bool? isChecked = true; @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: Center( child: Card( color: Colors.white, child: ListTile( onTap: () { switch (isChecked) { case null: isChecked = false; break; case true: isChecked = null; break; case false: isChecked = true; break; } setState(() {}); }, title: const Text("Choose your option"), trailing: CupertinoCheckbox( checkColor: CupertinoColors.white, tristate: true, value: isChecked, onChanged: (bool? value) { setState(() { isChecked = value; }); }, ), ), ), ), ); } }

Code Explanation
- Tristate Checkbox:
TheCupertinoCheckBox
supports a tristate value (true
,false
,null
). Settristate: true
to enable this functionality. - ListTile for Interaction:
TheListTile
widget wraps the checkbox, providing a tappable row that updates the state of the checkbox. - State Management:
TheonChanged
callback updates theisChecked
variable, while thesetState()
method refreshes the UI. - Switch Logic:
Tapping theListTile
cycles the checkbox through the three states:true
,false
, andnull
.
Features of CupertinoCheckBox
- Native Aesthetic: Matches macOS and iOS design standards.
- Tristate Support: Enables an additional state (
null
) for more complex scenarios. - Customizability: Easily integrated with other Cupertino widgets.
Use Cases for Tristate Checkboxes
- Indeterminate States: For example, when representing partial selections in multi-select lists.
- Form Validation: Show an undefined state when a user hasn’t made a choice.
- Hierarchical Selections: Indicate partial selections for nested options.
Advantages of Using Cupertino Widgets
- Consistency Across Apple Devices: Maintain a native look on iOS and macOS.
- Smooth Animations: Provide users with fluid interactions.
- Simple Integration: The Cupertino widget system is straightforward and highly customizable.
Conclusion
The CupertinoCheckBox
widget in Flutter offers an elegant solution for implementing macOS-style checkboxes, complete with tristate support. By combining it with widgets like ListTile
and handling state dynamically, you can create polished and functional components for your apps.
Explore more Cupertino widgets to further enhance your app’s design. With Flutter’s flexibility, building platform-specific UI has never been easier.