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..4768c414c82b --- /dev/null +++ b/packages/cupertino_ui/example/lib/checkbox/cupertino_checkbox.snippet.0.dart @@ -0,0 +1,37 @@ +// 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, + // 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)) { + return Colors.orange.withValues(alpha: .32); + } + return Colors.orange; + }), + ) + // #endregion 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 09710c691ffb..49e31f1e8241 100644 --- a/packages/cupertino_ui/lib/src/checkbox.dart +++ b/packages/cupertino_ui/lib/src/checkbox.dart @@ -210,25 +210,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: (_){}, - /// 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}