Skip to content

Commit 961df98

Browse files
authored
Add ZoomPageTransitionsBuilder.allowSnapshotting (#122019)
Add ZoomPageTransitionsBuilder.allowSnapshotting
1 parent 026adb8 commit 961df98

5 files changed

Lines changed: 288 additions & 8 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Flutter code sample for [PageTransitionsTheme].
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const PageTransitionsThemeApp());
10+
11+
class PageTransitionsThemeApp extends StatelessWidget {
12+
const PageTransitionsThemeApp({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return MaterialApp(
17+
theme: ThemeData(
18+
useMaterial3: true,
19+
pageTransitionsTheme: const PageTransitionsTheme(
20+
builders: <TargetPlatform, PageTransitionsBuilder>{
21+
TargetPlatform.android: ZoomPageTransitionsBuilder(
22+
allowSnapshotting: false,
23+
),
24+
},
25+
),
26+
),
27+
home: const HomePage(),
28+
);
29+
}
30+
}
31+
32+
class HomePage extends StatelessWidget {
33+
const HomePage({super.key});
34+
35+
@override
36+
Widget build(BuildContext context) {
37+
return Scaffold(
38+
backgroundColor: Colors.blueGrey,
39+
body: Center(
40+
child: ElevatedButton(
41+
onPressed: () {
42+
Navigator.of(context).push(
43+
MaterialPageRoute<SecondPage>(
44+
builder: (BuildContext context) => const SecondPage(),
45+
),
46+
);
47+
},
48+
child: const Text('To SecondPage'),
49+
),
50+
),
51+
);
52+
}
53+
}
54+
55+
class SecondPage extends StatelessWidget {
56+
const SecondPage({super.key});
57+
58+
@override
59+
Widget build(BuildContext context) {
60+
return Scaffold(
61+
backgroundColor: Colors.purple[200],
62+
body: Center(
63+
child: ElevatedButton(
64+
onPressed: () {
65+
Navigator.of(context).pop();
66+
},
67+
child: const Text('Back to HomePage'),
68+
),
69+
),
70+
);
71+
}
72+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/page_transitions_theme/page_transitions_theme.1.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('MaterialApp defines a custom PageTransitionsTheme', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.PageTransitionsThemeApp(),
13+
);
14+
15+
final Finder homePage = find.byType(example.HomePage);
16+
expect(homePage, findsOneWidget);
17+
18+
final PageTransitionsTheme theme = Theme.of(tester.element(homePage)).pageTransitionsTheme;
19+
expect(theme.builders, isNotNull);
20+
21+
// Check defined page transitions builder for each platform.
22+
for (final TargetPlatform platform in TargetPlatform.values) {
23+
switch (platform) {
24+
case TargetPlatform.android:
25+
expect(theme.builders[platform], isA<ZoomPageTransitionsBuilder>());
26+
final ZoomPageTransitionsBuilder builder = theme.builders[platform]! as ZoomPageTransitionsBuilder;
27+
expect(builder.allowSnapshotting, isFalse);
28+
break;
29+
case TargetPlatform.iOS:
30+
case TargetPlatform.macOS:
31+
case TargetPlatform.linux:
32+
case TargetPlatform.fuchsia:
33+
case TargetPlatform.windows:
34+
expect(theme.builders[platform], isNull);
35+
break;
36+
}
37+
}
38+
39+
// Can navigate to the second page.
40+
expect(find.text('To SecondPage'), findsOneWidget);
41+
await tester.tap(find.text('To SecondPage'));
42+
await tester.pumpAndSettle();
43+
44+
// Can navigate back to the home page.
45+
expect(find.text('Back to HomePage'), findsOneWidget);
46+
await tester.tap(find.text('Back to HomePage'));
47+
await tester.pumpAndSettle();
48+
expect(find.text('To SecondPage'), findsOneWidget);
49+
});
50+
}

