-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_base_widget.dart
264 lines (238 loc) · 8.77 KB
/
x_base_widget.dart
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:get/get.dart';
import 'package:baseX/base_x.dart';
import 'package:baseX/helper/scroll_behaviour.dart';
import 'package:baseX/model/ui/drawer_action.dart';
import 'package:baseX/model/ui/floating_action.dart';
abstract class BaseXWidget<T extends BaseXController> extends GetWidget<T> {
/// make [GetWidget.controller] to [c] as shortcut can be used on class who extended to [BaseXWidget]
T get c => controller;
String get routeName;
bool get safeArea => true;
bool get resizeToAvoidBottomInset => false;
/// non-negative value that indicate how much should touch into appbar
/// ```dart
/// @override
/// double get stackedAppBar => 15;
/// ```
double? get stackedAppBar => null;
bool get isReversedStackAppBar => false;
Color get backgroundColor => baseConstant.defaultBackgroundColor;
Widget loader() => baseConstant.defaultLoadingWidget;
Widget? appBar(BuildContext context);
Widget? body(BuildContext context);
Widget? overlayChild() => null;
Widget envBar() => baseConstant.envBar;
/// Override [floatingAction] with [FloatingAction] in your widget page with enable [hasFloatingButton] = true
/// ```dart
/// @override
/// FloatingAction? get floatingAction => FloatingAction(
/// floatingActionButton: FloatingActionButton(
/// onPressed: () {
/// //TODO Add your onPressed code here!
/// },
/// backgroundColor: Colors.blue,
/// child: Icon(Icons.add),
/// ),
/// );
/// ```
FloatingAction? get floatingAction => baseConstant.floatingAction;
/// Override [drawerAction] with [DrawerAction] in your widget page with enable [hasDrawer] = true
/// ```dart
/// @override
/// DrawerAction get drawerAction => DrawerAction(
/// drawerPosition: DrawerPosition.Right,
/// drawer: Drawer(
/// child: ListView(
/// padding: EdgeInsets.zero,
/// children: [
/// DrawerHeader(
/// decoration: BoxDecoration(
/// color: Colors.blue,
/// ),
/// child: Text('Drawer Header 2'),
/// ),
/// ListTile(
/// title: Text('Item 2'),
/// onTap: () {
/// // TODO update onTap
/// },
/// ),
/// ],
/// ),
/// ),
/// );
/// ```
DrawerAction? get drawerAction => baseConstant.drawerAction;
@override
Widget build(BuildContext context) {
if (c.hasFloatingButton.value) {
assert(!(baseConstant.floatingAction == null && floatingAction == null),
'Please put your floating button to either app constant or page \n');
}
if (c.hasDrawer.value) {
assert(!(baseConstant.drawerAction == null && drawerAction == null),
'Please put your drawer to app constant \n');
}
return GetPlatform.isIOS
? _scaffoldChild(context)
: WillPopScope(onWillPop: controller.onBack, child: _scaffoldChild(context));
}
Widget _contentBody(BuildContext context) {
bool isPage = true;
if (c.page != null &&
(c.page.runtimeType is int ||
c.page.runtimeType is double ||
c.page.runtimeType is String ||
c.page.runtimeType is bool)) {
assert(
!(c.page.runtimeType is int ||
c.page.runtimeType is double ||
c.page.runtimeType is String ||
c.page.runtimeType is bool),
'Please do not re-assign new value to c.page | page in controller \n');
} else {
isPage = (c.page as BaseXWidget).routeName == routeName;
}
return Stack(
children: [
Column(
mainAxisSize: MainAxisSize.max,
children: [
if (baseConstant.position == Position.top &&
baseConstant.appEnv == Environment.Staging &&
isPage)
envBar(),
if (stackedAppBar == null)
Expanded(
child: Column(
children: [
if (appBar(context) != null) appBar(context)!,
if (body(context) != null)
Expanded(
child: hideScrollShadow(body(context)!),
),
],
),
),
if (stackedAppBar != null)
Expanded(
child: hideScrollShadow(
Stack(
children: isReversedStackAppBar
? (stackedChildren(context).reversed.toList())
: stackedChildren(context),
),
),
),
if (baseConstant.position == Position.bottom &&
baseConstant.appEnv == Environment.Staging &&
isPage)
envBar(),
],
),
if (overlayChild() != null) Positioned.fill(child: overlayChild()!),
Obx(() => controller.isLoading.value ? loader() : SizedBox.shrink()),
if (kDebugMode && routeName.isNotEmpty) _nameOfPageTag(),
],
);
}
List<Widget> stackedChildren(BuildContext context) {
return [
if (appBar(context) != null) appBar(context)!,
Positioned.fill(
top: -stackedAppBar!,
left: 0,
right: 0,
child: Column(
children: [
if (appBar(context) != null)
Visibility(
visible: false,
maintainState: true,
maintainSize: true,
maintainAnimation: true,
child: appBar(context)!,
),
if (body(context) != null) hideScrollShadow(body(context)!),
],
),
),
];
}
Widget _nameOfPageTag() {
return Positioned(
left: 5,
top: 20.0 + (safeArea ? 0 : Get.mediaQuery.viewPadding.top),
child: IgnorePointer(
child: RotationTransition(
turns: AlwaysStoppedAnimation(330 / 360),
child: Container(
padding: EdgeInsets.fromLTRB(5, 0, 5, 3),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.7),
borderRadius: BorderRadius.circular(6),
),
child: Text(
routeName.replaceAll('/', ''),
style: TextStyle(
color: Colors.red,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
),
),
);
}
Widget _scaffoldChild(BuildContext context) {
bool isDrawerLeft = true;
if (c.hasDrawer.value) {
isDrawerLeft = drawerAction!.drawerPosition == DrawerPosition.Left;
}
return Obx(
() => Scaffold(
resizeToAvoidBottomInset: resizeToAvoidBottomInset,
backgroundColor: backgroundColor,
floatingActionButton:
c.hasFloatingButton.value ? floatingAction?.floatingActionButton : null,
floatingActionButtonLocation:
c.hasFloatingButton.value ? floatingAction?.floatingActionButtonLocation : null,
floatingActionButtonAnimator:
c.hasFloatingButton.value ? floatingAction?.floatingActionButtonAnimator : null,
drawer: c.hasDrawer.value && isDrawerLeft ? drawerAction!.drawer : null,
onDrawerChanged: c.hasDrawer.value && isDrawerLeft ? drawerAction!.onDrawerChanged : null,
drawerEnableOpenDragGesture:
c.hasDrawer.value && isDrawerLeft ? drawerAction!.drawerEnableOpenDragGesture : true,
endDrawer: c.hasDrawer.value && !isDrawerLeft ? drawerAction!.drawer : null,
onEndDrawerChanged:
c.hasDrawer.value && !isDrawerLeft ? drawerAction!.onDrawerChanged : null,
endDrawerEnableOpenDragGesture:
c.hasDrawer.value && !isDrawerLeft ? drawerAction!.drawerEnableOpenDragGesture : true,
drawerDragStartBehavior:
c.hasDrawer.value ? drawerAction!.drawerDragStartBehavior : DragStartBehavior.start,
drawerEdgeDragWidth: c.hasDrawer.value ? drawerAction!.drawerEdgeDragWidth : null,
drawerScrimColor: c.hasDrawer.value ? drawerAction!.drawerScrimColor : null,
body: GestureDetector(
onHorizontalDragDown: (details) {
controller.horizontalDown = details.localPosition.dx;
},
onHorizontalDragEnd: (details) async {
if (controller.horizontalDown < 5) {
bool needPop = await controller.onBack();
if (needPop) Get.back();
}
},
child: KeyboardDismissOnTap(
dismissOnCapturedTaps: true,
child: safeArea ? SafeArea(child: _contentBody(context)) : _contentBody(context),
),
),
),
);
}
}