Line data Source code
1 : part of 'main.dart';
2 :
3 : /// A class which helps you in beforeLeave or beforeEnter functions
4 : /// This class contain 2 main functionality:
5 : /// 1. It allows you to redirect using push, pushNamed, ...
6 : /// 2. It gives you access to information about the previous route and the new route
7 : ///
8 : /// Note that you should use this object to redirect in beforeLeave and beforeEnter. Never
9 : /// use VRouterData to do so.
10 : class VRedirector {
11 10 : VRedirector({
12 : required BuildContext context,
13 : required this.from,
14 : required this.to,
15 : required this.previousVRouterData,
16 : required this.newVRouterData,
17 : }) : _context = context;
18 :
19 : /// If [_shouldUpdate] is set to false, the current url updating is stopped
20 : ///
21 : /// You should NOT modify this, instead use [stopRedirection], or other methods
22 : /// such as [push], [pushNamed], ...
23 : bool _shouldUpdate = true;
24 :
25 18 : bool get shouldUpdate => _shouldUpdate;
26 :
27 : /// The url we are coming from
28 : final String? from;
29 :
30 : /// The url we are going to
31 : final String? to;
32 :
33 : /// The [VRouterData] of the previous route
34 : /// Useful information is:
35 : /// * [VRouterData.pathParameters]
36 : /// * [VRouterData.queryParameters]
37 : /// * [VRouterData.historyState]
38 : ///
39 : /// Note that you should NOT call [newVRouterData.replaceHistoryState]
40 : /// If you are in beforeLeave, call [saveHistoryState] instead
41 : /// If you are in beforeEnter, you can't save an history state here
42 : final RootVRouterData? previousVRouterData;
43 :
44 : /// The [VRouterData] of the new route
45 : /// Useful information is:
46 : /// * [VRouterData.pathParameters]
47 : /// * [VRouterData.queryParameters]
48 : /// * [VRouterData.historyState]
49 : ///
50 : /// Note that you should NOT call [newVRouterData.replaceHistoryState]
51 : /// If you are in beforeLeave, call [saveHistoryState] instead
52 : /// If you are in beforeEnter, you can't save an history state here
53 : final RootVRouterData? newVRouterData;
54 :
55 : /// A context which gives us access to VRouter and the current VRoute
56 : /// This is local because we don't want developers to use VRouterData to redirect
57 : final BuildContext _context;
58 :
59 : /// Function which will be executed after stopping the redirection
60 : /// if [push], [pushNamed], ... have been used.
61 : void Function(VRouteElementNode vRouteElementNode)? _redirectFunction;
62 :
63 : /// Stops the redirection
64 : ///
65 : /// This also checks that only one method which stops the redirection is used
66 4 : void stopRedirection() {
67 4 : if (!shouldUpdate) {
68 : throw 'You already stopped the redirection. You can only use one such action on VRedirector.';
69 : }
70 4 : _shouldUpdate = false;
71 : }
72 :
73 : /// Prevent the current redirection and push a route instead
74 : ///
75 : /// See [VRouter.push] for more information on push
76 1 : void push(
77 : String newUrl, {
78 : Map<String, String> queryParameters = const {},
79 : Map<String, String> historyState = const {},
80 : }) {
81 1 : stopRedirection();
82 5 : _redirectFunction = (_) => RootVRouterData.of(_context).push(newUrl,
83 : queryParameters: queryParameters, historyState: historyState);
84 : }
85 :
86 : /// Prevent the current redirection and pushNamed a route instead
87 : ///
88 : /// See [VRouter.push] for more information on push
89 1 : void pushNamed(
90 : String name, {
91 : Map<String, String> pathParameters = const {},
92 : Map<String, String> queryParameters = const {},
93 : String? routerState,
94 : }) {
95 1 : stopRedirection();
96 5 : _redirectFunction = (_) => RootVRouterData.of(_context).pushNamed(name,
97 : pathParameters: pathParameters,
98 : queryParameters: queryParameters,
99 : routerState: routerState);
100 : }
101 :
102 : /// Prevent the current redirection and pushReplacement a route instead
103 : ///
104 : /// See [VRouter.push] for more information on push
105 1 : void pushReplacement(
106 : String newUrl, {
107 : Map<String, String> queryParameters = const {},
108 : Map<String, String> historyState = const {},
109 : }) {
110 1 : stopRedirection();
111 5 : _redirectFunction = (_) => RootVRouterData.of(_context).pushReplacement(
112 : newUrl,
113 : queryParameters: queryParameters,
114 : historyState: historyState);
115 : }
116 :
117 : /// Prevent the current redirection and pushReplacementNamed a route instead
118 : ///
119 : /// See [VRouter.push] for more information on push
120 0 : void pushReplacementNamed(
121 : String name, {
122 : Map<String, String> pathParameters = const {},
123 : Map<String, String> queryParameters = const {},
124 : Map<String, String> historyState = const {},
125 : }) {
126 0 : stopRedirection();
127 0 : _redirectFunction = (_) => RootVRouterData.of(_context).pushReplacementNamed(
128 : name,
129 : pathParameters: pathParameters,
130 : queryParameters: queryParameters,
131 : historyState: historyState,
132 : );
133 : }
134 :
135 : /// Prevent the current redirection and pushExternal instead
136 : ///
137 : /// See [VRouter.push] for more information on push
138 0 : void pushExternal(String newUrl, {bool openNewTab = false}) {
139 0 : stopRedirection();
140 0 : _redirectFunction = (_) => RootVRouterData.of(_context)
141 0 : .pushExternal(newUrl, openNewTab: openNewTab);
142 : }
143 :
144 : /// Prevent the current redirection and call pop instead
145 : ///
146 : /// See [VRouter.pop] for more information on push
147 1 : void pop(
148 : {Map<String, String> pathParameters = const {},
149 : Map<String, String> queryParameters = const {},
150 : Map<String, String> newHistoryState = const {},}) {
151 1 : stopRedirection();
152 2 : _redirectFunction = (VRouteElementNode vRouteElementNode) =>
153 4 : RootVRouterData.of(_context).pop(vRouteElementNode.getVRouteElementToPop(), pathParameters: pathParameters, queryParameters: queryParameters, newHistoryState: newHistoryState,);
154 : }
155 :
156 : /// Prevent the current redirection and call systemPop instead
157 : ///
158 : /// See [VRouter.systemPop] for more information on push
159 1 : Future<void> systemPop(
160 : {Map<String, String> pathParameters = const {},
161 : Map<String, String> queryParameters = const {},
162 : Map<String, String> newHistoryState = const {},}) async {
163 1 : stopRedirection();
164 2 : _redirectFunction = (VRouteElementNode vRouteElementNode) =>
165 4 : RootVRouterData.of(_context).systemPop(vRouteElementNode.getVRouteElementToPop(), pathParameters: pathParameters, queryParameters: queryParameters, newHistoryState: newHistoryState,);
166 : }
167 : }
|