Skip to content

Commit 6e90a09

Browse files
committed
Merge branch 'skipReadOnly' into version_4
2 parents fa2857f + ada7b2a commit 6e90a09

File tree

3 files changed

+21
-65
lines changed

3 files changed

+21
-65
lines changed

example/lib/sources/complete_form.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CompleteFormState extends State<CompleteForm> {
3434
children: <Widget>[
3535
FormBuilder(
3636
key: _formKey,
37+
skipReadOnly: true,
3738
autovalidateMode: AutovalidateMode.disabled,
3839
initialValue: {
3940
'movie_rating': 5,
@@ -48,6 +49,7 @@ class CompleteFormState extends State<CompleteForm> {
4849
name: 'searchable_dropdown',
4950
items: allCountries,
5051
onChanged: _onChanged,
52+
readOnly: true,
5153
),
5254
FormBuilderLocationField(
5355
name: 'location',
@@ -57,6 +59,7 @@ class CompleteFormState extends State<CompleteForm> {
5759
SizedBox(height: 15),
5860
FormBuilderFilterChip(
5961
name: 'filter_chip',
62+
readOnly: true,
6063
decoration: const InputDecoration(
6164
labelText: 'Select many options',
6265
),

lib/src/form_builder.dart

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,6 @@ import 'package:flutter_form_builder/flutter_form_builder.dart';
33

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

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

49+
/// Whether the form should ignore submitting values from readOnly fields.
50+
/// This behavior is common in HTML forms where readonly values are not
51+
/// submitted when the form is submitted.
52+
///
53+
/// If true, in the final form value, there will be no values for readOnly
54+
/// fields
55+
final bool skipReadOnly;
56+
11257
const FormBuilder({
11358
Key key,
11459
@required this.child,
@@ -117,6 +62,7 @@ class FormBuilder extends StatefulWidget {
11762
this.autovalidateMode,
11863
this.onWillPop,
11964
this.initialValue = const {},
65+
this.skipReadOnly = false,
12066
}) : super(key: key);
12167

12268
static FormBuilderState of(BuildContext context) =>

lib/src/form_builder_field.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,15 @@ class FormBuilderFieldState<T> extends FormFieldState<T> with AfterInitMixin {
116116
@override
117117
void save() {
118118
super.save();
119-
_formBuilderState?.setInternalFieldValue(
120-
widget.name, widget.valueTransformer?.call(value) ?? value);
119+
if (_formBuilderState.widget.skipReadOnly) {
120+
if (!readOnly) {
121+
_formBuilderState?.setInternalFieldValue(
122+
widget.name, widget.valueTransformer?.call(value) ?? value);
123+
}
124+
} else {
125+
_formBuilderState?.setInternalFieldValue(
126+
widget.name, widget.valueTransformer?.call(value) ?? value);
127+
}
121128
}
122129

123130
void setTouchedHandler() {

0 commit comments

Comments
 (0)