flutter/examples/api/lib/widgets/routes/flexible_route_transitions.0.dart
Mitchell Goodwin d877d2875e
Allow mixing route transitions in one app. (#150031)
Fixes #33799

Allows for a route to inform the route below it in the navigation stack how to animate when the topmost route enters are leaves the stack.

It does this by making a `DelegatedTransition` available for the previous route to look up and use. If available, the route lower in the stack will wrap it's transition builders with that delegated transition and use it instead of it's default secondary transition.

This is what the sample code in this PR shows an app that is able to use both a Material zoom transition and a Cupertino slide transition in one app. It also includes a custom vertical transition. Every page animates off the screen in a way to match up with the incoming page's transition. When popped, the correct transitions play in reverse.

https://github.com/user-attachments/assets/1fc910fa-8cde-4e05-898e-daad8ff4a697

The below video shows this logic making a pseudo iOS styled sheet transition.

https://github.com/flutter/flutter/assets/58190796/207163d8-d87f-48b1-aad9-7e770d1d96c5

All existing page transitions in Flutter will be overwritten by the incoming route if a `delegatedTransition` is provided. This can be opted out of through `canTransitionTo` for a new route widget. Of Flutter's existing page transitions, this PR only adds a `DelegatedTransition` for the Zoom and Cupertino transitions. The other transitions possible in Material will get delegated transitions in a later PR.
2024-10-02 20:08:11 +00:00

224 lines
6.7 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/// This sample demonstrates creating a custom page transition that is able to
/// override the outgoing transition of the route behind it in the navigation
/// stack using [DelegatedTransitionBuilder].
void main() {
runApp(const FlexibleRouteTransitionsApp());
}
class FlexibleRouteTransitionsApp extends StatelessWidget {
const FlexibleRouteTransitionsApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Mixing Routes',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
pageTransitionsTheme: const PageTransitionsTheme(
builders: <TargetPlatform, PageTransitionsBuilder>{
// By default the zoom builder is used on all platforms but iOS. Normally
// on iOS the default is the Cupertino sliding transition. Setting
// it to use zoom on all platforms allows the example to show multiple
// transitions in one app for all platforms.
TargetPlatform.iOS: ZoomPageTransitionsBuilder(),
},
),
),
home: const _MyHomePage(title: 'Zoom Page'),
);
}
}
class _MyHomePage extends StatelessWidget {
const _MyHomePage({required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).push(
_VerticalTransitionPageRoute<void>(
builder: (BuildContext context) {
return const _MyHomePage(title: 'Crazy Vertical Page');
},
),
);
},
child: const Text('Crazy Vertical Transition'),
),
TextButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return const _MyHomePage(title: 'Zoom Page');
},
),
);
},
child: const Text('Zoom Transition'),
),
TextButton(
onPressed: () {
final CupertinoPageRoute<void> route = CupertinoPageRoute<void>(
builder: (BuildContext context) {
return const _MyHomePage(title: 'Cupertino Page');
}
);
Navigator.of(context).push(route);
},
child: const Text('Cupertino Transition'),
),
],
),
),
);
}
}
// A PageRoute that applies a _VerticalPageTransition.
class _VerticalTransitionPageRoute<T> extends PageRoute<T> {
_VerticalTransitionPageRoute({
required this.builder,
});
final WidgetBuilder builder;
@override
DelegatedTransitionBuilder? get delegatedTransition => _VerticalPageTransition._delegatedTransitionBuilder;
@override
Color? get barrierColor => const Color(0x00000000);
@override
bool get barrierDismissible => false;
@override
String? get barrierLabel => 'Should be no visible barrier...';
@override
bool get maintainState => true;
@override
bool get opaque => false;
@override
Duration get transitionDuration => const Duration(milliseconds: 2000);
@override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
return builder(context);
}
@override
Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return _VerticalPageTransition(
primaryRouteAnimation: animation,
secondaryRouteAnimation: secondaryAnimation,
child: child,
);
}
}
// A page transition that slides off the screen vertically, and uses
// delegatedTransition to ensure that the outgoing route slides with it.
class _VerticalPageTransition extends StatelessWidget {
_VerticalPageTransition({
required Animation<double> primaryRouteAnimation,
required this.secondaryRouteAnimation,
required this.child,
}) : _primaryPositionAnimation =
CurvedAnimation(
parent: primaryRouteAnimation,
curve: _curve,
reverseCurve: _curve,
).drive(_kBottomUpTween),
_secondaryPositionAnimation =
CurvedAnimation(
parent: secondaryRouteAnimation,
curve: _curve,
reverseCurve: _curve,
)
.drive(_kTopDownTween);
final Animation<Offset> _primaryPositionAnimation;
final Animation<Offset> _secondaryPositionAnimation;
final Animation<double> secondaryRouteAnimation;
final Widget child;
static const Curve _curve = Curves.decelerate;
static final Animatable<Offset> _kBottomUpTween = Tween<Offset>(
begin: const Offset(0.0, 1.0),
end: Offset.zero,
);
static final Animatable<Offset> _kTopDownTween = Tween<Offset>(
begin: Offset.zero,
end: const Offset(0.0, -1.0),
);
// When the _VerticalTransitionPageRoute animates onto or off of the navigation
// stack, this transition is given to the route below it so that they animate in
// sync.
static Widget _delegatedTransitionBuilder(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
bool allowSnapshotting,
Widget? child
) {
final Animatable<Offset> tween = Tween<Offset>(
begin: Offset.zero,
end: const Offset(0.0, -1.0),
).chain(CurveTween(curve: _curve));
return SlideTransition(
position: secondaryAnimation.drive(tween),
child: child,
);
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasDirectionality(context));
final TextDirection textDirection = Directionality.of(context);
return SlideTransition(
position: _secondaryPositionAnimation,
textDirection: textDirection,
transformHitTests: false,
child: SlideTransition(
position: _primaryPositionAnimation,
textDirection: textDirection,
child: ClipRRect(
borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
child: child,
)
),
);
}
}