-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_widget.dart
825 lines (784 loc) · 25.2 KB
/
custom_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:romlinks_frontend/logic/controller.dart';
import 'package:romlinks_frontend/logic/services/filestorage_service.dart';
import 'package:romlinks_frontend/logic/services/user_service.dart';
import 'package:romlinks_frontend/views/theme.dart';
//! container
class ContainerW extends StatelessWidget {
const ContainerW(this.child,
{this.height = 100,
this.width = 100,
this.padding = const EdgeInsets.all(10),
this.margin = const EdgeInsets.all(10),
this.color = ThemeApp.secondaryColor,
this.marginLeft = true,
this.marginRight = true,
this.tag,
Key? key})
: super(key: key);
final Widget child;
final double height;
final double width;
final EdgeInsets padding;
final EdgeInsets margin;
final Color color;
final bool marginLeft;
final bool marginRight;
final String? tag;
@override
Widget build(BuildContext context) {
EdgeInsets customMargin = margin.copyWith(left: (marginLeft) ? margin.left : 0, right: (marginRight) ? margin.right : 0);
return HeroW(
Container(
child: child,
height: height,
width: width,
padding: padding,
margin: customMargin,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(10),
),
),
tag: tag,
);
}
}
//! chip
class ChipW extends StatelessWidget {
const ChipW({this.text, this.icon, this.color = ThemeApp.secondaryColor, required this.height, required this.width, Key? key}) : super(key: key);
final String? text;
final IconData? icon;
final Color color;
final double height;
final double width;
@override
Widget build(BuildContext context) {
return ContainerW(
(text == null) ? Icon(icon) : TextW(text!, singleLine: true),
color: color,
height: height,
width: width,
padding: const EdgeInsets.all(5),
margin: const EdgeInsets.all(5),
);
}
}
//! button
class ButtonW extends StatelessWidget {
ButtonW(
this.text1, {
Key? key,
required this.onTap,
this.animated = false,
this.width = 170,
this.color = ThemeApp.accentColor,
this.padding = const EdgeInsets.all(10),
this.margin = const EdgeInsets.all(10),
this.tag,
}) : super(key: key);
final String text1;
final double width;
final bool animated;
final Function onTap;
final Color color;
final EdgeInsets padding;
final EdgeInsets margin;
final String? tag;
final RxBool animation = false.obs;
@override
Widget build(BuildContext context) {
return InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () async {
if (animated) {
animation.value = true;
await onTap();
animation.value = false;
} else {
onTap();
}
},
child: ContainerW(
FittedBox(
child: Obx(
() => (!animation.value)
? TextW(text1, singleLine: true)
: const Center(
child: CircularProgressIndicator(color: ThemeApp.primaryColor),
),
),
),
color: color,
padding: padding,
margin: margin,
height: 40,
width: width,
tag: tag,
),
);
}
}
//! text
class TextW extends StatelessWidget {
const TextW(this.text, {this.singleLine = false, this.size = 20, this.big = false, Key? key}) : super(key: key);
final String text;
final double size;
final bool big;
final bool singleLine;
@override
Widget build(BuildContext context) {
// text widget
Widget textW = Text(
text,
overflow: TextOverflow.clip,
style: TextStyle(
decoration: TextDecoration.none,
fontSize: (big) ? 30 : size,
color: Colors.white,
fontWeight: FontWeight.bold,
),
maxLines: singleLine ? 1 : 20,
);
return !singleLine ? textW : FittedBox(child: textW);
}
}
//! textfield
class TextFieldW extends StatelessWidget {
TextFieldW(
this.text, {
Key? key,
this.onChanged,
this.onPressed,
this.number = false,
this.controller,
this.prefixIcon,
this.hide = false,
this.buttonIcon = Icons.add,
}) : super(key: key);
final String text;
final void Function(String)? onChanged;
final void Function()? onPressed;
final bool number;
final TextEditingController? controller;
final IconData? prefixIcon;
final bool hide;
final IconData buttonIcon;
final RxBool showPassword = false.obs;
@override
Widget build(BuildContext context) {
if (hide) showPassword.value = true;
return Padding(
padding: const EdgeInsets.only(bottom: 10),
child: SizedBox(
width: 230,
child: Obx(
() => TextField(
controller: controller,
onChanged: onChanged,
keyboardType: (number) ? TextInputType.number : null,
inputFormatters: (number) ? <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] : null,
obscureText: showPassword.value,
onSubmitted: (_) {
if (onPressed != null) onPressed!();
},
decoration: InputDecoration(
prefixIcon: (prefixIcon != null) ? Icon(prefixIcon) : null,
hintText: text,
suffixIcon: (onPressed == null && hide == false)
? null
: Padding(
padding: const EdgeInsets.only(right: 5),
child: IconButton(
splashRadius: 20,
icon: Icon(
hide
? showPassword.value
? Icons.visibility_off
: Icons.visibility
: buttonIcon,
),
onPressed: (!hide) ? onPressed : () => showPassword.value = !showPassword.value,
),
),
),
),
),
),
);
}
}
//! custom future builder
class FutureBuilderW<T> extends StatelessWidget {
const FutureBuilderW({required this.future, required this.builder, Key? key}) : super(key: key);
final Widget Function(T) builder;
final Future future;
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState != ConnectionState.done) return const Center(child: CircularProgressIndicator());
return (snapshot.data == null) ? const ErrorW(msg: "unable to get the data") : builder(snapshot.data);
},
);
}
}
//! network image with error icon
class ImageW extends StatelessWidget {
const ImageW({required this.category, required this.name, this.profileIcon = false, this.heroTag, Key? key}) : super(key: key);
final PhotoCategory category;
final String name;
final bool profileIcon;
final String? heroTag;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (category == PhotoCategory.screenshot)
? () {
dialogW(
Center(
child: Padding(
padding: const EdgeInsets.all(40),
child: SizedBox(
height: 2000,
child: AspectRatio(
aspectRatio: 9 / 19,
child: HeroW(
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.network(
FileStorageService.imgUrl(category, name),
fit: BoxFit.contain,
errorBuilder: (_, __, ___) => ErrorW(personIcon: profileIcon),
),
),
tag: heroTag,
),
),
),
),
),
);
}
: null,
child: HeroW(
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.network(
FileStorageService.imgUrl(category, name),
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => ErrorW(personIcon: profileIcon),
),
),
tag: heroTag,
),
);
}
}
//! custom scaffold with padding
class ScaffoldW extends StatelessWidget {
ScaffoldW(this.child, {this.scroll = false, this.auth = false, this.button, Key? key}) : super(key: key);
final Widget child;
final bool scroll;
final bool auth;
final Widget? button;
final UserController _userController = Get.find();
@override
Widget build(BuildContext context) {
return (_userController.token.isEmpty && auth)
? Material(
color: ThemeApp.primaryColor,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const TextW("Unauthorized"),
const SpaceW(big: true),
ButtonW("Auth", onTap: () => Get.toNamed("/auth")),
],
),
),
)
: SafeArea(
child: Scaffold(
floatingActionButton: button,
body: SizedBox(
height: context.height,
width: context.width,
child: (!scroll)
? Padding(padding: const EdgeInsets.all(20), child: Center(child: child))
: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(20),
child: MaxWidthW(child),
),
),
),
),
);
}
}
//! display a list of suggestion
class SuggestionW extends StatelessWidget {
const SuggestionW({required this.suggestion, required this.onTap, Key? key}) : super(key: key);
final RxList suggestion;
final Function(String) onTap;
@override
Widget build(BuildContext context) {
return Obx(
() => (suggestion.isNotEmpty)
? ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 45, maxWidth: 800),
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: suggestion.length,
itemBuilder: (BuildContext context, int index) {
return ButtonW(
suggestion[index],
width: 90,
color: ThemeApp.secondaryColor,
margin: const EdgeInsets.fromLTRB(10, 0, 10, 10),
onTap: () => onTap(suggestion[index]),
);
},
),
)
: const SizedBox.shrink(),
);
}
}
//! dialog widget
class DialogW extends StatelessWidget {
const DialogW(
{required this.button1,
required this.text1,
required this.child,
required this.height,
required this.width,
this.button2,
this.text2,
this.alignment = Alignment.bottomCenter,
this.tag,
this.text3,
this.button3,
this.text4,
this.button4,
Key? key})
: super(key: key);
final Function button1;
final Function? button2;
final String text1;
final String? text2;
final Widget child;
final Alignment alignment;
final double height;
final double width;
final String? tag;
final String? text3;
final Function? button3;
final String? text4;
final Function? button4;
@override
Widget build(BuildContext context) {
return Align(
alignment: alignment,
child: SafeArea(
minimum: const EdgeInsets.all(5),
child: SizedBox(
height: height,
width: width,
child: HeroW(
Material(
color: Colors.transparent,
child: ContainerW(
Stack(
children: [
SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(child: child),
),
),
Align(
alignment: Alignment.bottomCenter,
child: (button3 == null)
? Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(child: ButtonW(text2 ?? "Close", onTap: () => (button2 != null) ? button2!() : Get.close(1))),
Expanded(child: ButtonW(text1, onTap: () => button1())),
],
)
: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(child: ButtonW(text2 ?? "Close", onTap: () => (button2 != null) ? button2!() : Get.close(1))),
Expanded(child: ButtonW(text1, onTap: () => button1())),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(child: ButtonW(text3!, onTap: () => button3!())),
if (text4 != null) Expanded(child: ButtonW(text4!, onTap: () => button4!())),
],
),
],
),
),
],
),
margin: const EdgeInsets.all(20),
color: ThemeApp.primaryColor,
),
),
tag: tag,
),
),
),
);
}
}
//! display a list of screenshot
class ScreenshotW extends StatelessWidget {
const ScreenshotW(this.image, {this.removeImage, this.useHero = true, Key? key}) : super(key: key);
final RxList<String> image;
final Function? removeImage;
final bool useHero;
@override
Widget build(BuildContext context) {
return SizedBox(
height: 250,
child: Obx(
() => ListView.builder(
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: (image.isNotEmpty) ? image.length : 1,
itemBuilder: (BuildContext context, int index) {
return SizedBox(
height: 250,
width: 120,
child: Stack(
children: [
AspectRatio(
aspectRatio: 9 / 19,
child: Padding(
padding: EdgeInsets.fromLTRB((index == 0) ? 0 : 10, 10, 10, 10),
child: ImageW(
heroTag: (image.isNotEmpty && useHero) ? image[index] : null,
category: PhotoCategory.screenshot,
name: (image.isNotEmpty) ? image[index] : "",
),
),
),
if (removeImage != null && image.isNotEmpty)
Padding(
padding: const EdgeInsets.all(10),
child: Align(
alignment: Alignment.bottomRight,
child: IconButton(
onPressed: () => removeImage!(index),
iconSize: 30,
splashRadius: 20,
icon: const Icon(Icons.edit),
),
),
)
],
),
);
},
),
),
);
}
}
//! button that redirect to the auth or the profile screen depending on the auth state
class AccountButtonW extends StatelessWidget {
final UserController _userController = Get.find();
AccountButtonW({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Obx(
() => (_userController.isLogged.value)
? GestureDetector(
onTap: () => Get.toNamed("/profile"),
child: ContainerW(
(_userController.userData.value.username != "")
? ImageW(
category: PhotoCategory.profile,
name: _userController.userData.value.username,
profileIcon: true,
)
: const SizedBox.shrink(),
height: 40,
width: 40,
marginRight: false,
padding: EdgeInsets.zero,
),
)
: ButtonW(
"Log in",
margin: const EdgeInsets.fromLTRB(10, 10, 0, 10),
width: 40 * 2.2,
onTap: () => Get.toNamed("/auth"),
),
);
}
}
//! display a list of text
class TextListW extends StatelessWidget {
const TextListW(this.data, {this.vertical = true, Key? key}) : super(key: key);
final RxList<String> data;
final bool vertical;
@override
Widget build(BuildContext context) {
return Obx(
() => SizedBox(
height: vertical
? null
: (data.isNotEmpty)
? 60
: 0,
child: ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: data.length,
scrollDirection: vertical ? Axis.vertical : Axis.horizontal,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Center(
child: ContainerW(
TextW(data[index], singleLine: true),
height: 45,
width: 210,
),
);
},
),
),
);
}
}
//! display an error icon
class ErrorW extends StatelessWidget {
const ErrorW({this.msg, this.personIcon = false, Key? key}) : super(key: key);
final String? msg;
final bool personIcon;
@override
Widget build(BuildContext context) {
Icon icon = Icon((personIcon) ? Icons.person : Icons.report_problem_outlined);
return (msg == null)
? ContainerW(icon, margin: EdgeInsets.zero, padding: EdgeInsets.zero)
: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon((personIcon) ? Icons.person : Icons.report_problem_outlined),
const SpaceW(),
TextW(msg!),
],
),
);
}
}
//! space
class SpaceW extends StatelessWidget {
const SpaceW({this.big = false, Key? key}) : super(key: key);
final bool big;
@override
Widget build(BuildContext context) => SizedBox(height: (big) ? 30 : 10);
}
//! widget that define the max width
class MaxWidthW extends StatelessWidget {
const MaxWidthW(this.child, {Key? key}) : super(key: key);
final Widget child;
@override
Widget build(BuildContext context) {
return Center(
child: SizedBox(
width: 1000,
child: child,
),
);
}
}
//! hero widget
class HeroW extends StatelessWidget {
const HeroW(this.child, {this.tag, Key? key}) : super(key: key);
final Widget child;
final String? tag;
@override
Widget build(BuildContext context) => (tag != null)
? Hero(
tag: tag!,
child: child,
createRectTween: (begin, end) => CustomRectTween(begin: begin!, end: end!),
)
: child;
}
//! custom courve for the hero widget
class CustomRectTween extends RectTween {
CustomRectTween({
required Rect begin,
required Rect end,
}) : super(begin: begin, end: end);
@override
Rect lerp(double t) {
final elasticCurveValue = Curves.easeOut.transform(t);
return Rect.fromLTRB(
lerpDouble(begin!.left, end!.left, elasticCurveValue)!,
lerpDouble(begin!.top, end!.top, elasticCurveValue)!,
lerpDouble(begin!.right, end!.right, elasticCurveValue)!,
lerpDouble(begin!.bottom, end!.bottom, elasticCurveValue)!,
);
}
}
//! custom page view
class PageViewW extends StatelessWidget {
PageViewW(this.child, {Key? key, this.page = 2}) : super(key: key);
final List<Widget> child;
final PageController controller = PageController();
final RxInt currentPage = 0.obs;
final int page;
@override
Widget build(BuildContext context) {
controller.addListener(() {
currentPage.value = controller.page!.round();
});
return Stack(
children: [
PageView(
physics: const BouncingScrollPhysics(),
controller: controller,
children: child,
),
Align(
alignment: Alignment.bottomCenter,
child: ContainerW(
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Obx(
() => Container(
height: 14,
width: 14,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(3)),
color: (currentPage.value == 0) ? ThemeApp.accentColor : ThemeApp.primaryColor,
),
),
),
Obx(
() => Container(
height: 14,
width: 14,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(3)),
color: (currentPage.value == 1) ? ThemeApp.accentColor : ThemeApp.primaryColor,
),
),
),
if (page == 3)
Obx(
() => Container(
height: 14,
width: 14,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(3)),
color: (currentPage.value == 2) ? ThemeApp.accentColor : ThemeApp.primaryColor,
),
),
),
],
),
height: 30,
width: (page == 2) ? 60 : 80,
padding: EdgeInsets.zero,
margin: EdgeInsets.zero,
),
),
],
);
}
}
//! display the username and the profile picture of a user
class UserW extends StatelessWidget {
const UserW(this.username, {Key? key}) : super(key: key);
final String username;
@override
Widget build(BuildContext context) {
return ContainerW(
GestureDetector(
onTap: () {
if (Get.find<UserController>().userData.value.moderator) {
UserService.editUserPerm(username: username, perm: PermType.verified, value: true);
}
},
onLongPress: () {
if (Get.find<UserController>().userData.value.moderator) {
UserService.editUserPerm(username: username, perm: PermType.moderator, value: true);
}
},
child: Column(
children: [
SizedBox(
child: ImageW(category: PhotoCategory.profile, name: username, profileIcon: true),
height: 100,
width: 100,
),
const SpaceW(),
TextW(username),
],
),
),
height: 155,
width: 140,
);
}
}
//! custom dialog
void dialogW(Widget child) {
navigator!.push(
PageRouteBuilder(
opaque: false,
barrierDismissible: true,
barrierColor: Colors.black.withOpacity(0.9),
pageBuilder: (_, __, ___) => child,
),
);
}
//! custom snackbar
void snackbarW(String title, String? msg) {
// ignore: curly_braces_in_flow_control_structures
if (msg != null) if (Get.isSnackbarOpen == false) {
Get.snackbar(
title,
msg,
icon: title.toLowerCase().contains("error") ? const Icon(Icons.report_problem_outlined, color: ThemeApp.accentColor) : null,
shouldIconPulse: false,
colorText: Colors.white,
duration: const Duration(seconds: 5),
backgroundColor: ThemeApp.secondaryColor.withOpacity(0.9),
snackPosition: SnackPosition.BOTTOM,
margin: const EdgeInsets.symmetric(vertical: 20, horizontal: 20),
);
}
}