Skip to content

Commit df73099

Browse files
authored
Implement superellipse support for CupertinoFocusHalo (#180724)
This PR closes #180719 Related to [this](#179224 (comment)) and [this](#179225 (comment)). ## 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. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#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/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent a78e806 commit df73099

3 files changed

Lines changed: 239 additions & 70 deletions

File tree

‎packages/flutter/lib/src/cupertino/cupertino_focus_halo.dart‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:flutter/widgets.dart';
5+
/// @docImport 'dart:ui';
6+
library;
67

8+
import 'package:flutter/widgets.dart';
79
import 'colors.dart';
810
import 'constants.dart';
911

@@ -34,7 +36,8 @@ class CupertinoFocusHalo extends StatefulWidget {
3436
/// )
3537
/// ```
3638
const CupertinoFocusHalo.withRect({required this.child, super.key})
37-
: _borderRadius = BorderRadius.zero;
39+
: _borderRadius = BorderRadius.zero,
40+
_shapeBuilder = RoundedRectangleBorder.new;
3841

3942
/// Creates a rounded rectangular [CupertinoFocusHalo] around the child
4043
///
@@ -56,10 +59,41 @@ class CupertinoFocusHalo extends StatefulWidget {
5659
required this.child,
5760
required BorderRadiusGeometry borderRadius,
5861
super.key,
59-
}) : _borderRadius = borderRadius;
62+
}) : _borderRadius = borderRadius,
63+
_shapeBuilder = RoundedRectangleBorder.new;
64+
65+
/// Creates a rounded superellipse-shaped [CupertinoFocusHalo] around the child
66+
///
67+
/// For example, to highlight a rounded superellipse-shaped section of the widget tree when any button inside that
68+
/// section has focus, one could write:
69+
///
70+
/// ```dart
71+
/// CupertinoFocusHalo.withRoundedSuperellipse(
72+
/// borderRadius: BorderRadius.circular(10.0),
73+
/// child: Column(
74+
/// children: <Widget>[
75+
/// CupertinoButton(child: const Text('Child 1'), onPressed: () {}),
76+
/// CupertinoButton(child: const Text('Child 2'), onPressed: () {}),
77+
/// ],
78+
/// ),
79+
/// )
80+
/// ```
81+
///
82+
/// See also:
83+
///
84+
/// * [RSuperellipse] and [RoundedSuperellipseBorder] for more introduction on
85+
/// the rounded superellipse shape.
86+
const CupertinoFocusHalo.withRoundedSuperellipse({
87+
required this.child,
88+
required BorderRadiusGeometry borderRadius,
89+
super.key,
90+
}) : _borderRadius = borderRadius,
91+
_shapeBuilder = RoundedSuperellipseBorder.new;
6092

6193
final BorderRadiusGeometry _borderRadius;
6294

