Make SelectableText focus traversal behavior more standard (#80110)

This commit is contained in:
Ludwik Trammer
2021-05-21 20:49:04 +02:00
committed by GitHub
parent f33e88cef7
commit 31e755452c
2 changed files with 44 additions and 2 deletions

View File

@@ -304,7 +304,9 @@ class SelectableText extends StatefulWidget {
/// focusNode.addListener(() { print(myFocusNode.hasFocus); });
/// ```
///
/// If null, this widget will create its own [FocusNode].
/// If null, this widget will create its own [FocusNode] with
/// [FocusNode.skipTraversal] parameter set to `true`, which causes the widget
/// to be skipped over during focus traversal.
final FocusNode? focusNode;
/// The style to use for the text.
@@ -431,7 +433,8 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive
late _TextSpanEditingController _controller;
FocusNode? _focusNode;
FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());
FocusNode get _effectiveFocusNode =>
widget.focusNode ?? (_focusNode ??= FocusNode(skipTraversal: true));
bool _showSelectionHandles = false;

View File

@@ -1417,6 +1417,45 @@ void main() {
expect(controller.selection, equals(TextRange.empty));
});
testWidgets('Selectable text is skipped during focus traversal',
(WidgetTester tester) async {
final FocusNode firstFieldFocus = FocusNode();
final FocusNode lastFieldFocus = FocusNode();
await tester.pumpWidget(
MaterialApp(
home: Material(
child: Center(
child: Column(
children: <Widget>[
TextField(
focusNode: firstFieldFocus,
autofocus: true,
),
const SelectableText('some text'),
TextField(
focusNode: lastFieldFocus,
),
],
),
),
),
),
);
await tester.pump();
expect(firstFieldFocus.hasFocus, isTrue);
expect(lastFieldFocus.hasFocus, isFalse);
firstFieldFocus.nextFocus();
await tester.pump();
// expecting focus to skip straight to the second field
expect(firstFieldFocus.hasFocus, isFalse);
expect(lastFieldFocus.hasFocus, isTrue);
});
testWidgets('Selectable text identifies as text field in semantics', (WidgetTester tester) async {
final SemanticsTester semantics = SemanticsTester(tester);