|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_map/flutter_map.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:latlong2/latlong.dart'; |
| 5 | + |
| 6 | +/// Wraps [child] in a box that reports [Size.zero] to the [LayoutBuilder] |
| 7 | +/// constraints for the first build, then switches to [realSize] once |
| 8 | +/// `reveal()` is called - while [MediaQuery] (from the ambient test binding) |
| 9 | +/// stays at its real, non-zero size throughout. |
| 10 | +/// |
| 11 | +/// This reproduces the situation described in |
| 12 | +/// `_FlutterMapStateContainer._parentConstraintsAreSet`'s doc comment: |
| 13 | +/// zero `LayoutBuilder` constraints while the platform/`MediaQuery` size is |
| 14 | +/// already known - which can happen any time a `FlutterMap` is nested under |
| 15 | +/// a widget that doesn't have its final size on the first layout pass (e.g. |
| 16 | +/// `Expanded`/`Flexible` inside a not-yet-measured parent, `AnimatedSize`, |
| 17 | +/// a lazily-revealed tab, etc.), not just during platform startup. |
| 18 | +class _DelayedSizeBox extends StatefulWidget { |
| 19 | + const _DelayedSizeBox({ |
| 20 | + super.key, |
| 21 | + required this.realSize, |
| 22 | + required this.child, |
| 23 | + }); |
| 24 | + |
| 25 | + final Size realSize; |
| 26 | + final Widget child; |
| 27 | + |
| 28 | + @override |
| 29 | + State<_DelayedSizeBox> createState() => _DelayedSizeBoxState(); |
| 30 | +} |
| 31 | + |
| 32 | +class _DelayedSizeBoxState extends State<_DelayedSizeBox> { |
| 33 | + bool _revealed = false; |
| 34 | + |
| 35 | + void reveal() => setState(() => _revealed = true); |
| 36 | + |
| 37 | + @override |
| 38 | + Widget build(BuildContext context) { |
| 39 | + final size = _revealed ? widget.realSize : Size.zero; |
| 40 | + return SizedBox( |
| 41 | + width: size.width, |
| 42 | + height: size.height, |
| 43 | + child: widget.child, |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void main() { |
| 49 | + testWidgets( |
| 50 | + 'initialCameraFit is still applied normally when the real size is ' |
| 51 | + 'available from the very first frame (no zero-size race)', |
| 52 | + (tester) async { |
| 53 | + final controller = MapController(); |
| 54 | + const realSize = Size(390, 500); |
| 55 | + final bounds = LatLngBounds( |
| 56 | + const LatLng(47.5, 7.5), |
| 57 | + const LatLng(52.5, 14.5), |
| 58 | + ); |
| 59 | + |
| 60 | + await tester.pumpWidget( |
| 61 | + MaterialApp( |
| 62 | + home: Center( |
| 63 | + child: SizedBox( |
| 64 | + width: realSize.width, |
| 65 | + height: realSize.height, |
| 66 | + child: FlutterMap( |
| 67 | + mapController: controller, |
| 68 | + options: MapOptions( |
| 69 | + initialCameraFit: CameraFit.bounds( |
| 70 | + bounds: bounds, |
| 71 | + padding: const EdgeInsets.all(50), |
| 72 | + ), |
| 73 | + ), |
| 74 | + children: const [], |
| 75 | + ), |
| 76 | + ), |
| 77 | + ), |
| 78 | + ), |
| 79 | + ); |
| 80 | + |
| 81 | + await tester.pumpAndSettle(); |
| 82 | + |
| 83 | + final camera = controller.camera; |
| 84 | + final expectedCamera = CameraFit.bounds( |
| 85 | + bounds: bounds, |
| 86 | + padding: const EdgeInsets.all(50), |
| 87 | + ).fit(camera.withNonRotatedSize(realSize)); |
| 88 | + |
| 89 | + expect(camera.nonRotatedSize, realSize); |
| 90 | + expect(camera.zoom, closeTo(expectedCamera.zoom, 0.001)); |
| 91 | + }, |
| 92 | + ); |
| 93 | + |
| 94 | + testWidgets( |
| 95 | + 'initialCameraFit applied while nested in a zero-then-real-size parent ' |
| 96 | + 'produces the correctly fitted zoom, not a degenerate one', |
| 97 | + (tester) async { |
| 98 | + final controller = MapController(); |
| 99 | + final key = GlobalKey<_DelayedSizeBoxState>(); |
| 100 | + // Kept within the default flutter_test surface size (800x600) so it |
| 101 | + // isn't clipped by the ambient window and `nonRotatedSize` ends up |
| 102 | + // exactly matching what was requested. |
| 103 | + const realSize = Size(390, 500); |
| 104 | + final bounds = LatLngBounds( |
| 105 | + const LatLng(47.5, 7.5), |
| 106 | + const LatLng(52.5, 14.5), |
| 107 | + ); |
| 108 | + |
| 109 | + await tester.pumpWidget( |
| 110 | + MaterialApp( |
| 111 | + home: Center( |
| 112 | + child: _DelayedSizeBox( |
| 113 | + key: key, |
| 114 | + realSize: realSize, |
| 115 | + child: FlutterMap( |
| 116 | + mapController: controller, |
| 117 | + options: MapOptions( |
| 118 | + initialCameraFit: CameraFit.bounds( |
| 119 | + bounds: bounds, |
| 120 | + padding: const EdgeInsets.all(50), |
| 121 | + ), |
| 122 | + ), |
| 123 | + children: const [], |
| 124 | + ), |
| 125 | + ), |
| 126 | + ), |
| 127 | + ), |
| 128 | + ); |
| 129 | + |
| 130 | + // First frame: FlutterMap is constrained to Size.zero, while |
| 131 | + // MediaQuery already reports the real (non-zero) test window size. |
| 132 | + await tester.pump(); |
| 133 | + |
| 134 | + // Reveal the real size, as would happen once the parent's layout |
| 135 | + // pass completes. |
| 136 | + key.currentState!.reveal(); |
| 137 | + await tester.pumpAndSettle(); |
| 138 | + |
| 139 | + final camera = controller.camera; |
| 140 | + // ignore: avoid_print |
| 141 | + print('camera.zoom=${camera.zoom} camera.center=${camera.center} ' |
| 142 | + 'nonRotatedSize=${camera.nonRotatedSize}'); |
| 143 | + |
| 144 | + expect( |
| 145 | + camera.nonRotatedSize, |
| 146 | + realSize, |
| 147 | + reason: 'sanity check: the widget should have settled at the ' |
| 148 | + 'requested real size, not have it clipped by the test window', |
| 149 | + ); |
| 150 | + |
| 151 | + // Compute what the correct fit *should* be, applied directly against |
| 152 | + // the real, final size - this is what a correctly-behaving |
| 153 | + // initialCameraFit should have converged to. |
| 154 | + final expectedCamera = CameraFit.bounds( |
| 155 | + bounds: bounds, |
| 156 | + padding: const EdgeInsets.all(50), |
| 157 | + ).fit(camera.withNonRotatedSize(realSize)); |
| 158 | + |
| 159 | + expect( |
| 160 | + camera.zoom, |
| 161 | + closeTo(expectedCamera.zoom, 0.001), |
| 162 | + reason: 'initialCameraFit should fit against the real widget size, ' |
| 163 | + 'not the transient zero size seen on the first frame', |
| 164 | + ); |
| 165 | + }, |
| 166 | + ); |
| 167 | +} |
0 commit comments