Line data Source code
1 : part of '../main.dart';
2 :
3 : /// An [InheritedWidget] which should not be accessed by end developers
4 : ///
5 : /// [RootVRouterData] holds methods and parameters from [VRouterState]
6 : class RootVRouterData extends VRouterData {
7 : final VRouterDelegate _state;
8 :
9 12 : RootVRouterData({
10 : Key? key,
11 : required Widget child,
12 : required VRouterDelegate state,
13 : required this.url,
14 : required this.previousUrl,
15 : required this.historyState,
16 : required this.pathParameters,
17 : required this.queryParameters,
18 : }) : _state = state,
19 12 : super(
20 : key: key,
21 : child: child,
22 : );
23 :
24 12 : @override
25 : bool updateShouldNotify(RootVRouterData old) {
26 36 : return (old.url != url ||
27 3 : old.previousUrl != previousUrl ||
28 0 : old.historyState != historyState ||
29 0 : old.pathParameters != pathParameters ||
30 0 : old.queryParameters != queryParameters);
31 : }
32 :
33 : /// Url currently synced with the state
34 : /// This url can differ from the once of the browser if
35 : /// the state has been yet been updated
36 : final String? url;
37 :
38 : /// Previous url that was synced with the state
39 : final String? previousUrl;
40 :
41 : /// This state is saved in the browser history. This means that if the user presses
42 : /// the back or forward button on the navigator, this historyState will be the same
43 : /// as the last one you saved.
44 : ///
45 : /// It can be changed by using [context.vRouter.replaceHistoryState(newState)]
46 : final Map<String, String> historyState;
47 :
48 : /// Maps all route parameters (i.e. parameters of the path
49 : /// mentioned as ":someId")
50 : final Map<String, String> pathParameters;
51 :
52 : /// Contains all query parameters (i.e. parameters after
53 : /// the "?" in the url) of the current url
54 : final Map<String, String> queryParameters;
55 :
56 : /// The duration of the transition which happens when this page
57 : /// is put in the widget tree
58 : ///
59 : /// This should be the default one, i.e. the one of [VRouter]
60 0 : Duration? get _defaultPageTransitionDuration => _state.transitionDuration;
61 :
62 : /// The duration of the transition which happens when this page
63 : /// is removed from the widget tree
64 : ///
65 : /// This should be the default one, i.e. the one of [VRouter]
66 0 : Duration? get _defaultPageReverseTransitionDuration =>
67 0 : _state.reverseTransitionDuration;
68 :
69 : /// A function to build the transition to or from this route
70 : ///
71 : /// This should be the default one, i.e. the one of [VRouter]git
72 11 : Widget Function(
73 : Animation<double> animation,
74 : Animation<double> secondaryAnimation,
75 22 : Widget child)? get _defaultPageBuildTransition => _state.buildTransition;
76 :
77 : /// See [VRouterState.push]
78 9 : void push(
79 : String newUrl, {
80 : Map<String, String> queryParameters = const {},
81 : Map<String, String> historyState = const {},
82 : }) =>
83 18 : _state.push(newUrl,
84 : queryParameters: queryParameters, historyState: historyState);
85 :
86 : /// See [VRouterState.pushNamed]
87 4 : void pushNamed(
88 : String name, {
89 : Map<String, String> pathParameters = const {},
90 : Map<String, String> queryParameters = const {},
91 : Map<String, String> historyState = const {},
92 : }) =>
93 8 : _state.pushNamed(name,
94 : pathParameters: pathParameters,
95 : queryParameters: queryParameters,
96 : historyState: historyState);
97 :
98 : /// See [VRouterState.pushReplacement]
99 1 : void pushReplacement(
100 : String newUrl, {
101 : Map<String, String> queryParameters = const {},
102 : Map<String, String> historyState = const {},
103 : }) =>
104 2 : _state.pushReplacement(newUrl,
105 : queryParameters: queryParameters, historyState: historyState);
106 :
107 : /// See [VRouterState.pushReplacementNamed]
108 0 : void pushReplacementNamed(
109 : String name, {
110 : Map<String, String> pathParameters = const {},
111 : Map<String, String> queryParameters = const {},
112 : Map<String, String> historyState = const {},
113 : }) =>
114 0 : _state.pushReplacementNamed(name,
115 : pathParameters: pathParameters,
116 : queryParameters: queryParameters,
117 : historyState: historyState);
118 :
119 : /// See [VRouterState.pushExternal]
120 0 : void pushExternal(String newUrl, {bool openNewTab = false}) =>
121 0 : _state.pushExternal(newUrl, openNewTab: openNewTab);
122 :
123 : /// See [VRouterState._pop]
124 0 : void pop({
125 : Map<String, String> pathParameters = const {},
126 : Map<String, String> queryParameters = const {},
127 : Map<String, String> newHistoryState = const {},
128 : }) =>
129 0 : popFromElement(
130 0 : _state._vRoute.vRouteElementNode.getVRouteElementToPop(),
131 : pathParameters: pathParameters,
132 : queryParameters: queryParameters,
133 : newHistoryState: newHistoryState,
134 : );
135 :
136 : /// See [VRouterState._systemPop]
137 0 : Future<void> systemPop({
138 : Map<String, String> pathParameters = const {},
139 : Map<String, String> queryParameters = const {},
140 : Map<String, String> newHistoryState = const {},
141 : }) =>
142 0 : systemPopFromElement(
143 0 : _state._vRoute.vRouteElementNode.getVRouteElementToPop(),
144 : pathParameters: pathParameters,
145 : queryParameters: queryParameters,
146 : newHistoryState: newHistoryState,
147 : );
148 :
149 : /// See [VRouterState._pop]
150 5 : void popFromElement(
151 : VRouteElement itemToPop, {
152 : Map<String, String> pathParameters = const {},
153 : Map<String, String> queryParameters = const {},
154 : Map<String, String> newHistoryState = const {},
155 : }) =>
156 10 : _state._pop(
157 : itemToPop,
158 5 : pathParameters: {
159 5 : ...pathParameters,
160 : ...this
161 5 : .pathParameters, // Include the previous path parameters when poping
162 : },
163 : queryParameters: queryParameters,
164 : newHistoryState: newHistoryState,
165 : );
166 :
167 : /// See [VRouterState._systemPop]
168 4 : Future<void> systemPopFromElement(
169 : VRouteElement itemToPop, {
170 : Map<String, String> pathParameters = const {},
171 : Map<String, String> queryParameters = const {},
172 : Map<String, String> newHistoryState = const {},
173 : }) =>
174 8 : _state._systemPop(
175 : itemToPop,
176 4 : pathParameters: {
177 4 : ...pathParameters,
178 : ...this
179 4 : .pathParameters, // Include the previous path parameters when poping
180 : },
181 : queryParameters: queryParameters,
182 : newHistoryState: newHistoryState,
183 : );
184 :
185 : /// See [VRouterState.replaceHistoryState]
186 0 : void replaceHistoryState(Map<String, String> historyState) =>
187 0 : _state.replaceHistoryState(historyState);
188 :
189 12 : static RootVRouterData of(BuildContext context) {
190 : final rootVRouterData =
191 12 : context.dependOnInheritedWidgetOfExactType<RootVRouterData>();
192 : if (rootVRouterData == null) {
193 0 : throw FlutterError(
194 : 'RootVRouterData.of(context) was called with a context which does not contain a VRouter.\n'
195 : 'The context used to retrieve RootVRouterData must be that of a widget that '
196 : 'is a descendant of a VRouter widget.');
197 : }
198 : return rootVRouterData;
199 : }
200 : }
|