Skip to content

Commit 8386619

Browse files
committed
Improvements to dirty check for FormBuilderField - fixes autovalidate only when dirty
1 parent c03dfaa commit 8386619

File tree

5 files changed

+88
-27
lines changed

5 files changed

+88
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [4.0.0-alpha.5] - 08-Jul-2020
2+
* Improvements to dirty check for FormBuilderField - fixes autovalidate only when dirty
3+
14
## [4.0.0-alpha.4] - 04-Jul-2020
25
* Added static getter for FormBuilderLocalizations delegate
36
* Fix issue where setting app localization is required for built-in validation to work

example/lib/home_page.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class HomePage extends StatelessWidget {
1616
ListTile(
1717
title: Text('Complete Form'),
1818
trailing: Icon(CupertinoIcons.right_chevron),
19-
onTap: () => Navigator.of(context)
20-
.push(MaterialPageRoute(builder: (context) => CodePage(
21-
title: 'Complete Form',
22-
child: CompleteForm(),
23-
sourceFilePath: 'lib/sources/complete_form.dart',
24-
))),
19+
onTap: () => Navigator.of(context).push(MaterialPageRoute(
20+
builder: (context) => CodePage(
21+
title: 'Complete Form',
22+
child: CompleteForm(),
23+
sourceFilePath: 'lib/sources/complete_form.dart',
24+
))),
2525
),
2626
Divider(),
2727
],

lib/src/form_builder.dart

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,69 @@ import 'package:flutter/material.dart';
22

33
typedef ValueTransformer<T> = dynamic Function(T value);
44

5+
/*class FormBuilder extends Form {
6+
@override
7+
final VoidCallback onChanged;
8+
@override
9+
final WillPopCallback onWillPop;
10+
@override
11+
final Widget child;
12+
@override
13+
final bool autovalidate;
14+
final bool readOnly;
15+
final Map<String, dynamic> initialValue;
16+
17+
const FormBuilder({
18+
Key key,
19+
@required this.child,
20+
this.autovalidate = false,
21+
this.onWillPop,
22+
this.onChanged,
23+
this.readOnly,
24+
this.initialValue = const {},
25+
}) : super(
26+
key: key,
27+
autovalidate: autovalidate,
28+
child: child,
29+
onChanged: onChanged,
30+
onWillPop: onWillPop,
31+
);
32+
33+
static FormBuilderState of(BuildContext context) =>
34+
context.findAncestorStateOfType<FormBuilderState>();
35+
}
36+
37+
class FormBuilderState extends FormState {
38+
@override
39+
FormBuilder get widget => super.widget;
40+
41+
Map<String, dynamic> _value;
42+
43+
Map<String, dynamic> get value => {...widget.initialValue ?? {}, ..._value};
44+
45+
Map<String, dynamic> get initialValue => widget.initialValue;
46+
47+
bool get readOnly => widget.readOnly;
48+
49+
@override
50+
void initState() {
51+
super.initState();
52+
// _fieldKeys = {};
53+
_value = {};
54+
}
55+
56+
void updateFormAttributeValue(String attribute, dynamic value) {
57+
setState(() {
58+
_value = {..._value, attribute: value};
59+
});
60+
}
61+
62+
bool saveAndValidate() {
63+
save();
64+
return validate();
65+
}
66+
}*/
67+
568
class FormBuilder extends StatefulWidget {
669
//final BuildContext context;
770
final Function(Map<String, dynamic>) onChanged;
@@ -97,10 +160,6 @@ class FormBuilderState extends State<FormBuilder> {
97160

98161
void reset() {
99162
_formKey.currentState.reset();
100-
/*_fieldKeys.forEach((mapKey, fieldKey) {
101-
// print("Reseting $mapKey");
102-
fieldKey.currentState.reset();
103-
});*/
104163
}
105164

106165
@override
@@ -111,10 +170,7 @@ class FormBuilderState extends State<FormBuilder> {
111170
autovalidate: widget.autovalidate,
112171
onWillPop: widget.onWillPop,
113172
onChanged: () {
114-
if (widget.onChanged != null) {
115-
save();
116-
widget.onChanged(value);
117-
}
173+
widget.onChanged?.call(value);
118174
},
119175
);
120176
}

lib/src/form_builder_field.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
4949

5050
bool get readOnly => _readOnly;
5151

52-
bool get isPristine => value == _initialValue;
52+
bool get pristine => !_dirty;
5353

54-
bool get autovalidate => !isPristine && widget.autovalidate;
54+
bool get dirty => !_dirty;
55+
56+
// Only autovalidate if dirty
57+
bool get autovalidate => dirty && widget.autovalidate;
5558

5659
GlobalKey<FormFieldState> get fieldKey => _fieldKey;
5760

@@ -63,6 +66,8 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
6366

6467
bool _readOnly = false;
6568

69+
bool _dirty = false;
70+
6671
T _initialValue;
6772

6873
@override
@@ -87,24 +92,21 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
8792
@override
8893
void save() {
8994
super.save();
90-
if (widget.valueTransformer != null) {
91-
var transformed = widget.valueTransformer(value);
92-
FormBuilder.of(context)
93-
?.updateFormAttributeValue(widget.attribute, transformed);
94-
} else {
95-
_formBuilderState?.updateFormAttributeValue(widget.attribute, value);
96-
}
95+
_formBuilderState?.updateFormAttributeValue(
96+
widget.attribute, widget.valueTransformer?.call(value) ?? value);
9797
}
9898

9999
@override
100100
void didChange(T value) {
101+
setState(() {
102+
_dirty = true;
103+
});
101104
super.didChange(value);
102105
widget.onChanged?.call(value);
103106
}
104107

105108
@override
106109
void reset() {
107-
// print("Resetting ${widget.attribute}");
108110
super.reset();
109111
setValue(_initialValue);
110112
if (widget.onReset != null) {
@@ -117,7 +119,7 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
117119
return super.validate() && widget.decoration?.errorText == null;
118120
}
119121

120-
void requestFocus() {
122+
/*void requestFocus() {
121123
FocusScope.of(context).requestFocus(FocusNode());
122-
}
124+
}*/
123125
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_form_builder
22
description: Package to build Material Form with fields like TextField, DropDown, Switches etc. with ability to create custom FormFields and composability and reuse validation functions.
3-
version: 4.0.0-alpha.4
3+
version: 4.0.0-alpha.5
44
homepage: https://github.com/danvick/flutter_form_builder
55

66
environment:

0 commit comments

Comments
 (0)