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

Skip to content

Commit 9540390

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

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,6 +3013,57 @@ void main() {
30133013
}
30143014
});
30153015

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

0 commit comments

Comments
 (0)