Skip to content

Commit 4adca2f

Browse files
Add dot shorthands support for focus order (flutter#190062)
This PR adds dot shorthand support for lexical / numeric focus traversal order Fixes flutter#189991 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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 f0bbd83 commit 4adca2f

7 files changed

Lines changed: 204 additions & 228 deletions

File tree

‎dev/integration_tests/new_gallery/lib/studies/rally/home.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class _RallyTabBar extends StatelessWidget {
221221
@override
222222
Widget build(BuildContext context) {
223223
return FocusTraversalOrder(
224-
order: const NumericFocusOrder(0),
224+
order: const .numeric(0),
225225
child: TabBar(
226226
// Setting isScrollable to true prevents the tabs from being
227227
// wrapped in [Expanded] widgets, which allows for more

‎dev/integration_tests/new_gallery/lib/studies/rally/tabs/overview.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class _FinancialView extends StatelessWidget {
237237
Widget build(BuildContext context) {
238238
final ThemeData theme = Theme.of(context);
239239
return FocusTraversalOrder(
240-
order: NumericFocusOrder(order!),
240+
order: .numeric(order!),
241241
child: ColoredBox(
242242
color: RallyColors.cardBackground,
243243
child: Column(

‎examples/api/lib/widgets/focus_traversal/focus_traversal_group.0.dart‎

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ class _OrderedButtonState<T> extends State<OrderedButton<T>> {
4343
@override
4444
void initState() {
4545
super.initState();
46-
focusNode = FocusNode(
47-
debugLabel: widget.name,
48-
canRequestFocus: widget.canRequestFocus,
49-
);
46+
focusNode = FocusNode(debugLabel: widget.name, canRequestFocus: widget.canRequestFocus);
5047
}
5148

5249
@override
@@ -70,8 +67,8 @@ class _OrderedButtonState<T> extends State<OrderedButton<T>> {
7067
@override
7168
Widget build(BuildContext context) {
7269
final FocusOrder order = switch (widget.order) {
73-
final num number => NumericFocusOrder(number.toDouble()),
74-
final Object? object => LexicalFocusOrder(object.toString()),
70+
final num number => .numeric(number.toDouble()),
71+
final Object? object => .lexical(object.toString()),
7572
};
7673

7774
return FocusTraversalOrder(
@@ -85,19 +82,13 @@ class _OrderedButtonState<T> extends State<OrderedButton<T>> {
8582
overlayColor: WidgetStateProperty<Color?>.fromMap(
8683
// If neither of these states is active, the property will
8784
// resolve to null, deferring to the default overlay color.
88-
<WidgetState, Color>{
89-
WidgetState.focused: Colors.red,
90-
WidgetState.hovered: Colors.blue,
91-
},
85+
<WidgetState, Color>{.focused: Colors.red, .hovered: Colors.blue},
9286
),
9387
foregroundColor: WidgetStateProperty<Color?>.fromMap(
9488
// "WidgetState.focused | WidgetState.hovered" could be used
9589
// instead of separate map keys, but this setup allows setting
9690
// the button style to a constant value for improved efficiency.
97-
<WidgetState, Color>{
98-
WidgetState.focused: Colors.white,
99-
WidgetState.hovered: Colors.white,
100-
},
91+
<WidgetState, Color>{.focused: Colors.white, .hovered: Colors.white},
10192
),
10293
),
10394
onPressed: () => _handleOnPressed(),
@@ -141,13 +132,8 @@ class FocusTraversalGroupExample extends StatelessWidget {
141132
mainAxisAlignment: .center,
142133
children: List<Widget>.generate(3, (int index) {
143134
// Order as "C" "B", "A".
144-
final String order = String.fromCharCode(
145-
'A'.codeUnitAt(0) + (2 - index),
146-
);
147-
return OrderedButton<String>(
148-
name: 'String: $order',
149-
order: order,
150-
);
135+
final String order = String.fromCharCode('A'.codeUnitAt(0) + (2 - index));
136+
return OrderedButton<String>(name: 'String: $order', order: order);
151137
}),
152138
),
153139
),
@@ -162,10 +148,7 @@ class FocusTraversalGroupExample extends StatelessWidget {
162148
child: Row(
163149
mainAxisAlignment: .center,
164150
children: List<Widget>.generate(3, (int index) {
165-
return OrderedButton<num>(
166-
name: 'ignored num: ${3 - index}',
167-
order: 3 - index,
168-
);
151+
return OrderedButton<num>(name: 'ignored num: ${3 - index}', order: 3 - index);
169152
}),
170153
),
171154
),

‎examples/api/lib/widgets/focus_traversal/ordered_traversal_policy.0.dart‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ class OrderedTraversalPolicyExampleApp extends StatelessWidget {
2323
}
2424

2525
class DemoButton extends StatelessWidget {
26-
const DemoButton({
27-
super.key,
28-
required this.name,
29-
this.autofocus = false,
30-
required this.order,
31-
});
26+
const DemoButton({super.key, required this.name, this.autofocus = false, required this.order});
3227

3328
final String name;
3429
final bool autofocus;
@@ -42,12 +37,8 @@ class DemoButton extends StatelessWidget {
4237
@override
4338
Widget build(BuildContext context) {
4439
return FocusTraversalOrder(
45-
order: NumericFocusOrder(order),
46-
child: TextButton(
47-
autofocus: autofocus,
48-
onPressed: () => _handleOnPressed(),
49-
child: Text(name),
50-
),
40+
order: .numeric(order),
41+
child: TextButton(autofocus: autofocus, onPressed: () => _handleOnPressed(), child: Text(name)),
5142
);
5243
}
5344
}

‎packages/flutter/lib/src/widgets/focus_traversal.dart‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,7 @@ mixin DirectionalFocusTraversalPolicyMixin on FocusTraversalPolicy {
13711371
final FocusScopeNode nearestScope = currentNode.nearestScope!;
13721372
final FocusNode? focusedChild = nearestScope.focusedChild;
13731373
if (focusedChild == null) {
1374-
final FocusNode firstFocus =
1375-
findFirstFocusInDirection(currentNode, direction) ?? currentNode;
1374+
final FocusNode firstFocus = findFirstFocusInDirection(currentNode, direction) ?? currentNode;
13761375
switch (direction) {
13771376
case TraversalDirection.up:
13781377
case TraversalDirection.left:
@@ -1795,6 +1794,12 @@ abstract class FocusOrder with Diagnosticable implements Comparable<FocusOrder>
17951794
/// const constructors so that they can be used in const expressions.
17961795
const FocusOrder();
17971796

1797+
/// Creates a [LexicalFocusOrder] that describes its order in lexical order.
1798+
const factory FocusOrder.lexical(String order) = LexicalFocusOrder;
1799+
1800+
/// Creates a [NumericFocusOrder] that describes its order numerically.
1801+
const factory FocusOrder.numeric(double order) = NumericFocusOrder;
1802+
17981803
/// Compares this object to another [Comparable].
17991804
///
18001805
/// When overriding [FocusOrder], implement [doCompare] instead of this

0 commit comments

Comments
 (0)