Showing
1 changed file
with
74 additions
and
11 deletions
@@ -15,7 +15,7 @@ Add this to your package's pubspec.yaml file: | @@ -15,7 +15,7 @@ Add this to your package's pubspec.yaml file: | ||
15 | 15 | ||
16 | ``` | 16 | ``` |
17 | dependencies: | 17 | dependencies: |
18 | - get: ^1.8.1 | 18 | + get: ^1.9.2 |
19 | ``` | 19 | ``` |
20 | 20 | ||
21 | And import it: | 21 | And import it: |
@@ -212,6 +212,40 @@ void main() { | @@ -212,6 +212,40 @@ void main() { | ||
212 | )); | 212 | )); |
213 | } | 213 | } |
214 | ``` | 214 | ``` |
215 | +#### Middleware | ||
216 | +If you want to hear Get events to trigger actions, you can add a GetObserver to your materialApp. This is extremely useful for triggering events whenever a specific Screen is displayed on the screen. Currently on Flutter you would have to put the event on initState and wait for a possible response in a navigator.pop (context); to get that. But with Get, this is extremely simple! | ||
217 | + | ||
218 | +##### add GetObserver(); | ||
219 | +```dart | ||
220 | +void main() { | ||
221 | + runApp(MaterialApp( | ||
222 | + onGenerateRoute: Router.generateRoute, | ||
223 | + initialRoute: "/", | ||
224 | + navigatorKey: Get.key, | ||
225 | + navigatorObservers: [ | ||
226 | + GetObserver(MiddleWare.observer), // HERE !!! | ||
227 | + ], | ||
228 | + )); | ||
229 | +} | ||
230 | +``` | ||
231 | +Create a MiddleWare class | ||
232 | + | ||
233 | +```dart | ||
234 | +class MiddleWare { | ||
235 | + static observer(Routing routing) { | ||
236 | + /// You can listen in addition to the routes, the snackbars, dialogs and bottomsheets on each screen. | ||
237 | + ///If you need to enter any of these 3 events directly here, | ||
238 | + ///you must specify that the event is != Than you are trying to do. | ||
239 | + if (routing.current == '/second' && !routing.isSnackbar) { | ||
240 | + Get.snackbar("Hi", "You are on second route"); | ||
241 | + } else if (routing.current =='/third'){ | ||
242 | + print('last route called'); | ||
243 | + } | ||
244 | + } | ||
245 | +} | ||
246 | +``` | ||
247 | + | ||
248 | + | ||
215 | 249 | ||
216 | ### COPY THE ROUTER CLASS BELOW: | 250 | ### COPY THE ROUTER CLASS BELOW: |
217 | Copy this Router class below and put it in your app, rename routes and classes for your own, add more classes to it if necessary. | 251 | Copy this Router class below and put it in your app, rename routes and classes for your own, add more classes to it if necessary. |
@@ -224,20 +258,24 @@ class Router { | @@ -224,20 +258,24 @@ class Router { | ||
224 | switch (settings.name) { | 258 | switch (settings.name) { |
225 | case '/': | 259 | case '/': |
226 | return GetRoute( | 260 | return GetRoute( |
227 | - page: SplashScreen(), | 261 | + page: First(), |
228 | settings: settings, | 262 | settings: settings, |
229 | ); | 263 | ); |
230 | - case '/Home': | ||
231 | - return GetRoute(settings: settings, page: Home(), transition: Transition.fade); | ||
232 | - case '/Chat': | ||
233 | - return GetRoute(settings: settings, page: Chat(), transition: Transition.rightToLeft); | 264 | + case '/second': |
265 | + return GetRoute( | ||
266 | + settings: settings, page: Second(), transition: Transition.fade); | ||
267 | + case '/third': | ||
268 | + return GetRoute( | ||
269 | + settings: settings, | ||
270 | + page: Third(), | ||
271 | + transition: Transition.rightToLeft); | ||
234 | default: | 272 | default: |
235 | return GetRoute( | 273 | return GetRoute( |
236 | settings: settings, | 274 | settings: settings, |
237 | transition: Transition.fade, | 275 | transition: Transition.fade, |
238 | page: Scaffold( | 276 | page: Scaffold( |
239 | - body: Center( | ||
240 | - child: Text('No route defined for ${settings.name}')), | 277 | + body: |
278 | + Center(child: Text('No route defined for ${settings.name}')), | ||
241 | )); | 279 | )); |
242 | } | 280 | } |
243 | } | 281 | } |
@@ -246,7 +284,7 @@ class Router { | @@ -246,7 +284,7 @@ class Router { | ||
246 | And now, all you need to do is use Get.toNamed() to navigate your named routes, without any context (BLoC will love it), and when your app is compiled to the web, your routes will appear in the url beautifully <3 | 284 | And now, all you need to do is use Get.toNamed() to navigate your named routes, without any context (BLoC will love it), and when your app is compiled to the web, your routes will appear in the url beautifully <3 |
247 | 285 | ||
248 | ```dart | 286 | ```dart |
249 | -class FirstRoute extends StatelessWidget { | 287 | +class First extends StatelessWidget { |
250 | @override | 288 | @override |
251 | Widget build(BuildContext context) { | 289 | Widget build(BuildContext context) { |
252 | return Scaffold( | 290 | return Scaffold( |
@@ -271,12 +309,37 @@ class FirstRoute extends StatelessWidget { | @@ -271,12 +309,37 @@ class FirstRoute extends StatelessWidget { | ||
271 | } | 309 | } |
272 | } | 310 | } |
273 | 311 | ||
274 | -class SecondRoute extends StatelessWidget { | 312 | +class Second extends StatelessWidget { |
313 | + @override | ||
314 | + Widget build(BuildContext context) { | ||
315 | + return Scaffold( | ||
316 | + appBar: AppBar( | ||
317 | + leading: IconButton( | ||
318 | + icon: Icon(Icons.add), | ||
319 | + onPressed: () { | ||
320 | + Get.snackbar("hi", "i am a modern snackbar"); | ||
321 | + }, | ||
322 | + ), | ||
323 | + title: Text('second Route'), | ||
324 | + ), | ||
325 | + body: Center( | ||
326 | + child: RaisedButton( | ||
327 | + child: Text('Open route'), | ||
328 | + onPressed: () { | ||
329 | + Get.toNamed("/third"); | ||
330 | + }, | ||
331 | + ), | ||
332 | + ), | ||
333 | + ); | ||
334 | + } | ||
335 | +} | ||
336 | + | ||
337 | +class Third extends StatelessWidget { | ||
275 | @override | 338 | @override |
276 | Widget build(BuildContext context) { | 339 | Widget build(BuildContext context) { |
277 | return Scaffold( | 340 | return Scaffold( |
278 | appBar: AppBar( | 341 | appBar: AppBar( |
279 | - title: Text("Second Route"), | 342 | + title: Text("Third Route"), |
280 | ), | 343 | ), |
281 | body: Center( | 344 | body: Center( |
282 | child: RaisedButton( | 345 | child: RaisedButton( |
-
Please register or login to post a comment