From 1d0cbbb244ddf840285083957bb40866e3cc5b6c Mon Sep 17 00:00:00 2001 From: xubaolin Date: Tue, 7 Feb 2023 14:10:06 +0800 Subject: [PATCH] fix a [SelectableRegion] crash bug (#120076) * fix a crash bug * review feedback --- .../lib/src/widgets/selectable_region.dart | 8 ++--- .../lib/src/widgets/selection_container.dart | 16 +++++++++- .../test/widgets/selectable_region_test.dart | 31 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/widgets/selectable_region.dart b/packages/flutter/lib/src/widgets/selectable_region.dart index 3d68ba9372..8159f060da 100644 --- a/packages/flutter/lib/src/widgets/selectable_region.dart +++ b/packages/flutter/lib/src/widgets/selectable_region.dart @@ -1771,11 +1771,11 @@ abstract class MultiSelectableSelectionContainerDelegate extends SelectionContai LayerLink? effectiveStartHandle = _startHandleLayer; LayerLink? effectiveEndHandle = _endHandleLayer; if (effectiveStartHandle != null || effectiveEndHandle != null) { - final Rect drawableArea = Rect + final Rect? drawableArea = hasSize ? Rect .fromLTWH(0, 0, containerSize.width, containerSize.height) - .inflate(_kSelectionHandleDrawableAreaPadding); - final bool hideStartHandle = value.startSelectionPoint == null || !drawableArea.contains(value.startSelectionPoint!.localPosition); - final bool hideEndHandle = value.endSelectionPoint == null || !drawableArea.contains(value.endSelectionPoint!.localPosition); + .inflate(_kSelectionHandleDrawableAreaPadding) : null; + final bool hideStartHandle = value.startSelectionPoint == null || drawableArea == null || !drawableArea.contains(value.startSelectionPoint!.localPosition); + final bool hideEndHandle = value.endSelectionPoint == null || drawableArea == null|| !drawableArea.contains(value.endSelectionPoint!.localPosition); effectiveStartHandle = hideStartHandle ? null : _startHandleLayer; effectiveEndHandle = hideEndHandle ? null : _endHandleLayer; } diff --git a/packages/flutter/lib/src/widgets/selection_container.dart b/packages/flutter/lib/src/widgets/selection_container.dart index 3f9819986b..0eec34f51c 100644 --- a/packages/flutter/lib/src/widgets/selection_container.dart +++ b/packages/flutter/lib/src/widgets/selection_container.dart @@ -298,12 +298,26 @@ abstract class SelectionContainerDelegate implements SelectionHandler, Selection return box.getTransformTo(ancestor); } + /// Whether the [SelectionContainer] has undergone layout and has a size. + /// + /// See also: + /// + /// * [RenderBox.hasSize], which is used internally by this method. + bool get hasSize { + assert( + _selectionContainerContext?.findRenderObject() != null, + 'The _selectionContainerContext must have a renderObject, such as after the first build has completed.', + ); + final RenderBox box = _selectionContainerContext!.findRenderObject()! as RenderBox; + return box.hasSize; + } + /// Gets the size of the [SelectionContainer] of this delegate. /// /// Can only be called after [SelectionContainer] is laid out. Size get containerSize { assert( - _selectionContainerContext?.findRenderObject() != null, + hasSize, 'containerSize cannot be called before SelectionContainer is laid out.', ); final RenderBox box = _selectionContainerContext!.findRenderObject()! as RenderBox; diff --git a/packages/flutter/test/widgets/selectable_region_test.dart b/packages/flutter/test/widgets/selectable_region_test.dart index 76f3f77128..579740f297 100644 --- a/packages/flutter/test/widgets/selectable_region_test.dart +++ b/packages/flutter/test/widgets/selectable_region_test.dart @@ -73,6 +73,37 @@ void main() { await gesture.up(); }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/102410. + testWidgets('Does not crash when using Navigator pages', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/119776 + await tester.pumpWidget( + MaterialApp( + home: Navigator( + pages: > [ + MaterialPage( + child: Column( + children: [ + const Text('How are you?'), + SelectableRegion( + focusNode: FocusNode(), + selectionControls: materialTextSelectionControls, + child: const SelectAllWidget(child: SizedBox(width: 100, height: 100)), + ), + const Text('Fine, thank you.'), + ], + ), + ), + const MaterialPage( + child: Scaffold(body: Text('Foreground Page')), + ), + ], + onPopPage: (_, __) => false, + ), + ), + ); + + expect(tester.takeException(), isNull); + }); + testWidgets('can draw handles when they are at rect boundaries', (WidgetTester tester) async { final UniqueKey spy = UniqueKey(); await tester.pumpWidget(