How to Create a CupertinoCheckBox in Flutter: A macOS-Style Tristate Custom Checkbox Widget


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:
    The CupertinoCheckBox supports a tristate value (true, false, null). Set tristate: true to enable this functionality.
  • ListTile for Interaction:
    The ListTile widget wraps the checkbox, providing a tappable row that updates the state of the checkbox.
  • State Management:
    The onChanged callback updates the isChecked variable, while the setState() method refreshes the UI.
  • Switch Logic:
    Tapping the ListTile cycles the checkbox through the three states: true, false, and null.

Features of CupertinoCheckBox

  1. Native Aesthetic: Matches macOS and iOS design standards.
  2. Tristate Support: Enables an additional state (null) for more complex scenarios.
  3. 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.