mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This pull request aims to improve code readability, based on feedback gathered in a recent design doc. <br> There are two factors that hugely impact how easy it is to understand a piece of code: **verbosity** and **complexity**. Reducing **verbosity** is important, because boilerplate makes a project more difficult to navigate. It also has a tendency to make one's eyes gloss over, and subtle typos/bugs become more likely to slip through. Reducing **complexity** makes the code more accessible to more people. This is especially important for open-source projects like Flutter, where the code is read by those who make contributions, as well as others who read through source code as they debug their own projects. <hr> <br> The following examples show how pattern-matching might affect these two factors: <details> <summary><h3>Example 1 (GOOD)</h3> [click to expand]</summary> ```dart if (ancestor case InheritedElement(:final InheritedTheme widget)) { themes.add(widget); } ``` Without using patterns, this might expand to ```dart if (ancestor is InheritedElement) { final InheritedWidget widget = ancestor.widget; if (widget is InheritedTheme) { themes.add(widget); } } ``` Had `ancestor` been a non-local variable, it would need to be "converted" as well: ```dart final Element ancestor = this.ancestor; if (ancestor is InheritedElement) { final InheritedWidget inheritedWidget = ancestor.widget; if (widget is InheritedTheme) { themes.add(theme); } } ``` </details> <details> <summary><h3>Example 2 (BAD) </h3> [click to expand]</summary> ```dart if (widget case PreferredSizeWidget(preferredSize: Size(:final double height))) { return height; } ``` Assuming `widget` is a non-local variable, this would expand to: ```dart final Widget widget = this.widget; if (widget is PreferredSizeWidget) { return widget.preferredSize.height; } ``` <br> </details> In both of the examples above, an `if-case` statement simultaneously verifies that an object meets the specified criteria and performs a variable assignment accordingly. But there are some differences: Example 2 uses a more deeply-nested pattern than Example 1 but makes fewer useful checks. **Example 1:** - checks that `ancestor` is an `InheritedElement` - checks that the inherited element's `widget` is an `InheritedTheme` **Example 2:** - checks that `widget` is a `PreferredSizeWidget` (every `PreferredSizeWidget` has a `size` field, and every `Size` has a `height` field) <br> <hr> I feel hesitant to try presenting a set of cut-and-dry rules as to which scenarios should/shouldn't use pattern-matching, since there are an abundance of different types of patterns, and an abundance of different places where they might be used. But hopefully the conversations we've had recently will help us converge toward a common intuition of how pattern-matching can best be utilized for improved readability. <br><br> - resolves https://github.com/flutter/flutter/issues/152313 - Design Doc: [flutter.dev/go/dart-patterns](https://flutter.dev/go/dart-patterns)
178 lines
5.6 KiB
Dart
178 lines
5.6 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Flutter code sample for [FocusTraversalGroup].
|
|
|
|
void main() => runApp(const FocusTraversalGroupExampleApp());
|
|
|
|
class FocusTraversalGroupExampleApp extends StatelessWidget {
|
|
const FocusTraversalGroupExampleApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const MaterialApp(
|
|
home: FocusTraversalGroupExample(),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// A button wrapper that adds either a numerical or lexical order, depending on
|
|
/// the type of T.
|
|
class OrderedButton<T> extends StatefulWidget {
|
|
const OrderedButton({
|
|
super.key,
|
|
required this.name,
|
|
this.canRequestFocus = true,
|
|
this.autofocus = false,
|
|
required this.order,
|
|
});
|
|
|
|
final String name;
|
|
final bool canRequestFocus;
|
|
final bool autofocus;
|
|
final T order;
|
|
|
|
@override
|
|
State<OrderedButton<T>> createState() => _OrderedButtonState<T>();
|
|
}
|
|
|
|
class _OrderedButtonState<T> extends State<OrderedButton<T>> {
|
|
late FocusNode focusNode;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
focusNode = FocusNode(
|
|
debugLabel: widget.name,
|
|
canRequestFocus: widget.canRequestFocus,
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
focusNode.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(OrderedButton<T> oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
focusNode.canRequestFocus = widget.canRequestFocus;
|
|
}
|
|
|
|
void _handleOnPressed() {
|
|
focusNode.requestFocus();
|
|
debugPrint('Button ${widget.name} pressed.');
|
|
debugDumpFocusTree();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final FocusOrder order = switch (widget.order) {
|
|
final num number => NumericFocusOrder(number.toDouble()),
|
|
final Object? object => LexicalFocusOrder(object.toString()),
|
|
};
|
|
|
|
return FocusTraversalOrder(
|
|
order: order,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: OutlinedButton(
|
|
focusNode: focusNode,
|
|
autofocus: widget.autofocus,
|
|
style: const ButtonStyle(
|
|
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,
|
|
},
|
|
),
|
|
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,
|
|
},
|
|
),
|
|
),
|
|
onPressed: () => _handleOnPressed(),
|
|
child: Text(widget.name),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class FocusTraversalGroupExample extends StatelessWidget {
|
|
const FocusTraversalGroupExample({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ColoredBox(
|
|
color: Colors.white,
|
|
child: FocusTraversalGroup(
|
|
policy: OrderedTraversalPolicy(),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
// A group that is ordered with a numerical order, from left to right.
|
|
FocusTraversalGroup(
|
|
policy: OrderedTraversalPolicy(),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: List<Widget>.generate(3, (int index) {
|
|
return OrderedButton<num>(
|
|
name: 'num: $index',
|
|
// TRY THIS: change this to "3 - index" and see how the order changes.
|
|
order: index,
|
|
);
|
|
}),
|
|
),
|
|
),
|
|
// A group that is ordered with a lexical order, from right to left.
|
|
FocusTraversalGroup(
|
|
policy: OrderedTraversalPolicy(),
|
|
child: Row(
|
|
mainAxisAlignment: 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,
|
|
);
|
|
}),
|
|
),
|
|
),
|
|
// A group that orders in widget order, regardless of what the order is set to.
|
|
FocusTraversalGroup(
|
|
// Because this is NOT an OrderedTraversalPolicy, the
|
|
// assigned order of these OrderedButtons is ignored, and they
|
|
// are traversed in widget order. TRY THIS: change this to
|
|
// "OrderedTraversalPolicy()" and see that it now follows the
|
|
// numeric order set on them instead of the widget order.
|
|
policy: WidgetOrderTraversalPolicy(),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: List<Widget>.generate(3, (int index) {
|
|
return OrderedButton<num>(
|
|
name: 'ignored num: ${3 - index}',
|
|
order: 3 - index,
|
|
);
|
|
}),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|