From 2f45458e2c335b88f557b31c593f00ba894c351f Mon Sep 17 00:00:00 2001 From: hangyu Date: Mon, 17 Apr 2023 12:59:06 -0700 Subject: [PATCH] Fix a null crash in SelectableRegion Widget (#124736) issue #123378 --- .../lib/src/widgets/selectable_region.dart | 4 +-- .../test/material/selection_area_test.dart | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/selectable_region.dart b/packages/flutter/lib/src/widgets/selectable_region.dart index c8d56898ed..6846489f80 100644 --- a/packages/flutter/lib/src/widgets/selectable_region.dart +++ b/packages/flutter/lib/src/widgets/selectable_region.dart @@ -1301,13 +1301,13 @@ class _SelectableRegionContainerDelegate extends MultiSelectableSelectionContain } void _updateLastEdgeEventsFromGeometries() { - if (currentSelectionStartIndex != -1) { + if (currentSelectionStartIndex != -1 && selectables[currentSelectionStartIndex].value.hasSelection) { final Selectable start = selectables[currentSelectionStartIndex]; final Offset localStartEdge = start.value.startSelectionPoint!.localPosition + Offset(0, - start.value.startSelectionPoint!.lineHeight / 2); _lastStartEdgeUpdateGlobalPosition = MatrixUtils.transformPoint(start.getTransformTo(null), localStartEdge); } - if (currentSelectionEndIndex != -1) { + if (currentSelectionEndIndex != -1 && selectables[currentSelectionEndIndex].value.hasSelection) { final Selectable end = selectables[currentSelectionEndIndex]; final Offset localEndEdge = end.value.endSelectionPoint!.localPosition + Offset(0, -end.value.endSelectionPoint!.lineHeight / 2); diff --git a/packages/flutter/test/material/selection_area_test.dart b/packages/flutter/test/material/selection_area_test.dart index e81281218b..13f690f1fa 100644 --- a/packages/flutter/test/material/selection_area_test.dart +++ b/packages/flutter/test/material/selection_area_test.dart @@ -38,6 +38,37 @@ void main() { } }, variant: TargetPlatformVariant.all()); + testWidgets('Does not crash when long pressing on padding after dragging', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/123378 + await tester.pumpWidget( + const MaterialApp( + color: Color(0xFF2196F3), + title: 'Demo', + home: Scaffold( + body: SelectionArea( + child: Padding( + padding: EdgeInsets.all(100.0), + child: Text('Hello World'), + ), + ), + ), + ), + ); + final TestGesture dragging = await tester.startGesture(const Offset(10, 10)); + addTearDown(dragging.removePointer); + await tester.pump(const Duration(milliseconds: 500)); + await dragging.moveTo(const Offset(90, 90)); + await dragging.up(); + + final TestGesture longpress = await tester.startGesture(const Offset(20,20)); + addTearDown(longpress.removePointer); + await tester.pump(const Duration(milliseconds: 500)); + await longpress.up(); + + expect(tester.takeException(), isNull); + }); + + testWidgets('builds the default context menu by default', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp(