material_with_modal_page_route.dart
2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../modal_bottom_sheet.dart';
import 'bottom_sheet_route.dart';
class MaterialWithModalsPageRoute<T> extends MaterialPageRoute<T> {
/// Construct a MaterialPageRoute whose contents are defined by [builder].
///
/// The values of [builder], [maintainState], and [fullScreenDialog] must not
/// be null.
MaterialWithModalsPageRoute({
required WidgetBuilder builder,
RouteSettings? settings,
bool maintainState = true,
bool fullscreenDialog = false,
}) : assert(builder != null),
assert(maintainState != null),
assert(fullscreenDialog != null),
super(
settings: settings,
fullscreenDialog: fullscreenDialog,
builder: builder,
maintainState: maintainState);
ModalBottomSheetRoute? _nextModalRoute;
@override
bool canTransitionTo(TransitionRoute<dynamic> nextRoute) {
// Don't perform outgoing animation if the next route is a fullscreen dialog.
return (nextRoute is MaterialPageRoute && !nextRoute.fullscreenDialog) ||
(nextRoute is CupertinoPageRoute && !nextRoute.fullscreenDialog) ||
(nextRoute is MaterialWithModalsPageRoute &&
!nextRoute.fullscreenDialog) ||
(nextRoute is ModalBottomSheetRoute);
}
@override
void didChangeNext(Route? nextRoute) {
if (nextRoute is ModalBottomSheetRoute) {
_nextModalRoute = nextRoute;
}
super.didChangeNext(nextRoute);
}
@override
void didPopNext(Route nextRoute) {
super.didPopNext(nextRoute);
}
@override
bool didPop(T? result) {
_nextModalRoute = null;
return super.didPop(result);
}
@override
Widget buildTransitions(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
final theme = Theme.of(context).pageTransitionsTheme;
final nextRoute = _nextModalRoute;
if (nextRoute != null) {
if (!secondaryAnimation.isDismissed) {
// Avoid default transition theme to animate when a new modal view is pushed
final fakeSecondaryAnimation =
Tween<double>(begin: 0, end: 0).animate(secondaryAnimation);
final defaultTransition = theme.buildTransitions<T>(
this, context, animation, fakeSecondaryAnimation, child);
return nextRoute.getPreviousRouteTransition(
context, secondaryAnimation, defaultTransition);
} else {
_nextModalRoute = null;
}
}
return theme.buildTransitions<T>(
this, context, animation, secondaryAnimation, child);
}
}