routes.dart
7.74 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import 'package:flutter/material.dart';
import 'bottomsheet.dart';
import 'dialog.dart';
import 'snack.dart';
import 'getroute.dart';
class Get {
static Get _get;
static GlobalKey<NavigatorState> key = new GlobalKey<NavigatorState>();
///Use Get.to instead of Navigator.push, Get.off instead of Navigator.pushReplacement,
///Get.offAll instead of Navigator.pushAndRemoveUntil. For named routes just add "named"
///after them. Example: Get.toNamed, Get.offNamed, and Get.AllNamed.
///To return to the previous screen, use Get.back().
///No need to pass any context to Get, just put the name of the route inside
///the parentheses and the magic will occur.
factory Get() {
if (_get == null) _get = Get._();
return _get;
}
///Use Get.to instead of Navigator.push, Get.off instead of Navigator.pushReplacement,
///Get.offAll instead of Navigator.pushAndRemoveUntil. For named routes just add "named"
///after them. Example: Get.toNamed, Get.offNamed, and Get.AllNamed.
///To return to the previous screen, use Get.back().
///No need to pass any context to Get, just put the name of the route inside
///the parentheses and the magic will occur.
Get._();
/// It replaces Navigator.push, but needs no context, and it doesn't have the Navigator.push
/// routes rebuild bug present in Flutter. If for some strange reason you want the default behavior
/// of rebuilding every app after a route, use rebuildRoutes = true as the parameter.
static to(Widget page,
{bool rebuildRoutes = false, Transition transition = Transition.fade}) {
return key.currentState.push(
GetRoute(opaque: rebuildRoutes, child: page, transition: transition));
}
/// It replaces Navigator.pushNamed, but needs no context, and it doesn't have the Navigator.pushNamed
/// routes rebuild bug present in Flutter. If for some strange reason you want the default behavior
/// of rebuilding every app after a route, use rebuildRoutes = true as the parameter.
static toNamed(String page, {arguments}) {
return key.currentState.pushNamed(page, arguments: arguments);
}
/// It replaces Navigator.pushReplacementNamed, but needs no context.
static offNamed(String page, {arguments}) {
return key.currentState.pushReplacementNamed(page, arguments: arguments);
}
/// It replaces Navigator.popUntil, but needs no context.
static until(String page, predicate) {
return key.currentState.popUntil(predicate);
}
/// It replaces Navigator.pushAndRemoveUntil, but needs no context.
static offUntil(page, predicate) {
return key.currentState.pushAndRemoveUntil(page, predicate);
}
/// It replaces Navigator.pushNamedAndRemoveUntil, but needs no context.
static offNamedUntil(page, predicate) {
return key.currentState.pushNamedAndRemoveUntil(page, predicate);
}
/// It replaces Navigator.removeRoute, but needs no context.
static removeRoute(route) {
return key.currentState.removeRoute(route);
}
/// It replaces Navigator.pushNamedAndRemoveUntil, but needs no context.
static offAllNamed(
String newRouteName,
RoutePredicate predicate, {
arguments,
}) {
return key.currentState
.pushNamedAndRemoveUntil(newRouteName, predicate, arguments: arguments);
}
/// It replaces Navigator.pop, but needs no context.
static back({result}) {
return key.currentState.pop(result);
}
/// It replaces Navigator.pushReplacement, but needs no context, and it doesn't have the Navigator.pushReplacement
/// routes rebuild bug present in Flutter. If for some strange reason you want the default behavior
/// of rebuilding every app after a route, use rebuildRoutes = true as the parameter.
static off(Widget page,
{bool rebuildRoutes = false,
Transition transition = Transition.rightToLeft}) {
return key.currentState.pushReplacement(
GetRoute(opaque: rebuildRoutes, child: page, transition: transition));
}
/// It replaces Navigator.pushAndRemoveUntil, but needs no context
static offAll(Widget page, RoutePredicate predicate,
{bool rebuildRoutes = false,
Transition transition = Transition.rightToLeft}) {
return key.currentState.pushAndRemoveUntil(
GetRoute(opaque: rebuildRoutes, child: page, transition: transition),
predicate);
}
/// Show a dialog. You can choose color and opacity of background
static Future<T> dialog<T>(
Widget child, {
bool barrierDismissible = true,
// WidgetBuilder builder,
bool useRootNavigator = true,
}) {
assert(child == null
// || builder == null
);
assert(useRootNavigator != null);
// assert(debugCheckHasMaterialLocalizations(context));
final ThemeData theme =
Theme.of(Get.key.currentContext, shadowThemeOnly: true);
return getShowGeneralDialog(
pageBuilder: (BuildContext buildContext, Animation<double> animation,
Animation<double> secondaryAnimation) {
final Widget pageChild = child; // ?? Builder(builder: builder);
return SafeArea(
child: Builder(builder: (BuildContext context) {
return theme != null
? Theme(data: theme, child: pageChild)
: pageChild;
}),
);
},
barrierDismissible: barrierDismissible,
barrierLabel: MaterialLocalizations.of(Get.key.currentContext)
.modalBarrierDismissLabel,
barrierColor: Colors.black54,
transitionDuration: const Duration(milliseconds: 150),
// transitionBuilder: _buildMaterialDialogTransitions,
useRootNavigator: useRootNavigator,
);
}
static defaultDialog(
{Color color,
double opacity = 0.2,
String title = "Alert dialog",
Widget content,
Widget cancel,
Widget confirm}) {
final child = DefaultDialogGet(
color: color,
opacity: opacity,
title: title,
content: content,
cancel: cancel,
confirm: confirm,
);
dialog(child);
}
static Future<T> bottomSheet<T>({
@required WidgetBuilder builder,
Color backgroundColor,
double elevation,
ShapeBorder shape,
Clip clipBehavior,
Color barrierColor,
bool isScrollControlled = false,
bool useRootNavigator = false,
bool isDismissible = true,
bool enableDrag = true,
}) {
assert(builder != null);
assert(isScrollControlled != null);
assert(useRootNavigator != null);
assert(isDismissible != null);
assert(enableDrag != null);
return Get.key.currentState.push<T>(GetModalBottomSheetRoute<T>(
builder: builder,
theme: Theme.of(Get.key.currentContext, shadowThemeOnly: true),
isScrollControlled: isScrollControlled,
barrierLabel: MaterialLocalizations.of(Get.key.currentContext)
.modalBarrierDismissLabel,
backgroundColor: backgroundColor,
elevation: elevation,
shape: shape,
clipBehavior: clipBehavior,
isDismissible: isDismissible,
modalBarrierColor: barrierColor,
enableDrag: enableDrag,
));
}
static snackbar(title, message,
{Color colorText, Duration duration, SnackPosition snackPosition}) {
return GetBar(
titleText: Text(
title,
style: TextStyle(
color: colorText ?? Theme.of(Get.key.currentContext).accentColor,
fontWeight: FontWeight.w800,
fontSize: 16),
),
messageText: Text(
message,
style: TextStyle(
color: colorText ?? Theme.of(Get.key.currentContext).accentColor,
fontWeight: FontWeight.w300,
fontSize: 14),
),
snackPosition: snackPosition ?? SnackPosition.TOP,
borderRadius: 15,
margin: EdgeInsets.symmetric(horizontal: 10),
duration: duration ?? Duration(seconds: 3),
barBlur: 7.0,
backgroundColor: Colors.grey.withOpacity(0.2),
)..show();
}
}