route_middleware.dart
2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 'package:flutter/cupertino.dart';
import '../../../get.dart';
/// The Page Middlewares
/// The Functions will be called in this order
/// (( [onPageCalled] -> [onBindingsStart] -> [onPageBuildStart] ->
/// [onPageBuilt] -> [onPageDispose] ))
abstract class GetPageMiddleware {
/// The Order of the Middlewares to run.
int priority;
/// This function will be the first thing to call when this Page is called
/// you can use it to redirect befor anything in this page happend.
/// {@tool snippet}
/// ```dart
/// GetPage onPageCalled( ) {
/// final authService = Get.find<AuthService>();
/// return authService.isAuthed ? null : '/login';
/// }
/// ```
/// {@end-tool}
String redirect();
///This function will be called right before the [Bindings] are initialize.
/// Here you can change [Bindings] for this page
List<Bindings> onBindingsStart(List<Bindings> bindings);
/// This function will be called right after the [Bindings] are initialize.
GetPageBuilder onPageBuildStart(GetPageBuilder page);
// Get the built page
Widget onPageBuilt(Widget page);
void onPageDispose();
}
class MiddlewareRunner {
MiddlewareRunner(this._middlewares);
final List<GetPageMiddleware> _middlewares;
List<GetPageMiddleware> _getMiddlewares() {
if (_middlewares != null) {
_middlewares.sort((a, b) => a.priority.compareTo(b.priority));
return _middlewares;
}
return <GetPageMiddleware>[];
}
String runOnPageCalled() {
var to = '';
_getMiddlewares().forEach((element) {
to = element.redirect();
});
if (!to.isNullOrBlank) {
Get.log('Redirect to $to');
}
return to;
}
List<Bindings> runOnBindingsStart(List<Bindings> bindings) {
_getMiddlewares().forEach((element) {
bindings = element.onBindingsStart(bindings);
});
return bindings;
}
GetPageBuilder runOnPageBuildStart(GetPageBuilder page) {
_getMiddlewares().forEach((element) {
page = element.onPageBuildStart(page);
});
return page;
}
Widget runOnPageBuilt(Widget page) {
_getMiddlewares().forEach((element) {
page = element.onPageBuilt(page);
});
return page;
}
void runOnPageDispose() =>
_getMiddlewares().forEach((element) => element.onPageDispose());
}