|
5 | 5 | import 'package:flutter/cupertino.dart'; |
6 | 6 | import 'package:flutter/foundation.dart'; |
7 | 7 | import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter/rendering.dart'; |
8 | 9 | import 'package:flutter_test/flutter_test.dart'; |
9 | 10 |
|
10 | 11 | void main() { |
@@ -175,7 +176,138 @@ void main() { |
175 | 176 | expect(findFadeUpwardsPageTransition(), findsOneWidget); |
176 | 177 | }, variant: TargetPlatformVariant.only(TargetPlatform.android)); |
177 | 178 |
|
178 | | - testWidgets('_ZoomPageTransition only cause child widget built once', (WidgetTester tester) async { |
| 179 | + Widget boilerplate({ |
| 180 | + required bool themeAllowSnapshotting, |
| 181 | + bool secondRouteAllowSnapshotting = true, |
| 182 | + }) { |
| 183 | + return MaterialApp( |
| 184 | + theme: ThemeData( |
| 185 | + useMaterial3: true, |
| 186 | + pageTransitionsTheme: PageTransitionsTheme( |
| 187 | + builders: <TargetPlatform, PageTransitionsBuilder>{ |
| 188 | + TargetPlatform.android: ZoomPageTransitionsBuilder( |
| 189 | + allowSnapshotting: themeAllowSnapshotting, |
| 190 | + ), |
| 191 | + }, |
| 192 | + ), |
| 193 | + ), |
| 194 | + onGenerateRoute: (RouteSettings settings) { |
| 195 | + if (settings.name == '/') { |
| 196 | + return MaterialPageRoute<Widget>( |
| 197 | + builder: (_) => const Material(child: Text('Page 1')), |
| 198 | + ); |
| 199 | + } |
| 200 | + return MaterialPageRoute<Widget>( |
| 201 | + builder: (_) => const Material(child: Text('Page 2')), |
| 202 | + allowSnapshotting: secondRouteAllowSnapshotting, |
| 203 | + ); |
| 204 | + }, |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | + bool isTransitioningWithSnapshotting(WidgetTester tester, Finder of) { |
| 209 | + final Iterable<Layer> layers = tester.layerListOf( |
| 210 | + find.ancestor(of: of, matching: find.byType(SnapshotWidget)).first, |
| 211 | + ); |
| 212 | + final bool hasOneOpacityLayer = layers.whereType<OpacityLayer>().length == 1; |
| 213 | + final bool hasOneTransformLayer = layers.whereType<TransformLayer>().length == 1; |
| 214 | + // When snapshotting is on, the OpacityLayer and TransformLayer will not be |
| 215 | + // applied directly. |
| 216 | + return !(hasOneOpacityLayer && hasOneTransformLayer); |
| 217 | + } |
| 218 | + |
| 219 | + testWidgets('ZoomPageTransitionsBuilder default route snapshotting behavior', (WidgetTester tester) async { |
| 220 | + await tester.pumpWidget( |
| 221 | + boilerplate(themeAllowSnapshotting: true), |
| 222 | + ); |
| 223 | + |
| 224 | + final Finder page1 = find.text('Page 1'); |
| 225 | + final Finder page2 = find.text('Page 2'); |
| 226 | + |
| 227 | + // Transitioning from page 1 to page 2. |
| 228 | + tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/2'); |
| 229 | + await tester.pump(); |
| 230 | + await tester.pump(const Duration(milliseconds: 50)); |
| 231 | + |
| 232 | + // Exiting route should be snapshotted. |
| 233 | + expect(isTransitioningWithSnapshotting(tester, page1), isTrue); |
| 234 | + |
| 235 | + // Entering route should be snapshotted. |
| 236 | + expect(isTransitioningWithSnapshotting(tester, page2), isTrue); |
| 237 | + |
| 238 | + await tester.pumpAndSettle(); |
| 239 | + |
| 240 | + // Transitioning back from page 2 to page 1. |
| 241 | + tester.state<NavigatorState>(find.byType(Navigator)).pop(); |
| 242 | + await tester.pump(); |
| 243 | + await tester.pump(const Duration(milliseconds: 50)); |
| 244 | + |
| 245 | + // Exiting route should be snapshotted. |
| 246 | + expect(isTransitioningWithSnapshotting(tester, page2), isTrue); |
| 247 | + |
| 248 | + // Entering route should be snapshotted. |
| 249 | + expect(isTransitioningWithSnapshotting(tester, page1), isTrue); |
| 250 | + }, variant: TargetPlatformVariant.only(TargetPlatform.android), skip: kIsWeb); // [intended] rasterization is not used on the web. |
| 251 | + |
| 252 | + testWidgets('ZoomPageTransitionsBuilder.allowSnapshotting can disable route snapshotting', (WidgetTester tester) async { |
| 253 | + await tester.pumpWidget( |
| 254 | + boilerplate(themeAllowSnapshotting: false), |
| 255 | + ); |
| 256 | + |
| 257 | + final Finder page1 = find.text('Page 1'); |
| 258 | + final Finder page2 = find.text('Page 2'); |
| 259 | + |
| 260 | + // Transitioning from page 1 to page 2. |
| 261 | + tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/2'); |
| 262 | + await tester.pump(); |
| 263 | + await tester.pump(const Duration(milliseconds: 50)); |
| 264 | + |
| 265 | + // Exiting route should not be snapshotted. |
| 266 | + expect(isTransitioningWithSnapshotting(tester, page1), isFalse); |
| 267 | + |
| 268 | + // Entering route should not be snapshotted. |
| 269 | + expect(isTransitioningWithSnapshotting(tester, page2), isFalse); |
| 270 | + |
| 271 | + await tester.pumpAndSettle(); |
| 272 | + |
| 273 | + // Transitioning back from page 2 to page 1. |
| 274 | + tester.state<NavigatorState>(find.byType(Navigator)).pop(); |
| 275 | + await tester.pump(); |
| 276 | + await tester.pump(const Duration(milliseconds: 50)); |
| 277 | + |
| 278 | + // Exiting route should not be snapshotted. |
| 279 | + expect(isTransitioningWithSnapshotting(tester, page2), isFalse); |
| 280 | + |
| 281 | + // Entering route should not be snapshotted. |
| 282 | + expect(isTransitioningWithSnapshotting(tester, page1), isFalse); |
| 283 | + }, variant: TargetPlatformVariant.only(TargetPlatform.android), skip: kIsWeb); // [intended] rasterization is not used on the web. |
| 284 | + |
| 285 | + testWidgets('Setting PageRoute.allowSnapshotting to false overrides ZoomPageTransitionsBuilder.allowSnapshotting = true', (WidgetTester tester) async { |
| 286 | + await tester.pumpWidget( |
| 287 | + boilerplate( |
| 288 | + themeAllowSnapshotting: true, |
| 289 | + secondRouteAllowSnapshotting: false, |
| 290 | + ), |
| 291 | + ); |
| 292 | + |
| 293 | + final Finder page1 = find.text('Page 1'); |
| 294 | + final Finder page2 = find.text('Page 2'); |
| 295 | + |
| 296 | + // Transitioning from page 1 to page 2. |
| 297 | + tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/2'); |
| 298 | + await tester.pump(); |
| 299 | + await tester.pump(const Duration(milliseconds: 50)); |
| 300 | + |
| 301 | + // First route should be snapshotted. |
| 302 | + expect(isTransitioningWithSnapshotting(tester, page1), isTrue); |
| 303 | + |
| 304 | + // Second route should not be snapshotted. |
| 305 | + expect(isTransitioningWithSnapshotting(tester, page2), isFalse); |
| 306 | + |
| 307 | + await tester.pumpAndSettle(); |
| 308 | + }, variant: TargetPlatformVariant.only(TargetPlatform.android), skip: kIsWeb); // [intended] rasterization is not used on the web. |
| 309 | + |
| 310 | + testWidgets('_ZoomPageTransition only causes child widget built once', (WidgetTester tester) async { |
179 | 311 | // Regression test for https://github.com/flutter/flutter/issues/58345 |
180 | 312 |
|
181 | 313 | int builtCount = 0; |
|
0 commit comments