Skip to content

Commit 1d268f4

Browse files
Add focus support for CupertinoActionSheetAction #166398 (#167119)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> ### This PR fixes #166398 - Adds focus support for `CupertinoActionSheetAction`. This makes it work with keyboard shortcuts - Creates new widget, `CupertinoTraversalGroup` that applies a Cupertino style focus border around its child when any of its descendant has focus - Employs `CupertinoTraversalGroup` in `CupertinoActionSheet` How the new implementation looks and behaves: https://github.com/user-attachments/assets/ea6789f1-921d-4598-bcca-489dc063ff73 How the native counterpart looks and behaves: https://github.com/user-attachments/assets/4c6ae2a0-7205-4de2-b981-ec7f4839da6e ## 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 --------- Co-authored-by: Tong Mu <[email protected]>
1 parent e302730 commit 1d268f4

5 files changed

Lines changed: 976 additions & 59 deletions

File tree

‎packages/flutter/lib/cupertino.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export 'src/cupertino/colors.dart';
3232
export 'src/cupertino/constants.dart';
3333
export 'src/cupertino/context_menu.dart';
3434
export 'src/cupertino/context_menu_action.dart';
35+
export 'src/cupertino/cupertino_focus_halo.dart';
3536
export 'src/cupertino/date_picker.dart';
3637
export 'src/cupertino/debug.dart';
3738
export 'src/cupertino/desktop_text_selection.dart';
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/widgets.dart';
6+
7+
import 'colors.dart';
8+
import 'constants.dart';
9+
10+
/// Applies an iOS-style focus border around its child when any of child focus nodes gain focus.
11+
///
12+
/// The shape of the focus halo does not automatically adapt to the child widget
13+
/// it encloses. You are responsible for specifying a shape that correctly
14+
/// matches the child's geometry by using the appropriate constructor, such as
15+
/// [CupertinoFocusHalo.withRect] or [CupertinoFocusHalo.withRRect].
16+
///
17+
/// See also:
18+
///
19+
/// * <https://developer.apple.com/design/human-interface-guidelines/focus-and-selection/>
20+
class CupertinoFocusHalo extends StatefulWidget {
21+
/// Creates a rectangular [CupertinoFocusHalo] around the child.
22+
///
23+
/// For example, to highlight a rectangular section of the widget tree when any button inside that
24+
/// section has focus, one could write:
25+
///
26+
/// ```dart
27+
/// CupertinoFocusHalo.withRect(
28+
/// child: Column(
29+
/// children: <Widget>[
30+
/// CupertinoButton(child: const Text('Child 1'), onPressed: () {}),
31+
/// CupertinoButton(child: const Text('Child 2'), onPressed: () {}),
32+
/// ],
33+
/// ),
34+
/// )
35+
/// ```
36+
const CupertinoFocusHalo.withRect({required this.child, super.key})
37+
: _borderRadius = BorderRadius.zero;
38+
39+
/// Creates a rounded rectangular [CupertinoFocusHalo] around the child
40+
///
41+
/// For example, to highlight a rounded rectangular section of the widget tree when any button inside that
42+
/// section has focus, one could write:
43+
///
44+
/// ```dart
45+
/// CupertinoFocusHalo.withRRect(
46+
/// borderRadius: BorderRadius.circular(10.0),
47+
/// child: Column(
48+
/// children: <Widget>[
49+
/// CupertinoButton(child: const Text('Child 1'), onPressed: () {}),
50+
/// CupertinoButton(child: const Text('Child 2'), onPressed: () {}),
51+
/// ],
52+
/// ),
53+
/// )
54+
/// ```
55+
const CupertinoFocusHalo.withRRect({
56+
required this.child,
57+
required BorderRadiusGeometry borderRadius,
58+
super.key,
59+
}) : _borderRadius = borderRadius;
60+
61+
final BorderRadiusGeometry _borderRadius;
62+
63+
/// The child to draw the focused border around.
64+
///
65+
/// Since [CupertinoFocusHalo] can't request focus to itself, this [child] should
66+
/// contain widget(s) that can request focus.
67+
///
68+
/// The child widget is responsible for its own visual shape, for example by
69+
/// using an appropriate clipping.
70+
final Widget child;
71+
72+
@override
73+
State<CupertinoFocusHalo> createState() => _CupertinoFocusHaloState();
74+
}
75+
76+
class _CupertinoFocusHaloState extends State<CupertinoFocusHalo> {
77+
bool _childHasFocus = false;
78+
79+
Color get _effectiveFocusOutlineColor =>
80+
HSLColor.fromColor(CupertinoColors.activeBlue.withOpacity(kCupertinoFocusColorOpacity))
81+
.withLightness(kCupertinoFocusColorBrightness)
82+
.withSaturation(kCupertinoFocusColorSaturation)
83+
.toColor();
84+
85+
@override
86+
Widget build(BuildContext context) {
87+
return Focus(
88+
canRequestFocus: false,
89+
skipTraversal: true,
90+
includeSemantics: false,
91+
onFocusChange: (bool hasFocus) {
92+
setState(() {
93+
_childHasFocus = hasFocus;
94+
});
95+
},
96+
child: DecoratedBox(
97+
position: DecorationPosition.foreground,
98+
decoration: BoxDecoration(
99+
borderRadius: widget._borderRadius,
100+
border: _childHasFocus
101+
? Border.fromBorderSide(BorderSide(color: _effectiveFocusOutlineColor, width: 3.5))
102+
: null,
103+
),
104+
child: widget.child,
105+
),
106+
);
107+
}
108+
}

0 commit comments

Comments
 (0)