‎packages/flutter/lib/src/material/page_transitions_theme.dart‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,36 @@ class ZoomPageTransitionsBuilder extends PageTransitionsBuilder {
610610
/// Constructs a page transition animation that matches the transition used on
611611
/// Android Q.
612612
const ZoomPageTransitionsBuilder({
613+
this.allowSnapshotting = true,
613614
this.allowEnterRouteSnapshotting = true,
614615
});
615616

617+
/// Whether zoom page transitions will prefer to animate a snapshot of the entering
618+
/// and exiting routes.
619+
///
620+
/// If not specified, defaults to true.
621+
///
622+
/// When this value is true, zoom page transitions will snapshot the entering and
623+
/// exiting routes. These snapshots are then animated in place of the underlying
624+
/// widgets to improve performance of the transition.
625+
///
626+
/// Generally this means that animations that occur on the entering/exiting route
627+
/// while the route animation plays may appear frozen - unless they are a hero
628+
/// animation or something that is drawn in a separate overlay.
629+
///
630+
/// {@tool dartpad}
631+
/// This example shows a [MaterialApp] that disables snapshotting for the zoom
632+
/// transitions on Android.
633+
///
634+
/// ** See code in examples/api/lib/material/page_transitions_theme/page_transitions_theme.1.dart **
635+
/// {@end-tool}
636+
///
637+
/// See also:
638+
///
639+
/// * [PageRoute.allowSnapshotting], which enables or disables snapshotting
640+
/// on a per route basis.
641+
final bool allowSnapshotting;
642+
616643
/// Whether to enable snapshotting on the entering route during the
617644
/// transition animation.
618645
///
@@ -633,7 +660,7 @@ class ZoomPageTransitionsBuilder extends PageTransitionsBuilder {
633660
return _ZoomPageTransition(
634661
animation: animation,
635662
secondaryAnimation: secondaryAnimation,
636-
allowSnapshotting: route?.allowSnapshotting ?? true,
663+
allowSnapshotting: allowSnapshotting && (route?.allowSnapshotting ?? true),
637664
allowEnterRouteSnapshotting: allowEnterRouteSnapshotting,
638665
child: child,
639666
);

‎packages/flutter/test/material/page_test.dart‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void main() {
274274

275275
await expectLater(find.byKey(key), matchesGoldenFile('zoom_page_transition.small.png'));
276276

277-
// Change the view insets
277+
// Change the view insets.
278278
tester.binding.window.viewInsetsTestValue = const TestViewPadding(left: 0, top: 0, right: 0, bottom: 500);
279279

280280
await tester.pump();
@@ -287,7 +287,6 @@ void main() {
287287
}
288288
}, variant: TargetPlatformVariant.only(TargetPlatform.android), skip: kIsWeb); // [intended] rasterization is not used on the web.
289289

290-
291290
testWidgets(
292291
'test page transition (_ZoomPageTransition) with rasterization disables snapshotting for enter route',
293292
(WidgetTester tester) async {
@@ -456,7 +455,7 @@ void main() {
456455
expect(find.text('Page 1'), findsNothing);
457456
expect(find.text('Page 2'), isOnstage);
458457

459-
// Page 2 didn't move
458+
// Page 2 didn't move.
460459
expect(tester.getTopLeft(find.text('Page 2')), Offset.zero);
461460
}, variant: TargetPlatformVariant.only(TargetPlatform.android));
462461

@@ -618,7 +617,7 @@ void main() {
618617
expect(find.text('Page 1'), findsNothing);
619618
expect(find.text('Page 2'), isOnstage);
620619

621-
// Page 2 didn't move
620+
// Page 2 didn't move.
622621
expect(tester.getTopLeft(find.text('Page 2')), Offset.zero);
623622
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
624623

@@ -798,7 +797,7 @@ void main() {
798797
);
799798

800799
// Check the basic iOS back-swipe dismiss transition. Dragging the pushed
801-
// route halfway across the screen will trigger the iOS dismiss animation
800+
// route halfway across the screen will trigger the iOS dismiss animation.
802801

803802
await tester.tap(find.text('push'));
804803
await tester.pumpAndSettle();
@@ -809,7 +808,7 @@ void main() {
809808
await gesture.moveBy(const Offset(400, 0));
810809
await gesture.up();
811810
await tester.pump();
812-
expect( // The 'route' route has been dragged to the right, halfway across the screen
811+
expect( // The 'route' route has been dragged to the right, halfway across the screen.
813812
tester.getTopLeft(find.ancestor(of: find.text('route'), matching: find.byType(Scaffold))),
814813
const Offset(400, 0),
815814
);

‎packages/flutter/test/material/page_transitions_theme_test.dart‎

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:flutter/cupertino.dart';
66
import 'package:flutter/foundation.dart';
77
import 'package:flutter/material.dart';
8+
import 'package:flutter/rendering.dart';
89
import 'package:flutter_test/flutter_test.dart';
910

1011
void main() {
@@ -175,7 +176,138 @@ void main() {
175176
expect(findFadeUpwardsPageTransition(), findsOneWidget);
176177
}, variant: TargetPlatformVariant.only(TargetPlatform.android));
177178

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 {
179311
// Regression test for https://github.com/flutter/flutter/issues/58345
180312

181313
int builtCount = 0;

0 commit comments

Comments
 (0)