Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 58e32d2

Browse files
committed
Fix read only TextField focus traversal on macOS
1 parent c575638 commit 58e32d2

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

packages/flutter/lib/src/widgets/editable_text.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,14 +2469,19 @@ class EditableTextState extends State<EditableText>
24692469
/// Read-only input fields do not need a connection with the platform since
24702470
/// there's no need for text editing capabilities (e.g. virtual keyboard).
24712471
///
2472+
/// On macOS, most of the selection and focus related shortcuts require a
2473+
/// connection with the platform because appropriate platform selectors are
2474+
/// sent from the engine and translated into intents. For read-only fields
2475+
/// those shortcuts should be available (for instance to allow tab traversal).
2476+
///
24722477
/// On the web, we always need a connection because we want some browser
24732478
/// functionalities to continue to work on read-only input fields like:
2474-
///
24752479
/// - Relevant context menu.
24762480
/// - cmd/ctrl+c shortcut to copy.
24772481
/// - cmd/ctrl+a to select all.
24782482
/// - Changing the selection using a physical keyboard.
2479-
bool get _shouldCreateInputConnection => kIsWeb || !widget.readOnly;
2483+
bool get _shouldCreateInputConnection =>
2484+
kIsWeb || defaultTargetPlatform == TargetPlatform.macOS || !widget.readOnly;
24802485

24812486
// The time it takes for the floating cursor to snap to the text aligned
24822487
// cursor position after the user has finished placing it.

packages/flutter/test/widgets/editable_text_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,6 +3013,62 @@ void main() {
30133013
}
30143014
});
30153015

3016+
testWidgets(
3017+
'Read-only fields can be traversed on all platforms',
3018+
(WidgetTester tester) async {
3019+
final TextEditingController controller1 = TextEditingController();
3020+
addTearDown(controller1.dispose);
3021+
final TextEditingController controller2 = TextEditingController();
3022+
addTearDown(controller2.dispose);
3023+
final FocusNode focusNode1 = FocusNode();
3024+
addTearDown(focusNode1.dispose);
3025+
final FocusNode focusNode2 = FocusNode();
3026+
addTearDown(focusNode2.dispose);
3027+
3028+
await tester.pumpWidget(
3029+
MaterialApp(
3030+
home: Column(
3031+
children: <Widget>[
3032+
EditableText(
3033+
focusNode: focusNode1,
3034+
autofocus: true,
3035+
controller: controller1,
3036+
backgroundCursorColor: Colors.grey,
3037+
style: textStyle,
3038+
cursorColor: cursorColor,
3039+
),
3040+
EditableText(
3041+
readOnly: true,
3042+
focusNode: focusNode2,
3043+
controller: controller2,
3044+
backgroundCursorColor: Colors.grey,
3045+
style: textStyle,
3046+
cursorColor: cursorColor,
3047+
),
3048+
],
3049+
),
3050+
),
3051+
);
3052+
3053+
expect(focusNode1.hasPrimaryFocus, true);
3054+
expect(focusNode2.hasPrimaryFocus, false);
3055+
3056+
// Change focus to the readonly EditableText.
3057+
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
3058+
3059+
expect(focusNode1.hasPrimaryFocus, false);
3060+
expect(focusNode2.hasPrimaryFocus, true);
3061+
3062+
// Change focus back to the first EditableText.
3063+
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
3064+
3065+
expect(focusNode1.hasPrimaryFocus, true);
3066+
expect(focusNode2.hasPrimaryFocus, false);
3067+
},
3068+
variant: TargetPlatformVariant.all(),
3069+
skip: kIsWeb, // [intended]
3070+
);
3071+
30163072
testWidgets('Sends "updateConfig" when read-only flag is flipped', (WidgetTester tester) async {
30173073
bool readOnly = true;
30183074
late StateSetter setState;

0 commit comments

Comments
 (0)