Sitelet https://github.com/flutter/flutter/pull/190062/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class _RallyTabBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FocusTraversalOrder(
order: const NumericFocusOrder(0),
order: const .numeric(0),
child: TabBar(
// Setting isScrollable to true prevents the tabs from being
// wrapped in [Expanded] widgets, which allows for more
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class _FinancialView extends StatelessWidget {
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
return FocusTraversalOrder(
order: NumericFocusOrder(order!),
order: .numeric(order!),
child: ColoredBox(
color: RallyColors.cardBackground,
child: Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ class _OrderedButtonState<T> extends State<OrderedButton<T>> {
@override
void initState() {
super.initState();
focusNode = FocusNode(
debugLabel: widget.name,
canRequestFocus: widget.canRequestFocus,
);
focusNode = FocusNode(debugLabel: widget.name, canRequestFocus: widget.canRequestFocus);
}

@override
Expand All @@ -70,8 +67,8 @@ class _OrderedButtonState<T> extends State<OrderedButton<T>> {
@override
Widget build(BuildContext context) {
final FocusOrder order = switch (widget.order) {
final num number => NumericFocusOrder(number.toDouble()),
final Object? object => LexicalFocusOrder(object.toString()),
final num number => .numeric(number.toDouble()),
final Object? object => .lexical(object.toString()),
};

return FocusTraversalOrder(
Expand All @@ -85,19 +82,13 @@ class _OrderedButtonState<T> extends State<OrderedButton<T>> {
overlayColor: WidgetStateProperty<Color?>.fromMap(
// If neither of these states is active, the property will
// resolve to null, deferring to the default overlay color.
<WidgetState, Color>{
WidgetState.focused: Colors.red,
WidgetState.hovered: Colors.blue,
},
<WidgetState, Color>{.focused: Colors.red, .hovered: Colors.blue},
),
foregroundColor: WidgetStateProperty<Color?>.fromMap(
// "WidgetState.focused | WidgetState.hovered" could be used
// instead of separate map keys, but this setup allows setting
// the button style to a constant value for improved efficiency.
<WidgetState, Color>{
WidgetState.focused: Colors.white,
WidgetState.hovered: Colors.white,
},
<WidgetState, Color>{.focused: Colors.white, .hovered: Colors.white},
),
),
onPressed: () => _handleOnPressed(),
Expand Down Expand Up @@ -141,13 +132,8 @@ class FocusTraversalGroupExample extends StatelessWidget {
mainAxisAlignment: .center,
children: List<Widget>.generate(3, (int index) {
// Order as "C" "B", "A".
final String order = String.fromCharCode(
'A'.codeUnitAt(0) + (2 - index),
);
return OrderedButton<String>(
name: 'String: $order',
order: order,
);
final String order = String.fromCharCode('A'.codeUnitAt(0) + (2 - index));
return OrderedButton<String>(name: 'String: $order', order: order);
}),
),
),
Expand All @@ -162,10 +148,7 @@ class FocusTraversalGroupExample extends StatelessWidget {
child: Row(
mainAxisAlignment: .center,
children: List<Widget>.generate(3, (int index) {
return OrderedButton<num>(
name: 'ignored num: ${3 - index}',
order: 3 - index,
);
return OrderedButton<num>(name: 'ignored num: ${3 - index}', order: 3 - index);
}),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ class OrderedTraversalPolicyExampleApp extends StatelessWidget {
}

class DemoButton extends StatelessWidget {
const DemoButton({
super.key,
required this.name,
this.autofocus = false,
required this.order,
});
const DemoButton({super.key, required this.name, this.autofocus = false, required this.order});

final String name;
final bool autofocus;
Expand All @@ -42,12 +37,8 @@ class DemoButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FocusTraversalOrder(
order: NumericFocusOrder(order),
child: TextButton(
autofocus: autofocus,
onPressed: () => _handleOnPressed(),
child: Text(name),
),
order: .numeric(order),
child: TextButton(autofocus: autofocus, onPressed: () => _handleOnPressed(), child: Text(name)),
);
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/flutter/lib/src/widgets/focus_traversal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,7 @@ mixin DirectionalFocusTraversalPolicyMixin on FocusTraversalPolicy {
final FocusScopeNode nearestScope = currentNode.nearestScope!;
final FocusNode? focusedChild = nearestScope.focusedChild;
if (focusedChild == null) {
final FocusNode firstFocus =
findFirstFocusInDirection(currentNode, direction) ?? currentNode;
final FocusNode firstFocus = findFirstFocusInDirection(currentNode, direction) ?? currentNode;
switch (direction) {
case TraversalDirection.up:
case TraversalDirection.left:
Expand Down Expand Up @@ -1795,6 +1794,12 @@ abstract class FocusOrder with Diagnosticable implements Comparable<FocusOrder>
/// const constructors so that they can be used in const expressions.
const FocusOrder();

/// Creates a [LexicalFocusOrder] that describes its order in lexical order.
const factory FocusOrder.lexical(String order) = LexicalFocusOrder;

/// Creates a [NumericFocusOrder] that describes its order numerically.
const factory FocusOrder.numeric(double order) = NumericFocusOrder;

/// Compares this object to another [Comparable].
///
/// When overriding [FocusOrder], implement [doCompare] instead of this
Expand Down
Loading