Skip to content

Commit f6bf74a

Browse files
committed
Created new constructors for password & textField and removed default
constructor for FormBuilderInput
1 parent 8e0cea3 commit f6bf74a

File tree

4 files changed

+83
-48
lines changed

4 files changed

+83
-48
lines changed

.idea/workspace.xml

Lines changed: 15 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/main.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,29 @@ class MyHomePage extends StatelessWidget {
4646
require: true,
4747
min: 18,
4848
),
49-
FormBuilderInput(
49+
FormBuilderInput.textField(
5050
type: FormBuilderInput.TYPE_EMAIL,
5151
attribute: "email",
5252
label: "Email",
5353
require: true,
5454
),
55-
FormBuilderInput(
55+
FormBuilderInput.textField(
5656
type: FormBuilderInput.TYPE_URL,
5757
attribute: "url",
5858
label: "URL",
5959
require: true,
6060
),
61+
FormBuilderInput.textField(
62+
type: FormBuilderInput.TYPE_PHONE,
63+
attribute: "phone",
64+
label: "Phone",
65+
//require: true,
66+
),
67+
FormBuilderInput.password(
68+
attribute: "password",
69+
label: "Password",
70+
//require: true,
71+
),
6172
FormBuilderInput.datePicker(
6273
label: "Date of Birth",
6374
attribute: "dob",

lib/src/form_builder.dart

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class _FormBuilderState extends State<FormBuilder> {
4141
return Form(
4242
key: _formKey,
4343
onChanged: widget.onChanged,
44-
//TODO: Allow user to update field value based on changes in others
44+
//TODO: Allow user to update field value based on changes in others (e.g. Summations)
4545
onWillPop: widget.onWillPop,
4646
autovalidate: widget.autovalidate,
4747
child: ListView(
@@ -120,6 +120,14 @@ class _FormBuilderState extends State<FormBuilder> {
120120
if (formControl.min != null &&
121121
num.tryParse(value) < formControl.min)
122122
return "${formControl.label} should not be less than ${formControl.min}";
123+
}else{
124+
if (formControl.max != null &&
125+
value.length > formControl.max)
126+
return "${formControl.label} should have ${formControl.max} character(s) or less";
127+
if (formControl.min != null &&
128+
value.length < formControl.min)
129+
return "${formControl.label} should have ${formControl.min} character(s) or more";
130+
123131
}
124132

125133
if (formControl.type == FormBuilderInput.TYPE_EMAIL &&
@@ -330,18 +338,21 @@ class _FormBuilderState extends State<FormBuilder> {
330338
),
331339
),
332340
field.hasError
333-
? Padding(padding: EdgeInsets.only(top: 5.0),child: Text(
334-
field.errorText,
335-
style: TextStyle(
336-
color: Theme.of(context).errorColor,
337-
),
338-
),)
341+
? Padding(
342+
padding: EdgeInsets.only(top: 5.0),
343+
child: Text(
344+
field.errorText,
345+
style: TextStyle(
346+
color: Theme.of(context).errorColor,
347+
),
348+
),
349+
)
339350
: SizedBox(),
340351
formControl.hasHint()
341352
? Text(
342-
formControl.hint,
343-
style: Theme.of(context).textTheme.caption,
344-
)
353+
formControl.hint,
354+
style: Theme.of(context).textTheme.caption,
355+
)
345356
: SizedBox(),
346357
],
347358
),
@@ -416,8 +427,7 @@ class _FormBuilderState extends State<FormBuilder> {
416427
Row(
417428
children: <Widget>[
418429
Expanded(
419-
child:
420-
Text(formControl.label ?? formControl.hint),
430+
child: Text(formControl.label ?? formControl.hint),
421431
),
422432
SyStepper(
423433
value: field.value ?? 0,
@@ -515,7 +525,7 @@ class _FormBuilderState extends State<FormBuilder> {
515525
},
516526
builder: (FormFieldState<dynamic> field) {
517527
return Column(children: [
518-
Row(
528+
Row( //TODO: Extract as CheckboxWithLabel to separate widget
519529
children: <Widget>[
520530
Expanded(
521531
child: Text(
@@ -524,13 +534,14 @@ class _FormBuilderState extends State<FormBuilder> {
524534
),
525535
),
526536
Checkbox(
527-
value: field.value ?? false,
528-
onChanged: (bool value) {
529-
setState(() {
530-
formControls[count].value = value;
531-
});
532-
field.didChange(value);
533-
})
537+
value: field.value ?? false,
538+
onChanged: (bool value) {
539+
setState(() {
540+
formControls[count].value = value;
541+
});
542+
field.didChange(value);
543+
},
544+
),
534545
],
535546
),
536547
field.hasError
@@ -620,6 +631,7 @@ class _FormBuilderState extends State<FormBuilder> {
620631
children: <Widget>[
621632
Checkbox(
622633
value: formControls[count].options[i].value,
634+
//FIXME: Checkbox initial value should be picked from formControl value (field.initialValue)
623635
onChanged: (bool value) {
624636
setState(() {
625637
formControls[count].options[i].value = value;
@@ -633,7 +645,7 @@ class _FormBuilderState extends State<FormBuilder> {
633645
},
634646
),
635647
Expanded(
636-
child: Text(formControls[count].options[i].label),
648+
child: Text("${formControls[count].options[i].label}"),
637649
),
638650
],
639651
));

lib/src/form_builder_input.dart

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,31 @@ class FormBuilderInput {
4545
SuggestionsCallback suggestionsCallback;
4646
ItemBuilder itemBuilder;
4747

48-
FormBuilderInput({
48+
FormBuilderInput.textField({
4949
@required this.label,
5050
@required this.type,
5151
@required this.attribute,
5252
this.hint,
53-
this.options,
5453
this.value,
5554
this.require = false,
5655
this.validator,
5756
this.min,
5857
this.max,
59-
this.divisions,
60-
this.firstDate,
61-
this.lastDate,
62-
this.suggestionsCallback,
63-
this.itemBuilder,
64-
6558
});
6659

60+
FormBuilderInput.password({
61+
@required this.label,
62+
@required this.attribute,
63+
this.hint,
64+
this.value,
65+
this.require = false,
66+
this.validator,
67+
this.min,
68+
this.max,
69+
}){
70+
type = FormBuilderInput.TYPE_PASSWORD;
71+
}
72+
6773
FormBuilderInput.typeAhead({
6874
@required this.label,
6975
@required this.attribute,
@@ -86,7 +92,8 @@ class FormBuilderInput {
8692
this.max,
8793
this.require = false,
8894
this.validator,
89-
}): assert(min == null || max == null || min <= max, "Min cannot be higher than Max") {
95+
}) : assert(min == null || max == null || min <= max,
96+
"Min cannot be higher than Max") {
9097
type = FormBuilderInput.TYPE_NUMBER;
9198
}
9299

@@ -100,7 +107,8 @@ class FormBuilderInput {
100107
this.step,
101108
this.require = false,
102109
this.validator,
103-
}): assert(min == null || max == null || min <= max, "Min cannot be higher than Max") {
110+
}) : assert(min == null || max == null || min <= max,
111+
"Min cannot be higher than Max") {
104112
type = FormBuilderInput.TYPE_STEPPER;
105113
}
106114

@@ -114,7 +122,8 @@ class FormBuilderInput {
114122
this.hint,
115123
this.require = false,
116124
this.validator,
117-
}): assert(max > value || value == null, "Initial value cannot be higher than Max") {
125+
}) : assert(max > value || value == null,
126+
"Initial value cannot be higher than Max") {
118127
type = FormBuilderInput.TYPE_RATE;
119128
}
120129

@@ -176,7 +185,7 @@ class FormBuilderInput {
176185
this.value,
177186
this.require = false,
178187
this.validator,
179-
}) {
188+
}) : assert(value == null || value is List) {
180189
type = FormBuilderInput.TYPE_CHECKBOX_LIST;
181190
}
182191

@@ -206,7 +215,7 @@ class FormBuilderInput {
206215
type = FormBuilderInput.TYPE_TIME_PICKER;
207216
}
208217

209-
hasHint(){
218+
hasHint() {
210219
return hint != null;
211220
}
212221
}

0 commit comments

Comments
 (0)