95+
final ShapeBorder Function({BorderRadiusGeometry borderRadius, BorderSide side}) _shapeBuilder;
96+
6397
/// The child to draw the focused border around.
6498
///
6599
/// Since [CupertinoFocusHalo] can't request focus to itself, this [child] should
@@ -95,11 +129,13 @@ class _CupertinoFocusHaloState extends State<CupertinoFocusHalo> {
95129
},
96130
child: DecoratedBox(
97131
position: DecorationPosition.foreground,
98-
decoration: BoxDecoration(
99-
borderRadius: widget._borderRadius,
100-
border: _childHasFocus
101-
? Border.fromBorderSide(BorderSide(color: _effectiveFocusOutlineColor, width: 3.5))
102-
: null,
132+
decoration: ShapeDecoration(
133+
shape: widget._shapeBuilder(
134+
side: _childHasFocus
135+
? BorderSide(color: _effectiveFocusOutlineColor, width: 3.5)
136+
: BorderSide.none,
137+
borderRadius: widget._borderRadius,
138+
),
103139
),
104140
child: widget.child,
105141
),

‎packages/flutter/test/cupertino/action_sheet_test.dart‎

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,19 +1990,6 @@ void main() {
19901990

19911991
tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
19921992

1993-
final defaultFocusedBorder = Border.fromBorderSide(
1994-
BorderSide(
1995-
color:
1996-
HSLColor.fromColor(
1997-
CupertinoColors.activeBlue.withOpacity(kCupertinoFocusColorOpacity),
1998-
)
1999-
.withLightness(kCupertinoFocusColorBrightness)
2000-
.withSaturation(kCupertinoFocusColorSaturation)
2001-
.toColor(),
2002-
width: 3.5,
2003-
),
2004-
);
2005-
20061993
await tester.pumpWidget(
20071994
createAppWithButtonThatLaunchesActionSheet(
20081995
CupertinoActionSheet(
@@ -2052,42 +2039,68 @@ void main() {
20522039
matching: decoratedBoxBetweenTraversalGroupAndButtonBackgroundFinder,
20532040
);
20542041

2055-
BoxBorder? getBorder(Finder decoratedBoxFinder) {
2042+
ShapeBorder findBorder(Finder decoratedBoxFinder) {
20562043
final box = tester.widget(decoratedBoxFinder) as DecoratedBox;
2057-
final decoration = box.decoration as BoxDecoration;
2044+
final decoration = box.decoration as ShapeDecoration;
20582045

2059-
return decoration.border;
2046+
return decoration.shape;
20602047
}
20612048

2049+
BorderSide getExpectedBorderSide({required bool hasFocus}) => hasFocus
2050+
? BorderSide(
2051+
color:
2052+
HSLColor.fromColor(
2053+
CupertinoColors.activeBlue.withOpacity(kCupertinoFocusColorOpacity),
2054+
)
2055+
.withLightness(kCupertinoFocusColorBrightness)
2056+
.withSaturation(kCupertinoFocusColorSaturation)
2057+
.toColor(),
2058+
width: 3.5,
2059+
)
2060+
: BorderSide.none;
2061+
2062+
ShapeBorder getExpectedActionHaloBorder({required bool hasFocus}) => RoundedRectangleBorder(
2063+
side: getExpectedBorderSide(hasFocus: hasFocus),
2064+
borderRadius: kCupertinoButtonSizeBorderRadius[CupertinoButtonSize.large]!.copyWith(
2065+
topLeft: Radius.zero,
2066+
topRight: Radius.zero,
2067+
),
2068+
);
2069+
2070+
ShapeBorder getExpectedCancelHaloBorder({required bool hasFocus}) => RoundedRectangleBorder(
2071+
side: getExpectedBorderSide(hasFocus: hasFocus),
2072+
borderRadius: kCupertinoButtonSizeBorderRadius[CupertinoButtonSize.large]!,
2073+
);
2074+
20622075
expect(actionsDecoratedBoxFinder, findsOneWidget);
20632076
expect(cancelDecoratedBoxFinder, findsOneWidget);
20642077

2065-
expect(getBorder(actionsDecoratedBoxFinder), isNull);
2066-
expect(getBorder(cancelDecoratedBoxFinder), isNull);
2078+
expect(findBorder(actionsDecoratedBoxFinder), getExpectedActionHaloBorder(hasFocus: false));
2079+
expect(findBorder(cancelDecoratedBoxFinder), getExpectedCancelHaloBorder(hasFocus: false));
20672080

20682081
focusNodeOne.requestFocus();
20692082
await tester.pumpAndSettle();
20702083

2071-
expect(getBorder(actionsDecoratedBoxFinder), defaultFocusedBorder);
2072-
expect(getBorder(cancelDecoratedBoxFinder), isNull);
2084+
expect(findBorder(actionsDecoratedBoxFinder), getExpectedActionHaloBorder(hasFocus: true));
2085+
expect(findBorder(cancelDecoratedBoxFinder), getExpectedCancelHaloBorder(hasFocus: false));
20732086

20742087
focusNodeTwo.requestFocus();
20752088
await tester.pumpAndSettle();
20762089

2077-
expect(getBorder(actionsDecoratedBoxFinder), defaultFocusedBorder);
2078-
expect(getBorder(cancelDecoratedBoxFinder), isNull);
2090+
expect(findBorder(actionsDecoratedBoxFinder), getExpectedActionHaloBorder(hasFocus: true));
2091+
expect(findBorder(cancelDecoratedBoxFinder), getExpectedCancelHaloBorder(hasFocus: false));
20792092

20802093
focusNodeCancel.requestFocus();
20812094
await tester.pumpAndSettle();
20822095

2083-
expect(getBorder(actionsDecoratedBoxFinder), isNull);
2084-
expect(getBorder(cancelDecoratedBoxFinder), defaultFocusedBorder);
2096+
expect(findBorder(actionsDecoratedBoxFinder), getExpectedActionHaloBorder(hasFocus: false));
2097+
expect(findBorder(cancelDecoratedBoxFinder), getExpectedCancelHaloBorder(hasFocus: true));
20852098

20862099
focusNodeCancel.unfocus();
20872100
await tester.pumpAndSettle();
20882101

2089-
expect(getBorder(actionsDecoratedBoxFinder), isNull);
2090-
expect(getBorder(cancelDecoratedBoxFinder), isNull);
2102+
expect(findBorder(actionsDecoratedBoxFinder), getExpectedActionHaloBorder(hasFocus: false));
2103+
expect(findBorder(cancelDecoratedBoxFinder), getExpectedCancelHaloBorder(hasFocus: false));
20912104
},
20922105
);
20932106

0 commit comments

Comments
 (0)