Improve CupertinoCheckbox fidelity (#151441)
**NOTE: Previous [PR](https://github.com/flutter/flutter/pull/148804) was closed because of a bad merge leading to pollution with unrelated commits.** This PR improves on the look and feel of `CupertinoCheckbox` to more closely match native iOS/macOS checkboxes. Adds the following updates from a native macOS checkbox: * Fill color of an unchecked checkbox is a linear gradient that goes from darker at the top to lighter at the bottom in dark mode * Size of box reduced from 18.0 to 14.0 * Stroke width of check reduced from 2.5 to 2.0 * Border color changed from solid black to gray black in light mode and a transparent gray in dark mode * In light mode, checkbox darkens when pressed * In dark mode, checkbox lightens when pressed * Default blue color of a checked checkbox is darker in dark mode ### Light Mode | Native macOS | Flutter Before | Flutter After | | ----------- | ----------- | ----------- | | <img width="63" alt="native checkbox" src="https://github.com/flutter/flutter/assets/77553258/d57d4c78-2e67-49fb-9491-a5acee3782a7"> | <img width="66" alt="Screenshot 2024-06-27 at 10 23 18 AM" src="https://github.com/flutter/flutter/assets/77553258/31c913ff-d36f-4eb5-b737-3a9117bd7eff"> | <img width="66" alt="Screenshot 2024-06-27 at 10 39 22 AM" src="https://github.com/flutter/flutter/assets/77553258/ace8ef29-efae-4049-8f78-13fd39851947"> | ### Dark Mode - Checked | Native macOS | Flutter Before | Flutter After | | ----------- | ----------- | ----------- | | <img width="22" alt="native light" src="https://github.com/user-attachments/assets/fc52d5e1-7ab0-4a5d-b0fa-5b5bee3ed39d"> | <img width="22" alt="flutter before light" src="https://github.com/user-attachments/assets/16e033a1-d2dd-4fb2-a5a5-f730c5f7cdc7"> | <img width="22" alt="flutter after light" src="https://github.com/user-attachments/assets/8c0cff99-930e-4f5e-8540-e64294c1b4fa"> | ### Dark Mode - Unchecked | Native macOS | Flutter Before | Flutter After | | ----------- | ----------- | ----------- | | <img width="22" alt="native dark mode" src="https://github.com/user-attachments/assets/333280a0-85db-4464-9663-03ef7eafc270"> | <img width="22" alt="flutter before dark mode" src="https://github.com/user-attachments/assets/a46e01ec-0d0b-4bb7-8d08-4b2723424a12"> | <img width="22" alt="flutter dark mode" src="https://github.com/user-attachments/assets/a70ae4ad-f1ad-4441-a416-350cbdc32679"> | ### Light Mode - Disabled | Native macOS | Flutter Before | Flutter After | | --- | --- | --- | | <img width="121" alt="native disabled checkbox" src="https://github.com/user-attachments/assets/ed050d14-efec-49dd-82b6-1e7ed7fa99f9"> | <img width="136" alt="flutter b4 disabled checkbox" src="https://github.com/user-attachments/assets/564918cf-f936-448d-b975-7bf9248bbf35"> | <img width="156" alt="flutter disabled checkbox" src="https://github.com/user-attachments/assets/82f672a7-12e8-469c-99af-9f94c959df8f"> | ### Dark Mode - Disabled | Native macOS | Flutter Before | Flutter After | | --- | --- | --- | | <img width="110" alt="disabled dark checkbox native" src="https://github.com/user-attachments/assets/02a43b3f-5619-4b05-9066-2fd43a58c956"> | <img width="136" alt="disabled dark checkbox flutter b4" src="https://github.com/user-attachments/assets/3a3db322-2002-4808-adc0-b10a7ab42381"> | <img width="140" alt="disabled dark checkbox flutter" src="https://github.com/user-attachments/assets/cb91955a-8302-4dc7-8050-221fa2a7045f"> Fixes #148719. Related PR exploring these changes: #147892 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat [Data Driven Fixes]: https://github.com/flutter/flutter/wiki/Data-driven-Fixes --------- Co-authored-by: Kate Lovett <katelovett@google.com>
This commit is contained in:
@@ -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<double> _kDarkGradientOpacities = <double>[0.14, 0.29];
|
||||
const List<double> _kDisabledDarkGradientOpacities = <double>[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<CupertinoCheckbox> createState() => _CupertinoCheckboxState();
|
||||
@@ -221,19 +268,67 @@ class _CupertinoCheckboxState extends State<CupertinoCheckbox> with TickerProvid
|
||||
@override
|
||||
bool? get value => widget.value;
|
||||
|
||||
void onFocusChange(bool value) {
|
||||
if (focused != value) {
|
||||
focused = value;
|
||||
WidgetStateProperty<Color> get _defaultFillColor {
|
||||
return WidgetStateProperty.resolveWith((Set<WidgetState> 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<Color> get _defaultCheckColor {
|
||||
return WidgetStateProperty.resolveWith((Set<WidgetState> 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<BorderSide> get _defaultSide {
|
||||
return WidgetStateProperty.resolveWith((Set<WidgetState> 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<WidgetState> states) {
|
||||
if (side is WidgetStateBorderSide) {
|
||||
return WidgetStateProperty.resolveAs<BorderSide?>(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<WidgetState> activeStates = states..add(WidgetState.selected);
|
||||
final Set<WidgetState> 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<WidgetState> 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<CupertinoCheckbox> 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<CupertinoCheckbox> 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: <Color>[
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(<String>['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();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user