Showing
4 changed files
with
58 additions
and
11 deletions
| @@ -66,7 +66,7 @@ class GetPageRoute<T> extends PageRoute<T> { | @@ -66,7 +66,7 @@ class GetPageRoute<T> extends PageRoute<T> { | ||
| 66 | 66 | ||
| 67 | final Alignment alignment; | 67 | final Alignment alignment; |
| 68 | 68 | ||
| 69 | - final List<GetPageMiddleware> middlewares; | 69 | + final List<GetMiddleware> middlewares; |
| 70 | 70 | ||
| 71 | @override | 71 | @override |
| 72 | final Color barrierColor; | 72 | final Color barrierColor; |
| @@ -23,7 +23,7 @@ class GetPage { | @@ -23,7 +23,7 @@ class GetPage { | ||
| 23 | final bool fullscreenDialog; | 23 | final bool fullscreenDialog; |
| 24 | final RouteSettings settings; | 24 | final RouteSettings settings; |
| 25 | final List<GetPage> children; | 25 | final List<GetPage> children; |
| 26 | - final List<GetPageMiddleware> middlewares; | 26 | + final List<GetMiddleware> middlewares; |
| 27 | 27 | ||
| 28 | const GetPage({ | 28 | const GetPage({ |
| 29 | @required this.name, | 29 | @required this.name, |
| 1 | import 'package:flutter/cupertino.dart'; | 1 | import 'package:flutter/cupertino.dart'; |
| 2 | import '../../../get.dart'; | 2 | import '../../../get.dart'; |
| 3 | 3 | ||
| 4 | -/// The Page Middlewares | ||
| 5 | -/// The Functions will be called in this order | ||
| 6 | -/// (( [onPageCalled] -> [onBindingsStart] -> [onPageBuildStart] -> | ||
| 7 | -/// [onPageBuilt] -> [onPageDispose] )) | ||
| 8 | -abstract class GetPageMiddleware { | 4 | +abstract class _RouteMiddleware { |
| 9 | /// The Order of the Middlewares to run. | 5 | /// The Order of the Middlewares to run. |
| 6 | + /// | ||
| 7 | + /// {@tool snippet} | ||
| 8 | + /// This Middewares will be called in this order. | ||
| 9 | + /// ```dart | ||
| 10 | + /// final middlewares = [ | ||
| 11 | + /// GetMiddleware(priority: 2), | ||
| 12 | + /// GetMiddleware(priority: 5), | ||
| 13 | + /// GetMiddleware(priority: 4), | ||
| 14 | + /// GetMiddleware(priority: -8), | ||
| 15 | + /// ]; | ||
| 16 | + /// ``` | ||
| 17 | + /// -8 => 2 => 4 => 5 | ||
| 18 | + /// {@end-tool} | ||
| 10 | int priority; | 19 | int priority; |
| 11 | 20 | ||
| 12 | /// This function will be the first thing to call when this Page is called | 21 | /// This function will be the first thing to call when this Page is called |
| @@ -21,11 +30,12 @@ abstract class GetPageMiddleware { | @@ -21,11 +30,12 @@ abstract class GetPageMiddleware { | ||
| 21 | /// {@end-tool} | 30 | /// {@end-tool} |
| 22 | String redirect(); | 31 | String redirect(); |
| 23 | 32 | ||
| 24 | - ///This function will be called right before the [Bindings] are initialize. | 33 | + /// This function will be called right before the [Bindings] are initialize. |
| 25 | /// Here you can change [Bindings] for this page | 34 | /// Here you can change [Bindings] for this page |
| 26 | List<Bindings> onBindingsStart(List<Bindings> bindings); | 35 | List<Bindings> onBindingsStart(List<Bindings> bindings); |
| 27 | 36 | ||
| 28 | /// This function will be called right after the [Bindings] are initialize. | 37 | /// This function will be called right after the [Bindings] are initialize. |
| 38 | + /// Here you can change the Page to build | ||
| 29 | GetPageBuilder onPageBuildStart(GetPageBuilder page); | 39 | GetPageBuilder onPageBuildStart(GetPageBuilder page); |
| 30 | 40 | ||
| 31 | // Get the built page | 41 | // Get the built page |
| @@ -34,17 +44,50 @@ abstract class GetPageMiddleware { | @@ -34,17 +44,50 @@ abstract class GetPageMiddleware { | ||
| 34 | void onPageDispose(); | 44 | void onPageDispose(); |
| 35 | } | 45 | } |
| 36 | 46 | ||
| 47 | +/// The Page Middlewares. | ||
| 48 | +/// The Functions will be called in this order | ||
| 49 | +/// (( [redirect] -> [onBindingsStart] -> [onPageBuildStart] -> | ||
| 50 | +/// [onPageBuilt] -> [onPageDispose] )) | ||
| 51 | +class GetMiddleware implements _RouteMiddleware { | ||
| 52 | + @override | ||
| 53 | + int priority = 0; | ||
| 54 | + | ||
| 55 | + GetMiddleware({this.priority}); | ||
| 56 | + | ||
| 57 | + @override | ||
| 58 | + List<Bindings> onBindingsStart(List<Bindings> bindings) => bindings; | ||
| 59 | + | ||
| 60 | + @override | ||
| 61 | + GetPageBuilder onPageBuildStart(GetPageBuilder page) => page; | ||
| 62 | + @override | ||
| 63 | + Widget onPageBuilt(Widget page) => page; | ||
| 64 | + | ||
| 65 | + @override | ||
| 66 | + void onPageDispose() {} | ||
| 67 | + | ||
| 68 | + @override | ||
| 69 | + String redirect() => ''; | ||
| 70 | +} | ||
| 71 | + | ||
| 37 | class MiddlewareRunner { | 72 | class MiddlewareRunner { |
| 38 | MiddlewareRunner(this._middlewares); | 73 | MiddlewareRunner(this._middlewares); |
| 39 | 74 | ||
| 40 | - final List<GetPageMiddleware> _middlewares; | 75 | + final List<GetMiddleware> _middlewares; |
| 76 | + | ||
| 77 | + List<GetMiddleware> _getMiddlewares() { | ||
| 78 | + if (_middlewares != null) { | ||
| 79 | + _middlewares.sort((a, b) => a.priority.compareTo(b.priority)); | ||
| 80 | + return _middlewares; | ||
| 81 | + } | ||
| 82 | + return <GetMiddleware>[]; | ||
| 83 | + } | ||
| 41 | 84 | ||
| 42 | - List<GetPageMiddleware> _getMiddlewares() { | 85 | + List<GetMiddleware> getMiddlewares() { |
| 43 | if (_middlewares != null) { | 86 | if (_middlewares != null) { |
| 44 | _middlewares.sort((a, b) => a.priority.compareTo(b.priority)); | 87 | _middlewares.sort((a, b) => a.priority.compareTo(b.priority)); |
| 45 | return _middlewares; | 88 | return _middlewares; |
| 46 | } | 89 | } |
| 47 | - return <GetPageMiddleware>[]; | 90 | + return <GetMiddleware>[]; |
| 48 | } | 91 | } |
| 49 | 92 | ||
| 50 | String runOnPageCalled() { | 93 | String runOnPageCalled() { |
test/navigation/middleware_test.dart
0 → 100644
-
Please register or login to post a comment