-
Notifications
You must be signed in to change notification settings - Fork 9
/
fields.dart
747 lines (693 loc) · 21.7 KB
/
fields.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
///
/// Copyright (C) 2018 Andrious Solutions
///
/// This program is free software; you can redistribute it and/or
/// modify it under the terms of the GNU General Public License
/// as published by the Free Software Foundation; either version 3
/// of the License, or any later version.
///
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Created 19 Dec 2018
///
///
import 'package:flutter/material.dart';
import 'package:flutter/services.dart'
show
Brightness,
TextCapitalization,
TextInputAction,
TextInputFormatter,
TextInputType;
import 'package:mvc_application/app.dart' show App;
typedef OnSavedFunc = Function<E>(E v);
class Field<T> extends Item {
Field({
Object key,
this.object,
String label,
dynamic value,
// TextFormField
this.controller,
this.initialValue,
this.focusNode,
this.decoration,
this.keyboardType,
this.textCapitalization,
this.textInputAction,
this.textSpan,
this.style,
this.textAlign,
this.autofocus,
this.obscureText,
this.autocorrect,
this.autovalidate,
this.maxLengthEnforced,
this.maxLines,
this.maxLength,
this.editingComplete,
this.fieldSubmitted,
this.saved,
this.validator,
this.inputFormatters,
this.enabled,
this.keyboardAppearance,
this.scrollPadding,
// Text
this.textDirection,
this.locale,
this.softWrap,
this.overflow,
this.textScaleFactor,
this.semanticsLabel,
// ListTile
this.leading,
this.title,
this.subtitle,
this.trailing,
this.isThreeLine,
this.dense,
this.contentPadding,
this.tap,
this.longPress,
this.selected,
// CheckboxListTile
this.secondary,
this.controlAffinity,
// CircleAvatar
this.backgroundColor,
this.backgroundImage,
this.foregroundColor,
this.radius,
this.minRadius,
this.maxRadius,
// Dismissible
this.child,
this.background,
this.secondaryBackground,
this.resize,
this.dismissed,
this.direction,
this.resizeDuration,
this.dismissThresholds,
this.movementDuration,
this.crossAxisEndOffset,
// CheckBox
this.changed,
this.activeColor,
this.tristate,
this.materialTapTargetSize,
}) : super(label: label, value: value) {
_key = ObjectKey(key ?? this).toString();
_checkValue = (value == null
? false
: value is String
? value.isNotEmpty
: value is bool
? value
: value is int
? value > 0
? true
: value is double ? value > 0 ? true : false : false
: false);
}
final T object;
Iterable<Item> items;
final ThemeData _theme = App.theme;
String get key => _key;
String _key;
/// TextFormField
TextEditingController controller;
String initialValue;
FocusNode focusNode;
InputDecoration decoration;
TextInputType keyboardType;
TextCapitalization textCapitalization;
TextInputAction textInputAction;
TextStyle style;
TextAlign textAlign;
bool autofocus;
bool obscureText;
bool autocorrect;
bool autovalidate;
bool maxLengthEnforced;
int maxLines;
int maxLength;
VoidCallback editingComplete;
ValueChanged<String> fieldSubmitted;
FormFieldSetter<String> saved;
FormFieldValidator<String> validator;
List<TextInputFormatter> inputFormatters;
bool enabled;
Brightness keyboardAppearance;
EdgeInsets scrollPadding;
/// Text
// final String data;
TextSpan textSpan;
// final TextStyle style;
// final TextAlign textAlign;
TextDirection textDirection;
Locale locale;
bool softWrap;
TextOverflow overflow;
double textScaleFactor;
//final int maxLines;
String semanticsLabel;
/// ListTile
Widget leading;
Widget title;
Widget subtitle;
Widget trailing;
bool isThreeLine;
bool dense;
EdgeInsetsGeometry contentPadding;
// final bool enabled;
GestureTapCallback tap;
GestureLongPressCallback longPress;
bool selected;
/// CheckboxListTile
Widget secondary;
ListTileControlAffinity controlAffinity;
/// CircleAvatar
Color backgroundColor;
Color foregroundColor;
ImageProvider backgroundImage;
double radius;
double minRadius;
double maxRadius;
/// Dismissible
Widget child;
Widget background;
Widget secondaryBackground;
VoidCallback resize;
DismissDirectionCallback dismissed;
DismissDirection direction;
Duration resizeDuration;
Map<DismissDirection, double> dismissThresholds;
Duration movementDuration;
double crossAxisEndOffset;
/// CheckBox
bool _checkValue;
ValueChanged<bool> changed;
Color activeColor;
bool tristate;
MaterialTapTargetSize materialTapTargetSize;
TextFormField _textFormField;
Text _text;
Text _richText;
ListTile _listTile;
CheckboxListTile _checkboxListTile;
CircleAvatar _circleAvatar;
Dismissible _dismissible;
Checkbox _checkbox;
TextFormField get textFormField {
if (_textFormField == null)
_textFormField = TextFormField(
key: Key('TextFormField' + _key),
controller: controller,
initialValue: initialValue ?? value,
focusNode: focusNode,
decoration: decoration ?? InputDecoration(labelText: label),
keyboardType: keyboardType,
textCapitalization: textCapitalization ?? TextCapitalization.none,
textInputAction: textInputAction ?? TextInputAction.done,
style: style,
textAlign: textAlign ?? TextAlign.start,
autofocus: autofocus ?? false,
obscureText: obscureText ?? false,
autocorrect: autocorrect ?? true,
autovalidate: autovalidate ?? false,
maxLengthEnforced: maxLengthEnforced ?? true,
maxLines: maxLines ?? 1,
maxLength: maxLength,
onEditingComplete: editingComplete ?? onEditingComplete,
onFieldSubmitted: (String v) =>
fieldSubmitted == null ? onFieldSubmitted(v) : fieldSubmitted(v),
onSaved: (String v) => saved == null ? onSaved(v) : saved(v),
validator: (String v) =>
validator == null ? onValidator(v) : validator(v),
inputFormatters: inputFormatters,
enabled: enabled,
keyboardAppearance: keyboardAppearance,
scrollPadding: scrollPadding ?? const EdgeInsets.all(20.0),
);
return _textFormField;
}
TextFormField onTextFormField({
TextEditingController controller,
String initialValue,
FocusNode focusNode,
InputDecoration decoration,
TextInputType keyboardType,
TextCapitalization textCapitalization,
TextInputAction textInputAction,
TextStyle style,
TextAlign textAlign,
bool autofocus,
bool obscureText,
bool autocorrect,
bool autovalidate,
bool maxLengthEnforced,
int maxLines,
int maxLength,
VoidCallback editingComplete,
ValueChanged<String> fieldSubmitted,
FormFieldSetter<String> saved,
FormFieldValidator<String> validator,
List<TextInputFormatter> inputFormatters,
bool enabled,
Brightness keyboardAppearance,
EdgeInsets scrollPadding,
}) {
this.controller = controller ?? this.controller;
this.initialValue = initialValue ?? value;
this.focusNode = focusNode ?? this.focusNode;
this.decoration = decoration ?? this.decoration;
this.keyboardType = keyboardType ?? this.keyboardType;
this.textCapitalization = textCapitalization ?? this.textCapitalization;
this.textInputAction = textInputAction ?? this.textInputAction;
this.style = style ?? this.style;
this.textAlign = textAlign ?? this.textAlign;
this.autofocus = autofocus ?? this.autofocus;
this.obscureText = obscureText ?? this.obscureText;
this.autocorrect = autocorrect ?? this.autocorrect;
this.autovalidate = autovalidate ?? this.autovalidate;
this.maxLengthEnforced = maxLengthEnforced ?? this.maxLengthEnforced;
this.maxLines = maxLines ?? this.maxLines;
this.maxLength = maxLength ?? this.maxLength;
this.editingComplete = editingComplete ?? this.editingComplete;
this.fieldSubmitted = fieldSubmitted ?? this.fieldSubmitted;
this.saved = saved ?? this.saved;
this.validator = validator ?? this.validator;
this.inputFormatters = inputFormatters ?? this.inputFormatters;
this.enabled = enabled ?? this.enabled;
this.keyboardAppearance = keyboardAppearance ?? this.keyboardAppearance;
this.scrollPadding = scrollPadding ?? this.scrollPadding;
TextFormField oldWidget = _textFormField;
_textFormField = null;
TextFormField newWidget = textFormField;
_textFormField = oldWidget;
return newWidget;
}
// Override to perform what happens when finished editing the field.
void onEditingComplete() {}
// Override to perform what happens when the field value is submitted.
void onFieldSubmitted(String v) {}
// What happens when the field is saved?
void onSaved(String v) {
value = v; // As an example => object?.jobTitle = value = v;
}
// Override to return a different field value when validating.
// Return null if validated. Error message if not.
String onValidator(String v) => null;
Text get text {
if (_text == null)
_text = Text(
value ?? "",
key: Key('Text' + _key),
style: style,
textAlign: textAlign,
textDirection: textDirection,
locale: locale,
softWrap: softWrap,
overflow: overflow,
textScaleFactor: textScaleFactor,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
);
return _text;
}
Text onText(
TextSpan textSpan,
TextStyle style,
TextAlign textAlign,
TextDirection textDirection,
Locale locale,
bool softWrap,
TextOverflow overflow,
double textScaleFactor,
int maxLines,
String semanticsLabel,
) {
this.style = style ?? this.style;
this.textAlign = textAlign ?? this.textAlign;
this.textDirection = textDirection ?? this.textDirection;
this.locale = locale ?? this.locale;
this.softWrap = softWrap ?? this.softWrap;
this.overflow = overflow ?? this.overflow;
this.textScaleFactor = textScaleFactor ?? this.textScaleFactor;
this.maxLines = maxLines ?? this.maxLines;
this.semanticsLabel = semanticsLabel ?? this.semanticsLabel;
Text oldWidget = _text;
_text = null;
Text newWidget;
// text getter
if (textSpan == null) {
newWidget = text;
} else {
this.textSpan = textSpan;
// richText getter
newWidget = richText;
}
_text = oldWidget;
return newWidget;
}
Text get richText {
if (textSpan == null) {
return text;
} else {
if (_richText == null)
_richText = Text.rich(
textSpan,
key: Key('Text.rich' + _key),
style: style,
textAlign: textAlign,
textDirection: textDirection,
locale: locale,
softWrap: softWrap,
overflow: overflow,
textScaleFactor: textScaleFactor,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
);
return _richText;
}
}
Text onRichText(
TextSpan textSpan,
TextStyle style,
TextAlign textAlign,
TextDirection textDirection,
Locale locale,
bool softWrap,
TextOverflow overflow,
double textScaleFactor,
int maxLines,
String semanticsLabel,
) {
this.style = style ?? this.style;
this.textAlign = textAlign ?? this.textAlign;
this.textDirection = textDirection ?? this.textDirection;
this.locale = locale ?? this.locale;
this.softWrap = softWrap ?? this.softWrap;
this.overflow = overflow ?? this.overflow;
this.textScaleFactor = textScaleFactor ?? this.textScaleFactor;
this.maxLines = maxLines ?? this.maxLines;
this.semanticsLabel = semanticsLabel ?? this.semanticsLabel;
Text oldWidget = _richText;
_richText = null;
Text newWidget;
// text getter
if (textSpan == null) {
newWidget = text;
} else {
this.textSpan = textSpan;
// richText getter
newWidget = richText;
}
_richText = oldWidget;
return newWidget;
}
ListTile get listTile {
if (_listTile == null)
_listTile = ListTile(
key: Key('ListTile' + _key),
leading: leading ?? onLeading(),
title: title ?? onTitle(),
subtitle: subtitle ?? onSubtitle(),
trailing: trailing ?? text,
isThreeLine: isThreeLine ?? false,
dense: dense,
contentPadding: contentPadding,
enabled: enabled ?? true,
onTap: tap ?? onTap,
onLongPress: longPress ?? onLongPress,
selected: selected ?? false,
);
return _listTile;
}
ListTile onListTile({
Widget leading,
Widget title,
Widget subtitle,
Widget trailing,
bool isThreeLine,
bool dense,
EdgeInsetsGeometry contentPadding,
bool enabled,
GestureTapCallback tap,
GestureLongPressCallback longPress,
bool selected,
}) {
this.leading = leading ?? this.leading;
this.title = title ?? this.title;
this.subtitle = subtitle ?? this.subtitle;
this.trailing = trailing ?? this.trailing;
this.isThreeLine = isThreeLine ?? this.isThreeLine;
this.dense = dense ?? this.dense;
this.contentPadding = contentPadding ?? this.contentPadding;
this.enabled = enabled ?? this.enabled;
this.tap = tap ?? this.tap;
this.longPress = longPress ?? this.longPress;
this.selected = selected ?? this.selected;
ListTile oldWidget = _listTile;
_listTile = null;
ListTile newWidget = listTile;
_listTile = oldWidget;
return newWidget;
}
//for LisTile
Widget onLeading() => null;
Widget onTitle() => Text(label);
// Override to produce a subtitle.
Widget onSubtitle() => null;
Widget onTrailing() => null;
// Override to place what happens when the field is tapped.
void onTap() {}
// Override to place what happens when the field is 'long' pressed.
void onLongPress() {}
CheckboxListTile get checkboxListTile {
if (_checkboxListTile == null)
_checkboxListTile = CheckboxListTile(
key: Key('CheckboxListTile' + _key),
value: _checkValue,
onChanged: (bool v) => changed == null ? onChanged(v) : changed(v),
activeColor: activeColor,
title: title ?? onTitle(),
subtitle: subtitle ?? onSubtitle(),
isThreeLine: isThreeLine ?? false,
dense: dense,
secondary: secondary ?? onSecondary(),
selected: selected ?? false,
controlAffinity: controlAffinity,
);
return _checkboxListTile;
}
CheckboxListTile onCheckboxListTile({
@required bool value,
ValueChanged<bool> onChanged,
Color activeColor,
Widget title,
Widget subtitle,
bool isThreeLine,
bool dense,
Widget secondary,
bool selected,
ListTileControlAffinity controlAffinity,
}) {
_checkValue = value ?? _checkValue;
this.changed = onChanged ?? this.changed;
this.activeColor = activeColor ?? this.activeColor;
this.title = title ?? this.title;
this.subtitle = subtitle ?? this.subtitle;
this.isThreeLine = isThreeLine ?? this.isThreeLine;
this.dense = dense ?? this.dense;
this.secondary = secondary ?? this.secondary;
this.selected = selected ?? this.selected;
this.controlAffinity = controlAffinity ?? this.controlAffinity;
CheckboxListTile oldWidget = _checkboxListTile;
_checkboxListTile = null;
CheckboxListTile newWidget = checkboxListTile;
_checkboxListTile = oldWidget;
return newWidget;
}
// A widget to display on the opposite side of the tile from the checkbox.
Widget onSecondary() => null;
CircleAvatar get circleAvatar {
if (_circleAvatar == null)
_circleAvatar = CircleAvatar(
key: Key('CircleAvatar' + _key),
child: text,
backgroundColor: backgroundColor,
backgroundImage: backgroundImage,
foregroundColor: foregroundColor,
radius: radius,
minRadius: minRadius,
maxRadius: maxRadius,
);
return _circleAvatar;
}
CircleAvatar onCircleAvatar(
Color backgroundColor,
Color foregroundColor,
ImageProvider backgroundImage,
double radius,
double minRadius,
double maxRadius,
) {
this.backgroundColor = backgroundColor ?? this.backgroundColor;
this.backgroundImage = backgroundImage ?? this.backgroundImage;
this.foregroundColor = foregroundColor ?? this.foregroundColor;
this.radius = radius ?? this.radius;
this.minRadius = minRadius ?? this.minRadius;
this.maxRadius = maxRadius ?? this.maxRadius;
CircleAvatar oldWidget = _circleAvatar;
_circleAvatar = null;
CircleAvatar newWidget = circleAvatar;
_circleAvatar = oldWidget;
return newWidget;
}
Dismissible get dismissible {
if (_dismissible == null)
_dismissible = Dismissible(
key: Key('Dismissible' + _key),
child: child ?? onChild(),
background: background ?? onBackground(),
secondaryBackground: secondaryBackground ?? onSecondaryBackground(),
onResize: resize ?? onResize,
onDismissed: (DismissDirection direction) =>
dismissed == null ? onDismissed(direction) : dismissed(direction),
direction: direction ?? DismissDirection.endToStart,
resizeDuration: resizeDuration ?? const Duration(milliseconds: 300),
dismissThresholds:
dismissThresholds ?? const <DismissDirection, double>{},
movementDuration: movementDuration ?? const Duration(milliseconds: 200),
crossAxisEndOffset: crossAxisEndOffset ?? 0.0,
);
return _dismissible;
}
Dismissible onDismissible({
Widget child,
Widget background,
Widget secondaryBackground,
VoidCallback resize,
DismissDirectionCallback dismissed,
DismissDirection direction,
Duration resizeDuration,
Map<DismissDirection, double> dismissThresholds,
Duration movementDuration,
double crossAxisEndOffset,
}) {
this.child = child ?? this.child;
this.background = background ?? this.background;
this.secondaryBackground = secondaryBackground ?? this.secondaryBackground;
this.resize = resize ?? this.resize;
this.dismissed = dismissed ?? this.dismissed;
this.direction = direction ?? this.direction;
this.resizeDuration = resizeDuration ?? this.resizeDuration;
this.dismissThresholds = dismissThresholds ?? this.dismissThresholds;
this.movementDuration = movementDuration ?? this.movementDuration;
this.crossAxisEndOffset = crossAxisEndOffset ?? this.crossAxisEndOffset;
Dismissible oldWidget = _dismissible;
_dismissible = null;
Dismissible newWidget = dismissible;
_dismissible = oldWidget;
return newWidget;
}
// Override to place a different child in the Dismissible.
Widget onChild() {
return Container(
decoration: BoxDecoration(
color: _theme.canvasColor,
border: Border(bottom: BorderSide(color: _theme.dividerColor))),
child: listTile);
}
// for Dismissible
Widget onBackground() {
return Container(
color: Colors.red,
child: const ListTile(
trailing:
const Icon(Icons.delete, color: Colors.white, size: 36.0)));
}
// Override to provide a secondary background.
Widget onSecondaryBackground() => null;
// Override to place what happens when the field is resized.
void onResize() {}
// Override to place here what happens when the field is dismissed.
void onDismissed(DismissDirection direction) {}
Checkbox get checkbox {
if (_checkbox == null)
_checkbox = Checkbox(
key: Key('Checkbox' + _key),
value: _checkValue,
tristate: tristate,
onChanged: (bool v) => changed == null ? onChanged(v) : changed(v),
activeColor: activeColor,
materialTapTargetSize: materialTapTargetSize,
);
return _checkbox;
}
Checkbox onCheckBox({
@required bool value,
ValueChanged<bool> onChanged,
Color activeColor,
bool tristate,
MaterialTapTargetSize materialTapTargetSize,
}) {
_checkValue = value ?? _checkValue;
this.changed = onChanged ?? this.changed;
this.activeColor = activeColor ?? this.activeColor;
this.tristate = tristate ?? this.tristate;
this.materialTapTargetSize =
materialTapTargetSize ?? this.materialTapTargetSize;
Checkbox oldWidget = _checkbox;
_checkbox = null;
Checkbox newWidget = checkbox;
_checkbox = oldWidget;
return newWidget;
}
void onChanged(bool value) {}
ListItems get listItems => ListItems(title: label, items: (value ?? [this]));
}
/// Item class used for contact fields which only have a [label] and a [value]
class Item {
Item({this.label, this.value});
String label;
dynamic value;
Item.fromMap(Map m) {
label = m["label"];
value = m["value"];
}
Map get toMap => {"label": label, "value": value};
}
class ListItems extends StatelessWidget {
ListItems({this.title, this.items});
final String title;
final List<Item> items;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ListTile(title: Text(title)),
Column(
children: items
.map((i) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ListTile(
title: Text(i.label ?? ""),
trailing: Text(i.value ?? ""))))
.toList())
]);
}
}