Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<bool?>? onChanged;

ValueChanged<bool?>? 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<Color>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return Colors.orange.withValues(alpha: .32);
}
return Colors.orange;
}),
)
// #endregion body
;
}
}
Original file line number Diff line number Diff line change
@@ -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<RenderBox>(find.byType(CupertinoCheckbox));
}

Widget buildApp({required ValueChanged<bool?>? 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)));
});
}
15 changes: 1 addition & 14 deletions packages/cupertino_ui/lib/src/checkbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Color>((Set<WidgetState> 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}
Expand Down
Loading