Line data Source code
1 : // ignore: public_member_api_docs
2 : import 'package:flutter/material.dart';
3 :
4 : import '../router/utils.dart';
5 : import '../transitions/transition.dart';
6 :
7 : /// Navigator 1.0 to use push, pushReplacement, pushAndRemoveUntil and all pop methods
8 : class Navigator1 {
9 : // ignore: public_member_api_docs
10 8 : Navigator1(this._context);
11 :
12 : final BuildContext _context;
13 :
14 24 : NavigatorState get _navigator => Navigator.of(_context);
15 :
16 : /// Push the given [page] onto the navigator.
17 : ///
18 : /// [transitionDuration] will be [Duration.zero] when [transition] is equals to [Transition.none]
19 : ///
20 : /// [backGestureEnabled] ignored when [transition] is equals to [Transition.material] or [Transition.cupertino]
21 5 : Future<T?> push<T>(
22 : Widget page, {
23 : Object? arguments,
24 : bool maintainState = true,
25 : bool fullscreenDialog = false,
26 : Transition? transition,
27 : Duration? transitionDuration,
28 : bool backGestureEnabled = false,
29 : }) {
30 10 : return _navigator.push<T>(
31 5 : getRoute<T>(
32 : page,
33 : arguments: arguments,
34 : maintainState: maintainState,
35 : fullscreenDialog: fullscreenDialog,
36 : transition: transition,
37 : transitionDuration: transitionDuration,
38 : backGestureEnabled: backGestureEnabled,
39 : ),
40 : );
41 : }
42 :
43 : /// Replace the current [page] of the navigator by pushing the given [page] and then
44 : /// disposing the previous route once the new route has finished animating in.
45 : ///
46 : /// [transitionDuration] will be [Duration.zero] when [transition] is equals to [Transition.none]
47 : ///
48 : /// [backGestureEnabled] ignored when [transition] is [Transition.material] or [Transition.cupertino]
49 1 : Future<T?> pushReplacement<T extends Object?, TO extends Object?>(
50 : Widget page, {
51 : Object? arguments,
52 : bool maintainState = true,
53 : bool fullscreenDialog = false,
54 : Transition? transition,
55 : Duration transitionDuration = const Duration(milliseconds: 300),
56 : bool backGestureEnabled = false,
57 : TO? result,
58 : }) {
59 2 : return _navigator.pushReplacement<T, TO>(
60 1 : getRoute(
61 : page,
62 : arguments: arguments,
63 : maintainState: maintainState,
64 : fullscreenDialog: fullscreenDialog,
65 : transition: transition,
66 : transitionDuration: transitionDuration,
67 : backGestureEnabled: backGestureEnabled,
68 : ),
69 : result: result,
70 : );
71 : }
72 :
73 : /// navigates to a new pages and remove until
74 : ///
75 : /// [transitionDuration] is ignored when transition is equals to Transition.material or Transition.cupertino
76 : ///
77 : /// [backGestureEnabled] not works on Android if transition is Transition.material
78 1 : Future<T?> pushAndRemoveUntil<T>(
79 : Widget page, {
80 : bool Function(Route<dynamic>)? predicate,
81 : Object? arguments,
82 : bool backGestureEnabled = true,
83 : bool maintainState = true,
84 : bool fullscreenDialog = false,
85 : Transition? transition,
86 : Duration? transitionDuration,
87 : }) {
88 2 : return _navigator.pushAndRemoveUntil<T>(
89 1 : getRoute(
90 : page,
91 : arguments: arguments,
92 : maintainState: maintainState,
93 : fullscreenDialog: fullscreenDialog,
94 : transition: transition,
95 : transitionDuration: transitionDuration,
96 : backGestureEnabled: backGestureEnabled,
97 : ),
98 0 : predicate ?? (_) => false,
99 : );
100 : }
101 :
102 : /// Consults the current route's [Route.willPop] method,
103 : /// and acts accordingly, potentially popping the route as a result;
104 : ///
105 : /// returns whether the pop request should be considered handled.
106 1 : Future<bool> maybePop<T>([T? result]) {
107 2 : return _navigator.maybePop(result);
108 : }
109 :
110 : /// remove the current page or dialog from the stack until `predicate`
111 3 : void pop<T>([T? result]) {
112 6 : _navigator.pop(result);
113 : }
114 :
115 : /// remove all pages in the stack until [predicate]
116 1 : void popUntil([bool Function(Route<dynamic>)? predicate]) {
117 2 : _navigator.popUntil(predicate ?? (_) => false);
118 : }
119 :
120 : /// return true if we can do pop
121 3 : bool canPop() {
122 6 : return _navigator.canPop();
123 : }
124 : }
|