Skip to content

Commit 66f8fcd

Browse files
Cherry Pick: [SuperTextField][iOS] Fix selection highlight (Resolves #2346) (#2486)
1 parent b5afa67 commit 66f8fcd

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

super_editor/lib/src/super_textfield/ios/ios_textfield.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,17 +629,17 @@ class SuperIOSTextFieldState extends State<SuperIOSTextField>
629629
return Stack(
630630
clipBehavior: Clip.none,
631631
children: [
632-
if (widget.textController?.selection.isValid == true)
632+
if (_textEditingController.selection.isValid == true)
633633
// Selection highlight beneath the text.
634634
TextLayoutSelectionHighlight(
635635
textLayout: textLayout,
636636
style: SelectionHighlightStyle(
637637
color: widget.selectionColor,
638638
),
639-
selection: widget.textController?.selection,
639+
selection: _textEditingController.selection,
640640
),
641641
// Underline beneath the composing region.
642-
if (widget.textController?.composingRegion.isValid == true && widget.showComposingUnderline)
642+
if (_textEditingController.composingRegion.isValid == true && widget.showComposingUnderline)
643643
TextUnderlineLayer(
644644
textLayout: textLayout,
645645
style: StraightUnderlineStyle(
@@ -648,7 +648,7 @@ class SuperIOSTextFieldState extends State<SuperIOSTextField>
648648
),
649649
underlines: [
650650
TextLayoutUnderline(
651-
range: widget.textController!.composingRegion,
651+
range: _textEditingController.composingRegion,
652652
),
653653
],
654654
),

super_editor/test/super_textfield/ios/super_textfield_ios_selection_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import 'package:flutter/material.dart';
44
import 'package:flutter_test/flutter_test.dart';
55
import 'package:flutter_test_runners/flutter_test_runners.dart';
66
import 'package:super_editor/src/infrastructure/platforms/ios/toolbar.dart';
7+
import 'package:super_editor/super_editor_test.dart';
78
import 'package:super_editor/super_text_field.dart';
9+
import 'package:super_text_layout/super_text_layout.dart';
810

911
import '../super_textfield_inspector.dart';
1012
import '../super_textfield_robot.dart';
@@ -108,6 +110,41 @@ void main() {
108110
// Ensure that the text field toolbar is visible.
109111
expect(find.byType(IOSTextEditingFloatingToolbar), findsOneWidget);
110112
});
113+
114+
testWidgetsOnIos('displays selection highlight when controller is not provided', (tester) async {
115+
// Pump a tree with a SuperIOSTextField without providing it a controller to make sure
116+
// SuperIOSTextField does not rely on the provided controller to show the selection highlight.
117+
//
118+
// See https://github.com/superlistapp/super_editor/issues/2346 for details.
119+
await tester.pumpWidget(
120+
MaterialApp(
121+
home: Scaffold(
122+
body: ConstrainedBox(
123+
constraints: const BoxConstraints(minWidth: 300),
124+
child: const SuperIOSTextField(
125+
padding: EdgeInsets.all(12),
126+
caretStyle: CaretStyle(color: Colors.red),
127+
selectionColor: defaultSelectionColor,
128+
handlesColor: Colors.red,
129+
textStyleBuilder: defaultTextFieldStyleBuilder,
130+
),
131+
),
132+
),
133+
),
134+
);
135+
136+
// Place the caret at the beginning of the text.
137+
await tester.placeCaretInSuperTextField(0, find.byType(SuperIOSTextField));
138+
139+
// Type some text.
140+
await tester.typeImeText('This is some text');
141+
142+
// Double tap to select the word "some".
143+
await tester.doubleTapAtSuperTextField(10, find.byType(SuperIOSTextField));
144+
145+
// Ensure the selection highlight is displayed.
146+
expect(find.byType(TextLayoutSelectionHighlight), findsOneWidget);
147+
});
111148
});
112149
}
113150

0 commit comments

Comments
 (0)