Skip to content

Commit 51e24a3

Browse files
author
Shi-Hao Hong
authored
Implement reverseTransitionDuration for TransitionRoute (#48274)
* Implement reverseTransitionDuration in TransitionRoute
1 parent c241f9f commit 51e24a3

2 files changed

Lines changed: 212 additions & 2 deletions

File tree

‎packages/flutter/lib/src/widgets/routes.dart‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,20 @@ abstract class TransitionRoute<T> extends OverlayRoute<T> {
9191
Future<T> get completed => _transitionCompleter.future;
9292
final Completer<T> _transitionCompleter = Completer<T>();
9393

94-
/// The duration the transition lasts.
94+
/// The duration the transition going forwards.
95+
///
96+
/// See also:
97+
///
98+
/// * [reverseTransitionDuration], which controls the duration of the
99+
/// transition when it is in reverse.
95100
Duration get transitionDuration;
96101

102+
/// The duration the transition going in reverse.
103+
///
104+
/// By default, the reverse transition duration is set to the value of
105+
/// the forwards [transitionDuration].
106+
Duration get reverseTransitionDuration => transitionDuration;
107+
97108
/// Whether the route obscures previous routes when the transition is complete.
98109
///
99110
/// When an opaque route's entrance transition is complete, the routes behind
@@ -127,9 +138,11 @@ abstract class TransitionRoute<T> extends OverlayRoute<T> {
127138
AnimationController createAnimationController() {
128139
assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.');
129140
final Duration duration = transitionDuration;
141+
final Duration reverseDuration = reverseTransitionDuration;
130142
assert(duration != null && duration >= Duration.zero);
131143
return AnimationController(
132144
duration: duration,
145+
reverseDuration: reverseDuration,
133146
debugLabel: debugLabel,
134147
vsync: navigator,
135148
);

‎packages/flutter/test/widgets/routes_test.dart‎

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ void main() {
534534
expect(focusNode.hasPrimaryFocus, isTrue);
535535
});
536536

537-
group('TrasitionRoute', () {
537+
group('TransitionRoute', () {
538538
testWidgets('secondary animation is kDismissed when next route finishes pop', (WidgetTester tester) async {
539539
final GlobalKey<NavigatorState> navigator = GlobalKey<NavigatorState>();
540540
await tester.pumpWidget(
@@ -863,9 +863,206 @@ void main() {
863863
expect(rootObserver.dialogCount, 0);
864864
expect(nestedObserver.dialogCount, 1);
865865
});
866+
867+
testWidgets('reverseTransitionDuration defaults to transitionDuration', (WidgetTester tester) async {
868+
final GlobalKey containerKey = GlobalKey();
869+
870+
// Default MaterialPageRoute transition duration should be 300ms.
871+
await tester.pumpWidget(MaterialApp(
872+
onGenerateRoute: (RouteSettings settings) {
873+
return MaterialPageRoute<dynamic>(
874+
builder: (BuildContext context) {
875+
return RaisedButton(
876+
onPressed: () {
877+
Navigator.of(context).push(
878+
MaterialPageRoute<dynamic>(
879+
builder: (BuildContext innerContext) {
880+
return Container(
881+
key: containerKey,
882+
color: Colors.green,
883+
);
884+
},
885+
),
886+
);
887+
},
888+
child: const Text('Open page'),
889+
);
890+
},
891+
);
892+
},
893+
));
894+
895+
// Open the new route.
896+
await tester.tap(find.byType(RaisedButton));
897+
await tester.pumpAndSettle();
898+
expect(find.text('Open page'), findsNothing);
899+
expect(find.byKey(containerKey), findsOneWidget);
900+
901+
// Pop the new route.
902+
tester.state<NavigatorState>(find.byType(Navigator)).pop();
903+
await tester.pump();
904+
expect(find.byKey(containerKey), findsOneWidget);
905+
906+
// Container should be present halfway through the transition.
907+
await tester.pump(const Duration(milliseconds: 150));
908+
expect(find.byKey(containerKey), findsOneWidget);
909+
910+
// Container should be present at the very end of the transition.
911+
await tester.pump(const Duration(milliseconds: 150));
912+
expect(find.byKey(containerKey), findsOneWidget);
913+
914+
// Container have transitioned out after 300ms.
915+
await tester.pump(const Duration(milliseconds: 1));
916+
expect(find.byKey(containerKey), findsNothing);
917+
});
918+
919+
testWidgets('reverseTransitionDuration can be customized', (WidgetTester tester) async {
920+
final GlobalKey containerKey = GlobalKey();
921+
await tester.pumpWidget(MaterialApp(
922+
onGenerateRoute: (RouteSettings settings) {
923+
return MaterialPageRoute<dynamic>(
924+
builder: (BuildContext context) {
925+
return RaisedButton(
926+
onPressed: () {
927+
Navigator.of(context).push(
928+
ModifiedReverseTransitionDurationRoute<dynamic>(
929+
builder: (BuildContext innerContext) {
930+
return Container(
931+
key: containerKey,
932+
color: Colors.green,
933+
);
934+
},
935+
// modified value, default MaterialPageRoute transition duration should be 300ms.
936+
reverseTransitionDuration: const Duration(milliseconds: 150),
937+
),
938+
);
939+
},
940+
child: const Text('Open page'),
941+
);
942+
},
943+
);
944+
},
945+
));
946+
947+
// Open the new route.
948+
await tester.tap(find.byType(RaisedButton));
949+
await tester.pumpAndSettle();
950+
expect(find.text('Open page'), findsNothing);
951+
expect(find.byKey(containerKey), findsOneWidget);
952+
953+
// Pop the new route.
954+
tester.state<NavigatorState>(find.byType(Navigator)).pop();
955+
await tester.pump();
956+
expect(find.byKey(containerKey), findsOneWidget);
957+
958+
// Container should be present halfway through the transition.
959+
await tester.pump(const Duration(milliseconds: 75));
960+
expect(find.byKey(containerKey), findsOneWidget);
961+
962+
// Container should be present at the very end of the transition.
963+
await tester.pump(const Duration(milliseconds: 75));
964+
expect(find.byKey(containerKey), findsOneWidget);
965+
966+
// Container have transitioned out after 150ms.
967+
await tester.pump(const Duration(milliseconds: 1));
968+
expect(find.byKey(containerKey), findsNothing);
969+
});
970+
971+
testWidgets('custom reverseTransitionDuration does not result in interrupted animations', (WidgetTester tester) async {
972+
final GlobalKey containerKey = GlobalKey();
973+
await tester.pumpWidget(MaterialApp(
974+
theme: ThemeData(
975+
pageTransitionsTheme: const PageTransitionsTheme(
976+
builders: <TargetPlatform, PageTransitionsBuilder>{
977+
TargetPlatform.android: FadeUpwardsPageTransitionsBuilder(), // use a fade transition
978+
},
979+
),
980+
),
981+
onGenerateRoute: (RouteSettings settings) {
982+
return MaterialPageRoute<dynamic>(
983+
builder: (BuildContext context) {
984+
return RaisedButton(
985+
onPressed: () {
986+
Navigator.of(context).push(
987+
ModifiedReverseTransitionDurationRoute<dynamic>(
988+
builder: (BuildContext innerContext) {
989+
return Container(
990+
key: containerKey,
991+
color: Colors.green,
992+
);
993+
},
994+
// modified value, default MaterialPageRoute transition duration should be 300ms.
995+
reverseTransitionDuration: const Duration(milliseconds: 150),
996+
),
997+
);
998+
},
999+
child: const Text('Open page'),
1000+
);
1001+
},
1002+
);
1003+
},
1004+
));
1005+
1006+
// Open the new route.
1007+
await tester.tap(find.byType(RaisedButton));
1008+
await tester.pump();
1009+
await tester.pump(const Duration(milliseconds: 200)); // jump partway through the forward transition
1010+
expect(find.byKey(containerKey), findsOneWidget);
1011+
1012+
// Gets the opacity of the fade transition while animating forwards.
1013+
final double topFadeTransitionOpacity = _getOpacity(containerKey, tester);
1014+
1015+
// Pop the new route mid-transition.
1016+
tester.state<NavigatorState>(find.byType(Navigator)).pop();
1017+
await tester.pump();
1018+
1019+
// Transition should not jump. In other words, the fade transition
1020+
// opacity before and after animation changes directions should remain
1021+
// the same.
1022+
expect(_getOpacity(containerKey, tester), topFadeTransitionOpacity);
1023+
1024+
// Reverse transition duration should be:
1025+
// Forward transition elapsed time: 200ms / 300ms = 2 / 3
1026+
// Reverse transition remaining time: 150ms * 2 / 3 = 100ms
1027+
1028+
// Container should be present at the very end of the transition.
1029+
await tester.pump(const Duration(milliseconds: 100));
1030+
expect(find.byKey(containerKey), findsOneWidget);
1031+
1032+
// Container have transitioned out after 100ms.
1033+
await tester.pump(const Duration(milliseconds: 1));
1034+
expect(find.byKey(containerKey), findsNothing);
1035+
});
8661036
});
8671037
}
8681038

1039+
double _getOpacity(GlobalKey key, WidgetTester tester) {
1040+
final Finder finder = find.ancestor(
1041+
of: find.byKey(key),
1042+
matching: find.byType(FadeTransition),
1043+
);
1044+
return tester.widgetList(finder).fold<double>(1.0, (double a, Widget widget) {
1045+
final FadeTransition transition = widget as FadeTransition;
1046+
return a * transition.opacity.value;
1047+
});
1048+
}
1049+
1050+
class ModifiedReverseTransitionDurationRoute<T> extends MaterialPageRoute<T> {
1051+
ModifiedReverseTransitionDurationRoute({
1052+
@required WidgetBuilder builder,
1053+
RouteSettings settings,
1054+
this.reverseTransitionDuration,
1055+
bool fullscreenDialog = false,
1056+
}) : super(
1057+
builder: builder,
1058+
settings: settings,
1059+
fullscreenDialog: fullscreenDialog,
1060+
);
1061+
1062+
@override
1063+
final Duration reverseTransitionDuration;
1064+
}
1065+
8691066
class MockPageRoute extends Mock implements PageRoute<dynamic> { }
8701067

8711068
class MockRoute extends Mock implements Route<dynamic> { }

0 commit comments

Comments
 (0)