diff --git a/packages/flutter/lib/src/widgets/interactive_viewer.dart b/packages/flutter/lib/src/widgets/interactive_viewer.dart index 1bb4bb35c5..28b318efac 100644 --- a/packages/flutter/lib/src/widgets/interactive_viewer.dart +++ b/packages/flutter/lib/src/widgets/interactive_viewer.dart @@ -494,11 +494,7 @@ class _InteractiveViewerState extends State with TickerProvid // The _boundaryRect is calculated by adding the boundaryMargin to the size of // the child. - Rect _boundaryRectCached; Rect get _boundaryRect { - if (_boundaryRectCached != null) { - return _boundaryRectCached; - } assert(_childKey.currentContext != null); assert(!widget.boundaryMargin.left.isNaN); assert(!widget.boundaryMargin.right.isNaN); @@ -507,15 +503,15 @@ class _InteractiveViewerState extends State with TickerProvid final RenderBox childRenderBox = _childKey.currentContext.findRenderObject() as RenderBox; final Size childSize = childRenderBox.size; - _boundaryRectCached = widget.boundaryMargin.inflateRect(Offset.zero & childSize); + final Rect boundaryRect = widget.boundaryMargin.inflateRect(Offset.zero & childSize); // Boundaries that are partially infinite are not allowed because Matrix4's // rotation and translation methods don't handle infinites well. - assert(_boundaryRectCached.isFinite || - (_boundaryRectCached.left.isInfinite - && _boundaryRectCached.top.isInfinite - && _boundaryRectCached.right.isInfinite - && _boundaryRectCached.bottom.isInfinite), 'boundaryRect must either be infinite in all directions or finite in all directions.'); - return _boundaryRectCached; + assert(boundaryRect.isFinite || + (boundaryRect.left.isInfinite + && boundaryRect.top.isInfinite + && boundaryRect.right.isInfinite + && boundaryRect.bottom.isInfinite), 'boundaryRect must either be infinite in all directions or finite in all directions.'); + return boundaryRect; } // The Rect representing the child's parent. @@ -917,10 +913,6 @@ class _InteractiveViewerState extends State with TickerProvid @override void didUpdateWidget(InteractiveViewer oldWidget) { super.didUpdateWidget(oldWidget); - if (widget.child != oldWidget.child || widget.boundaryMargin != oldWidget.boundaryMargin) { - _boundaryRectCached = null; - } - // Handle all cases of needing to dispose and initialize // transformationControllers. if (oldWidget.transformationController == null) { diff --git a/packages/flutter/test/widgets/interactive_viewer_test.dart b/packages/flutter/test/widgets/interactive_viewer_test.dart index ca5f808285..1597bf7b6b 100644 --- a/packages/flutter/test/widgets/interactive_viewer_test.dart +++ b/packages/flutter/test/widgets/interactive_viewer_test.dart @@ -450,6 +450,56 @@ void main() { expect(transformationController.value.getMaxScaleOnAxis(), equals(1.0)); }); + + testWidgets('viewport changes size', (WidgetTester tester) async { + final TransformationController transformationController = TransformationController(); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: InteractiveViewer( + transformationController: transformationController, + child: Container(), + ), + ), + ), + ), + ); + + expect(transformationController.value, equals(Matrix4.identity())); + + // Attempting to drag to pan doesn't work because the child fits inside + // the viewport and has a tight boundary. + final Offset childOffset = tester.getTopLeft(find.byType(Container)); + final Offset childInterior = Offset( + childOffset.dx + 20.0, + childOffset.dy + 20.0, + ); + TestGesture gesture = await tester.startGesture(childInterior); + addTearDown(gesture.removePointer); + await tester.pump(); + await gesture.moveTo(childOffset); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(transformationController.value, equals(Matrix4.identity())); + + // Shrink the size of the screen. + tester.binding.window.physicalSizeTestValue = const Size(100.0, 100.0); + addTearDown(tester.binding.window.clearPhysicalSizeTestValue); + await tester.pump(); + + // Attempting to drag to pan still doesn't work, because the image has + // resized itself to fit the new screen size, and InteractiveViewer has + // updated its measurements to take that into consideration. + gesture = await tester.startGesture(childInterior); + await tester.pump(); + await gesture.moveTo(childOffset); + await tester.pump(); + await gesture.up(); + await tester.pumpAndSettle(); + expect(transformationController.value, equals(Matrix4.identity())); + }); }); group('getNearestPointOnLine', () {