diff --git a/packages/flutter/lib/src/cupertino/checkbox.dart b/packages/flutter/lib/src/cupertino/checkbox.dart index 3c84615a90..51fb59ff27 100644 --- a/packages/flutter/lib/src/cupertino/checkbox.dart +++ b/packages/flutter/lib/src/cupertino/checkbox.dart @@ -8,10 +8,12 @@ /// @docImport 'switch.dart'; library; +import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'colors.dart'; import 'constants.dart'; +import 'theme.dart'; // Examples can assume: // bool _throwShotAway = false; @@ -23,6 +25,33 @@ const double _kCupertinoFocusColorOpacity = 0.80; const double _kCupertinoFocusColorBrightness = 0.69; const double _kCupertinoFocusColorSaturation = 0.835; +// Eyeballed from a checkbox on a physical Macbook Pro running macOS version 14.5. +const Color _kDisabledCheckColor = CupertinoDynamicColor.withBrightness( + color: Color.fromARGB(64, 0, 0, 0), + darkColor: Color.fromARGB(64, 255, 255, 255), +); +const Color _kDisabledBorderColor = CupertinoDynamicColor.withBrightness( + color: Color.fromARGB(13, 0, 0, 0), + darkColor: Color.fromARGB(13, 0, 0, 0), +); +const CupertinoDynamicColor _kDefaultBorderColor = CupertinoDynamicColor.withBrightness( + color: Color.fromARGB(255, 209, 209, 214), + darkColor: Color.fromARGB(50, 128, 128, 128), +); +const CupertinoDynamicColor _kDefaultFillColor = CupertinoDynamicColor.withBrightness( + color: CupertinoColors.activeBlue, + darkColor: Color.fromARGB(255, 50, 100, 215), +); +const Color _kDefaultCheckColor = CupertinoDynamicColor.withBrightness( + color: CupertinoColors.white, + darkColor: Color.fromARGB(255, 222, 232, 248), +); +const double _kPressedOverlayOpacity = 0.15; +// In dark mode, the fill color of a checkbox is an opacity gradient of the +// background color. +const List _kDarkGradientOpacities = [0.14, 0.29]; +const List _kDisabledDarkGradientOpacities = [0.08, 0.14]; + /// A macOS style checkbox. /// /// The checkbox itself does not maintain any state. Instead, when the state of @@ -133,7 +162,8 @@ class CupertinoCheckbox extends StatefulWidget { /// The color to use for the check icon when this checkbox is checked. /// - /// If null, then the value of [CupertinoColors.white] is used. + /// If null, then the value of [CupertinoColors.white] is used if the checkbox + /// is enabled. If the checkbox is disabled, a grey-black color is used. final Color? checkColor; /// If true, the checkbox's [value] can be true, false, or null. @@ -162,8 +192,25 @@ class CupertinoCheckbox extends StatefulWidget { /// The color and width of the checkbox's border. /// - /// If this property is null, then the side defaults to a one pixel wide - /// black, solid border. + /// This property can be a [WidgetStateBorderSide] that can + /// specify different border color and widths depending on the + /// checkbox's state. + /// + /// Resolves in the following states: + /// * [WidgetState.pressed]. + /// * [WidgetState.selected]. + /// * [WidgetState.hovered]. + /// * [WidgetState.focused]. + /// * [WidgetState.disabled]. + /// * [WidgetState.error]. + /// + /// If this property is not a [WidgetStateBorderSide] and it is + /// non-null, then it is only rendered when the checkbox's value is + /// false. The difference in interpretation is for backwards + /// compatibility. + /// + /// If this property is null and the checkbox's value is false, then the side + /// defaults to a one pixel wide grey-black border. final BorderSide? side; /// The shape of the checkbox. @@ -180,7 +227,7 @@ class CupertinoCheckbox extends StatefulWidget { final String? semanticLabel; /// The width of a checkbox widget. - static const double width = 18.0; + static const double width = 14.0; @override State createState() => _CupertinoCheckboxState(); @@ -221,19 +268,67 @@ class _CupertinoCheckboxState extends State with TickerProvid @override bool? get value => widget.value; - void onFocusChange(bool value) { - if (focused != value) { - focused = value; + WidgetStateProperty get _defaultFillColor { + return WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled)) { + return CupertinoColors.white.withOpacity(0.5); + } + if (states.contains(WidgetState.selected)) { + return widget.activeColor ?? CupertinoDynamicColor.resolve(_kDefaultFillColor, context); + } + return CupertinoColors.white; + }); + } + + WidgetStateProperty get _defaultCheckColor { + return WidgetStateProperty.resolveWith((Set states) { + if (states.contains(WidgetState.disabled) && states.contains(WidgetState.selected)) { + return widget.checkColor ?? CupertinoDynamicColor.resolve(_kDisabledCheckColor, context); + } + if (states.contains(WidgetState.selected)) { + return widget.checkColor ?? CupertinoDynamicColor.resolve(_kDefaultCheckColor, context); + } + return CupertinoColors.white; + }); + } + + WidgetStateProperty get _defaultSide { + return WidgetStateProperty.resolveWith((Set states) { + if ((states.contains(WidgetState.selected) || states.contains(WidgetState.focused)) + && !states.contains(WidgetState.disabled)) { + return const BorderSide(width: 0.0, color: CupertinoColors.transparent); + } + if (states.contains(WidgetState.disabled)) { + return BorderSide(color: CupertinoDynamicColor.resolve(_kDisabledBorderColor, context)); + } + return BorderSide(color: CupertinoDynamicColor.resolve(_kDefaultBorderColor, context)); + }); + } + + BorderSide? _resolveSide(BorderSide? side, Set states) { + if (side is WidgetStateBorderSide) { + return WidgetStateProperty.resolveAs(side, states); } + if (!states.contains(WidgetState.selected)) { + return side; + } + return null; } @override Widget build(BuildContext context) { - final Color effectiveActiveColor = widget.activeColor - ?? CupertinoColors.activeBlue; - final Color? inactiveColor = widget.inactiveColor; - final Color effectiveInactiveColor = inactiveColor - ?? CupertinoColors.inactiveGray; + // Colors need to be resolved in selected and non selected states separately. + final Set activeStates = states..add(WidgetState.selected); + final Set inactiveStates = states..remove(WidgetState.selected); + + // Since the states getter always makes a new set, make a copy to use + // throughout the lifecycle of this build method. + final Set currentStates = states; + + final Color effectiveActiveColor = _defaultFillColor.resolve(activeStates); + + final BorderSide effectiveBorderSide = _resolveSide(widget.side, currentStates) + ?? _defaultSide.resolve(currentStates); final Color effectiveFocusOverlayColor = widget.focusColor ?? HSLColor @@ -242,9 +337,6 @@ class _CupertinoCheckboxState extends State with TickerProvid .withSaturation(_kCupertinoFocusColorSaturation) .toColor(); - final Color effectiveCheckColor = widget.checkColor - ?? CupertinoColors.white; - return Semantics( label: widget.semanticLabel, checked: widget.value ?? false, @@ -252,22 +344,25 @@ class _CupertinoCheckboxState extends State with TickerProvid child: buildToggleable( focusNode: widget.focusNode, autofocus: widget.autofocus, - onFocusChange: onFocusChange, size: const Size.square(kMinInteractiveDimensionCupertino), painter: _painter + ..position = position + ..reaction = reaction ..focusColor = effectiveFocusOverlayColor - ..isFocused = focused ..downPosition = downPosition + ..isFocused = currentStates.contains(WidgetState.focused) + ..isHovered = currentStates.contains(WidgetState.hovered) ..activeColor = effectiveActiveColor - ..inactiveColor = effectiveInactiveColor - ..checkColor = effectiveCheckColor + ..inactiveColor = _defaultFillColor.resolve(inactiveStates) + ..checkColor = _defaultCheckColor.resolve(currentStates) ..value = value ..previousValue = _previousValue ..isActive = widget.onChanged != null ..shape = widget.shape ?? RoundedRectangleBorder( borderRadius: BorderRadius.circular(4.0), ) - ..side = widget.side, + ..side = effectiveBorderSide + ..brightness = CupertinoTheme.of(context).brightness ), ); } @@ -314,9 +409,9 @@ class _CheckboxPainter extends ToggleablePainter { notifyListeners(); } - BorderSide? get side => _side; + BorderSide get side => _side!; BorderSide? _side; - set side(BorderSide? value) { + set side(BorderSide value) { if (_side == value) { return; } @@ -324,6 +419,16 @@ class _CheckboxPainter extends ToggleablePainter { notifyListeners(); } + Brightness? get brightness => _brightness; + Brightness? _brightness; + set brightness(Brightness? value) { + if (_brightness == value) { + return; + } + _brightness = value; + notifyListeners(); + } + Rect _outerRectAt(Offset origin) { const double size = CupertinoCheckbox.width; final Rect rect = Rect.fromLTWH(origin.dx, origin.dy, size, size); @@ -341,12 +446,36 @@ class _CheckboxPainter extends ToggleablePainter { return Paint() ..color = checkColor ..style = PaintingStyle.stroke - ..strokeWidth = 2.5 + ..strokeWidth = 2.0 ..strokeCap = StrokeCap.round; } - void _drawBox(Canvas canvas, Rect outer, Paint paint, BorderSide? side, bool fill) { - if (fill) { + // Draw a gradient from the top to the bottom of the checkbox. + void _drawFillGradient(Canvas canvas, Rect outer, Color topColor, Color bottomColor) { + final LinearGradient fillGradient = LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + // Eyeballed from a checkbox on a physical Macbook Pro running macOS version 14.5. + colors: [ + topColor, + bottomColor, + ], + ); + final Paint gradientPaint = Paint() + ..shader = fillGradient.createShader(outer); + canvas.drawPath(shape.getOuterPath(outer), gradientPaint); + } + + void _drawBox(Canvas canvas, Rect outer, Paint paint, BorderSide? side, bool value) { + // Draw a gradient in dark mode except when the checkbox is enabled and checked. + if (brightness == Brightness.dark && !(isActive && value)) { + _drawFillGradient( + canvas, + outer, + paint.color.withOpacity(isActive ? _kDarkGradientOpacities[0] : _kDisabledDarkGradientOpacities[0]), + paint.color.withOpacity(isActive ? _kDarkGradientOpacities[1] : _kDisabledDarkGradientOpacities[1]), + ); + } else { canvas.drawPath(shape.getOuterPath(outer), paint); } if (side != null) { @@ -359,12 +488,11 @@ class _CheckboxPainter extends ToggleablePainter { // The ratios for the offsets below were found from looking at the checkbox // examples on in the HIG docs. The distance from the needed point to the // edge was measured, then divided by the total width. - const Offset start = Offset(CupertinoCheckbox.width * 0.25, CupertinoCheckbox.width * 0.52); - const Offset mid = Offset(CupertinoCheckbox.width * 0.46, CupertinoCheckbox.width * 0.75); - const Offset end = Offset(CupertinoCheckbox.width * 0.72, CupertinoCheckbox.width * 0.29); + const Offset start = Offset(CupertinoCheckbox.width * 0.22, CupertinoCheckbox.width * 0.54); + const Offset mid = Offset(CupertinoCheckbox.width * 0.40, CupertinoCheckbox.width * 0.75); + const Offset end = Offset(CupertinoCheckbox.width * 0.78, CupertinoCheckbox.width * 0.25); path.moveTo(origin.dx + start.dx, origin.dy + start.dy); path.lineTo(origin.dx + mid.dx, origin.dy + mid.dy); - canvas.drawPath(path, paint); path.moveTo(origin.dx + mid.dx, origin.dy + mid.dy); path.lineTo(origin.dx + end.dx, origin.dy + end.dy); canvas.drawPath(path, paint); @@ -382,33 +510,34 @@ class _CheckboxPainter extends ToggleablePainter { void paint(Canvas canvas, Size size) { final Paint strokePaint = _createStrokePaint(); final Offset origin = size / 2.0 - const Size.square(CupertinoCheckbox.width) / 2.0 as Offset; - final Rect outer = _outerRectAt(origin); final Paint paint = Paint()..color = _colorAt(value ?? true); - if (value == false) { - - final BorderSide border = side ?? BorderSide(color: paint.color); - _drawBox(canvas, outer, paint, border, false); - } else { - - _drawBox(canvas, outer, paint, side, true); - if (value ?? false) { + switch (value) { + case false: + _drawBox(canvas, outer, paint, side, value ?? true); + case true: + _drawBox(canvas, outer, paint, side, value ?? true); _drawCheck(canvas, origin, strokePaint); - } else { + case null: + _drawBox(canvas, outer, paint, side, value ?? true); _drawDash(canvas, origin, strokePaint); - } } - + // The checkbox's opacity changes when pressed. + if (downPosition != null) { + final Paint pressedPaint = Paint() + ..color = brightness == Brightness.light + ? CupertinoColors.black.withOpacity(_kPressedOverlayOpacity) + : CupertinoColors.white.withOpacity(_kPressedOverlayOpacity); + canvas.drawPath(shape.getOuterPath(outer), pressedPaint); + } if (isFocused) { final Rect focusOuter = outer.inflate(1); - final Paint borderPaint = Paint() ..color = focusColor ..style = PaintingStyle.stroke ..strokeWidth = 3.5; - - _drawBox(canvas, focusOuter, borderPaint, side, true); + _drawBox(canvas, focusOuter, borderPaint, side, value ?? true); } } } diff --git a/packages/flutter/test/cupertino/checkbox_test.dart b/packages/flutter/test/cupertino/checkbox_test.dart index fbe0fe7355..30e0867652 100644 --- a/packages/flutter/test/cupertino/checkbox_test.dart +++ b/packages/flutter/test/cupertino/checkbox_test.dart @@ -2,6 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// reduced-test-set: +// This file is run as part of a reduced test set in CI on Mac and Windows +// machines. +@Tags(['reduced-test-set']) +library; + import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; @@ -25,7 +31,7 @@ void main() { value: false, onChanged: (bool? b) { }, ), - ) + ), ), ); @@ -45,7 +51,7 @@ void main() { value: true, onChanged: (bool? b) { }, ), - ) + ), ), ); @@ -66,7 +72,7 @@ void main() { value: false, onChanged: null, ), - ) + ), ), ); @@ -92,7 +98,7 @@ void main() { value: true, onChanged: null, ), - ) + ), ), ); @@ -110,7 +116,7 @@ void main() { tristate: true, onChanged: null, ), - ) + ), ), ); @@ -128,7 +134,7 @@ void main() { tristate: true, onChanged: null, ), - ) + ), ), ); @@ -146,7 +152,7 @@ void main() { tristate: true, onChanged: null, ), - ) + ), ), ); @@ -461,9 +467,286 @@ void main() { paints ..drrect( color: const Color(0xfff44336), - outer: RRect.fromLTRBR(13.0, 13.0, 31.0, 31.0, const Radius.circular(5)), - inner: RRect.fromLTRBR(17.0, 17.0, 27.0, 27.0, const Radius.circular(1)), + outer: RRect.fromLTRBR(15.0, 15.0, 29.0, 29.0, const Radius.circular(5)), + inner: RRect.fromLTRBR(19.0, 19.0, 25.0, 25.0, const Radius.circular(1)), ), ); }); + + testWidgets('Checkbox default colors, and size in light mode', (WidgetTester tester) async { + Widget buildCheckbox({bool value = true}) { + return CupertinoApp( + home: Center( + child: RepaintBoundary( + child: CupertinoCheckbox( + value: value, + onChanged: (bool? newValue) {}, + ), + ), + ), + ); + } + await tester.pumpWidget(buildCheckbox()); + await expectLater( + find.byType(CupertinoCheckbox), + matchesGoldenFile('checkbox.light_theme.selected.png'), + ); + await tester.pumpWidget(buildCheckbox(value: false)); + await expectLater( + find.byType(CupertinoCheckbox), + matchesGoldenFile('checkbox.light_theme.unselected.png'), + ); + }); + + testWidgets('Checkbox default colors, and size in dark mode', (WidgetTester tester) async { + Widget buildCheckbox({bool value = true}) { + return CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + home: Center( + child: RepaintBoundary( + child: CupertinoCheckbox( + value: value, + onChanged: (bool? newValue) {}, + ), + ), + ), + ); + } + await tester.pumpWidget(buildCheckbox()); + await expectLater( + find.byType(CupertinoCheckbox), + matchesGoldenFile('checkbox.dark_theme.selected.png'), + ); + await tester.pumpWidget(buildCheckbox(value: false)); + await expectLater( + find.byType(CupertinoCheckbox), + matchesGoldenFile('checkbox.dark_theme.unselected.png'), + ); + }); + + testWidgets('Disabled checkbox default colors, and size in light mode', (WidgetTester tester) async { + Widget buildCheckbox({bool value = true}) { + return CupertinoApp( + home: Center( + child: RepaintBoundary( + child: CupertinoCheckbox( + value: value, + onChanged: null, + ), + ), + ), + ); + } + await tester.pumpWidget(buildCheckbox()); + await expectLater( + find.byType(CupertinoCheckbox), + matchesGoldenFile('checkbox.disabled_light_theme.selected.png'), + ); + await tester.pumpWidget(buildCheckbox(value: false)); + await expectLater( + find.byType(CupertinoCheckbox), + matchesGoldenFile('checkbox.disabled_light_theme.unselected.png'), + ); + }); + + testWidgets('Disabled checkbox default colors, and size in dark mode', (WidgetTester tester) async { + Widget buildCheckbox({bool value = true}) { + return CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + home: Center( + child: RepaintBoundary( + child: CupertinoCheckbox( + value: value, + onChanged: null, + ), + ), + ), + ); + } + await tester.pumpWidget(buildCheckbox()); + await expectLater( + find.byType(CupertinoCheckbox), + matchesGoldenFile('checkbox.disabled_dark_theme.selected.png'), + ); + await tester.pumpWidget(buildCheckbox(value: false)); + await expectLater( + find.byType(CupertinoCheckbox), + matchesGoldenFile('checkbox.disabled_dark_theme.unselected.png'), + ); + }); + + testWidgets('Checkbox configures focus color', (WidgetTester tester) async { + const Color defaultCheckColor = Color(0xffffffff); + const Color defaultActiveFillColor = Color(0xff007aff); + const Color defaultFocusColor = Color(0xcc6eadf2); + const Color testFocusColor = Color(0xffaabbcc); + tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional; + final FocusNode node = FocusNode(); + addTearDown(node.dispose); + + Widget buildApp({Color? focusColor, bool autofocus = false, FocusNode? focusNode}) { + return CupertinoApp( + home: Center( + child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) { + return CupertinoCheckbox( + value: true, + onChanged: (bool? newValue) {}, + autofocus: autofocus, + focusNode: focusNode, + focusColor: focusColor, + ); + }), + ), + ); + } + await tester.pumpWidget(buildApp(focusNode: node, autofocus: true)); + await tester.pump(); + expect(node.hasPrimaryFocus, isTrue); + expect( + find.byType(CupertinoCheckbox), + paints + ..path(color: defaultActiveFillColor) + ..rrect() + ..path(color: defaultCheckColor) + ..path(color: defaultFocusColor, strokeWidth: 3.5, style: PaintingStyle.stroke), + reason: 'Checkbox shows the correct focus color', + ); + + await tester.pumpWidget(buildApp( + focusColor: testFocusColor, + focusNode: node, + autofocus: true, + )); + await tester.pump(); + expect(node.hasPrimaryFocus, isTrue); + expect( + find.byType(CupertinoCheckbox), + paints + ..path(color: defaultActiveFillColor) + ..rrect() + ..path(color: defaultCheckColor) + ..path(color: testFocusColor, strokeWidth: 3.5, style: PaintingStyle.stroke), + reason: 'Checkbox can configure a focus color', + ); + }); + + testWidgets('Checkbox is darkened when pressed in light mode', (WidgetTester tester) async { + const Color defaultCheckColor = Color(0xffffffff); + const Color defaultActiveFillColor = Color(0xff007aff); + const Color defaultInactiveFillColor = Color(0xffffffff); + const Color pressedDarkShadow = Color(0x26ffffff); + + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: CupertinoCheckbox( + value: false, + onChanged: (_) {}, + ), + ), + ), + ); + + final TestGesture gesture1 = await tester.startGesture(tester.getCenter(find.byType(CupertinoCheckbox))); + await tester.pump(); + + expect( + find.byType(CupertinoCheckbox), + paints + ..path(color: defaultInactiveFillColor) + ..drrect() + ..path(color: pressedDarkShadow), + reason: 'Inactive pressed checkbox is slightly darkened', + ); + + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: CupertinoCheckbox( + value: true, + onChanged: (_) {}, + ), + ), + ), + ); + + final TestGesture gesture2 = await tester.startGesture(tester.getCenter(find.byType(CupertinoCheckbox))); + await tester.pump(); + + expect( + find.byType(CupertinoCheckbox), + paints + ..path(color: defaultActiveFillColor) + ..rrect() + ..path(color: defaultCheckColor) + ..path(color: pressedDarkShadow), + reason: 'Active pressed checkbox is slightly darkened', + ); + + // Finish gestures to release resources. + await gesture1.up(); + await gesture2.up(); + await tester.pump(); + }); + + testWidgets('Checkbox is lightened when pressed in dark mode', (WidgetTester tester) async { + const Color checkColor = Color(0xffdee8f8); + const Color defaultActiveFillColor = Color(0xff3264d7); + const Color defaultInactiveFillColor = Color(0xff000000); + const Color pressedLightShadow = Color(0x26ffffff); + + await tester.pumpWidget( + CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + home: Center( + child: CupertinoCheckbox( + value: false, + onChanged: (_) {}, + ), + ), + ), + ); + + final TestGesture gesture1 = await tester.startGesture(tester.getCenter(find.byType(CupertinoCheckbox))); + await tester.pump(); + + expect( + find.byType(CupertinoCheckbox), + paints + ..path(color: defaultInactiveFillColor) + ..drrect() + ..path(color: pressedLightShadow), + reason: 'Inactive pressed checkbox is slightly lightened', + ); + + await tester.pumpWidget( + CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + home: Center( + child: CupertinoCheckbox( + value: true, + onChanged: (_) {}, + ), + ), + ), + ); + + final TestGesture gesture2 = await tester.startGesture(tester.getCenter(find.byType(CupertinoCheckbox))); + await tester.pump(); + + expect( + find.byType(CupertinoCheckbox), + paints + ..path(color: defaultActiveFillColor) + ..rrect() + ..path(color: checkColor) + ..path(color: pressedLightShadow), + reason: 'Active pressed checkbox is slightly lightened', + ); + + // Finish gestures to release resources. + await gesture1.up(); + await gesture2.up(); + await tester.pump(); + }); }