Fix off-screen selected text throws exception (#123595)

Fix off-screen selected text throws exception
This commit is contained in:
Taha Tesser 2023-03-29 09:54:53 +03:00 committed by GitHub
parent 47c92c00b0
commit d74a1bb012
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View file

@ -34,6 +34,12 @@ class TextSelectionToolbarAnchors {
renderBox.localToGlobal(Offset.zero),
renderBox.localToGlobal(renderBox.size.bottomRight(Offset.zero)),
);
if (editingRegion.left.isNaN || editingRegion.top.isNaN
|| editingRegion.right.isNaN || editingRegion.bottom.isNaN) {
return const TextSelectionToolbarAnchors(primaryAnchor: Offset.zero);
}
final bool isMultiline = selectionEndpoints.last.point.dy - selectionEndpoints.first.point.dy >
endGlyphHeight / 2;

View file

@ -5399,4 +5399,59 @@ void main() {
);
expect(count, 1); // The `onSelectionChanged` will not be triggered.
});
testWidgets("Off-screen selected text doesn't throw exception", (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/123527
TextSelection? selection;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: Builder(
builder: (BuildContext context) {
return Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: 100,
itemBuilder: (BuildContext context, int index) {
return SelectableText(
'I love Flutter! $index',
onSelectionChanged: (TextSelection s, _) {
selection = s;
},
);
},
),
),
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Pop'),
),
],
);
}
),
),
));
expect(selection, null);
final Offset selectableTextStart = tester.getTopLeft(find.byType(SelectableText).first);
final TestGesture gesture = await tester.startGesture(selectableTextStart + const Offset(50, 5));
await tester.pump(const Duration(milliseconds: 500));
await gesture.up();
expect(selection, isNotNull);
await tester.drag(find.byType(ListView), const Offset(0.0, -3000.0));
await tester.pumpAndSettle();
await tester.tap(find.text('Pop'));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
});
}