From d2a82493a06049d93125b4f7598e61b53c244d0d Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Wed, 8 Jul 2026 22:36:14 -0700 Subject: [PATCH 1/3] Impl --- .../cupertino_checkbox.snippet.0.dart | 35 +++++++++++++++++ .../cupertino_checkbox.snippet.0_test.dart | 39 +++++++++++++++++++ packages/cupertino_ui/lib/src/checkbox.dart | 2 +- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart create mode 100644 packages/cupertino_ui/example/test/checkbox/cupertino_checkbox.snippet.0_test.dart diff --git a/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart b/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart new file mode 100644 index 000000000000..e1fec472f008 --- /dev/null +++ b/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart @@ -0,0 +1,35 @@ +// Copyright 2013 The Flutter Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:cupertino_ui/cupertino_ui.dart'; +import 'package:material_ui/material_ui.dart' show Colors; + +/// Flutter code sample for [CupertinoCheckbox]. + +class CupertinoCheckboxExample extends StatelessWidget { + const CupertinoCheckboxExample({super.key, required this.onChanged}); + + final ValueChanged? onChanged; + + ValueChanged? get _nullableOnChanged => onChanged; + bool get _value => true; + + @override + Widget build(BuildContext context) { + return + // #region body + CupertinoCheckbox( + value: _value, + onChanged: _nullableOnChanged, + fillColor: WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return Colors.orange.withValues(alpha: .32); + } + return Colors.orange; + }), + ) + // #region body + ; + } +} diff --git a/packages/cupertino_ui/example/test/checkbox/cupertino_checkbox.snippet.0_test.dart b/packages/cupertino_ui/example/test/checkbox/cupertino_checkbox.snippet.0_test.dart new file mode 100644 index 000000000000..0371eeea9669 --- /dev/null +++ b/packages/cupertino_ui/example/test/checkbox/cupertino_checkbox.snippet.0_test.dart @@ -0,0 +1,39 @@ +// Copyright 2013 The Flutter Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:cupertino_ui/cupertino_ui.dart'; +import 'package:cupertino_ui_examples/checkbox/cupertino_checkbox.snippet.0.dart' + as example; +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart' show Colors; + +void main() { + testWidgets('Checkbox color is affected by whether it is enabled', (WidgetTester tester) async { + RenderBox getCheckboxRenderer() { + return tester.renderObject(find.byType(CupertinoCheckbox)); + } + + Widget buildApp({required ValueChanged? onChanged}) { + return CupertinoApp( + theme: CupertinoThemeData(brightness: .light), + home: CupertinoPageScaffold( + navigationBar: CupertinoNavigationBar( + middle: Text('CupertinoCheckbox Example'), + ), + child: SafeArea(child: example.CupertinoCheckboxExample( + onChanged: onChanged, + )), + ), + ); + } + + await tester.pumpWidget(buildApp(onChanged: (bool? _) {})); + await tester.pumpAndSettle(); + expect(getCheckboxRenderer(), paints..rrect(color: Colors.orange)); + + await tester.pumpWidget(buildApp(onChanged: null)); + await tester.pumpAndSettle(); + expect(getCheckboxRenderer(), paints..rrect(color: Colors.orange.withValues(alpha: .32))); + }); +} diff --git a/packages/cupertino_ui/lib/src/checkbox.dart b/packages/cupertino_ui/lib/src/checkbox.dart index c917fdb4aece..cf0200867b6f 100644 --- a/packages/cupertino_ui/lib/src/checkbox.dart +++ b/packages/cupertino_ui/lib/src/checkbox.dart @@ -217,7 +217,7 @@ class CupertinoCheckbox extends StatefulWidget { /// ```dart /// CupertinoCheckbox( /// value: true, - /// onChanged: (_){}, + /// onChanged: _nullableOnChanged, /// fillColor: WidgetStateProperty.resolveWith((Set states) { /// if (states.contains(WidgetState.disabled)) { /// return Colors.orange.withValues(alpha: .32); From e94b9f78ed3e336471cab7620e2671d9243df202 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Wed, 8 Jul 2026 22:54:39 -0700 Subject: [PATCH 2/3] More --- .../checkbox/cupertino_checkbox.snippet.0.dart | 2 +- packages/cupertino_ui/lib/src/checkbox.dart | 15 +-------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart b/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart index e1fec472f008..a93e49a816d6 100644 --- a/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart +++ b/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart @@ -29,7 +29,7 @@ class CupertinoCheckboxExample extends StatelessWidget { return Colors.orange; }), ) - // #region body + // #endregion body ; } } diff --git a/packages/cupertino_ui/lib/src/checkbox.dart b/packages/cupertino_ui/lib/src/checkbox.dart index cf0200867b6f..4a60ea1dc2a2 100644 --- a/packages/cupertino_ui/lib/src/checkbox.dart +++ b/packages/cupertino_ui/lib/src/checkbox.dart @@ -207,25 +207,12 @@ class CupertinoCheckbox extends StatefulWidget { /// // TODO(framework): Replace the following block with a blue example container // when it's supported. https://github.com/dart-lang/dartdoc/issues/4243 - // TODO(framework): Add unit tests to this code snippet. - // https://github.com/flutter/flutter/issues/188530 /// /// This example resolves the [fillColor] based on the current [WidgetState] /// of the [CupertinoCheckbox], providing a different [Color] when it is /// [WidgetState.disabled]. /// - /// ```dart - /// CupertinoCheckbox( - /// value: true, - /// onChanged: _nullableOnChanged, - /// fillColor: WidgetStateProperty.resolveWith((Set states) { - /// if (states.contains(WidgetState.disabled)) { - /// return Colors.orange.withValues(alpha: .32); - /// } - /// return Colors.orange; - /// }) - /// ) - /// ``` + /// {@example /example/lib/checkbox/cupertino_checkbox.snippet.0.dart#body indent=strip} /// // TODO(framework): End of the blue example container. /// {@endtemplate} From fb90d5c29c723e15dfb435de332a06b03cc155fc Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Fri, 10 Jul 2026 15:44:57 -0700 Subject: [PATCH 3/3] Add doc --- .../example/lib/checkbox/cupertino_checkbox.snippet.0.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart b/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart index a93e49a816d6..4768c414c82b 100644 --- a/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart +++ b/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart @@ -21,6 +21,8 @@ class CupertinoCheckboxExample extends StatelessWidget { // #region body CupertinoCheckbox( value: _value, + // If `onChanged` is null, this checkbox is disabled. + // If `onChanged` is not null, this checkbox is enabled. onChanged: _nullableOnChanged, fillColor: WidgetStateProperty.resolveWith((Set states) { if (states.contains(WidgetState.disabled)) {