Unverified Commit 31e75545 authored by Ludwik Trammer's avatar Ludwik Trammer Committed by GitHub

Make SelectableText focus traversal behavior more standard (#80110)

parent f33e88ce
......@@ -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;
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment