@@ -3,7 +3,14 @@ import 'package:flutter/rendering.dart';
3
3
import 'package:flutter/widgets.dart' ;
4
4
import 'package:flutter_form_builder/flutter_form_builder.dart' ;
5
5
6
- class FormBuilderCheckbox extends FormBuilderField <bool > {
6
+ class FormBuilderCheckbox extends StatefulWidget {
7
+ final String attribute;
8
+ final List <FormFieldValidator > validators;
9
+ final bool initialValue;
10
+ final bool readOnly;
11
+ final InputDecoration decoration;
12
+ final ValueChanged onChanged;
13
+ final ValueTransformer valueTransformer;
7
14
final bool leadingInput;
8
15
9
16
final Widget label;
@@ -12,6 +19,7 @@ class FormBuilderCheckbox extends FormBuilderField<bool> {
12
19
final Color checkColor;
13
20
final MaterialTapTargetSize materialTapTargetSize;
14
21
final bool tristate;
22
+ final FormFieldSetter onSaved;
15
23
final EdgeInsets contentPadding;
16
24
final Color focusColor;
17
25
final Color hoverColor;
@@ -22,57 +30,64 @@ class FormBuilderCheckbox extends FormBuilderField<bool> {
22
30
23
31
FormBuilderCheckbox ({
24
32
Key key,
25
- @required String attribute,
26
- bool readOnly = false ,
27
- AutovalidateMode autovalidateMode,
28
- bool enabled = true ,
29
- bool initialValue,
30
- InputDecoration decoration = const InputDecoration (),
31
- ValueChanged <bool > onChanged,
32
- FormFieldSetter <bool > onSaved,
33
- ValueTransformer <bool > valueTransformer,
34
- List <FormFieldValidator <bool >> validators = const [],
33
+ @required this .attribute,
35
34
@required this .label,
35
+ this .initialValue,
36
+ this .validators = const [],
37
+ this .readOnly = false ,
38
+ this .decoration = const InputDecoration (),
39
+ this .onChanged,
40
+ this .valueTransformer,
36
41
this .leadingInput = false ,
37
42
this .activeColor,
38
43
this .checkColor,
39
44
this .materialTapTargetSize,
40
45
this .tristate = false ,
41
- this .contentPadding = EdgeInsets .zero,
46
+ this .onSaved,
47
+ this .contentPadding = const EdgeInsets .all (0.0 ),
42
48
this .focusColor,
43
49
this .hoverColor,
44
50
this .focusNode,
45
51
this .autoFocus = false ,
46
52
this .mouseCursor,
47
53
this .visualDensity,
48
- }) : super (
49
- key: key,
50
- attribute: attribute,
51
- readOnly: readOnly,
52
- autovalidateMode: autovalidateMode,
53
- enabled: enabled,
54
- initialValue: initialValue,
55
- decoration: decoration,
56
- onChanged: onChanged,
57
- onSaved: onSaved,
58
- valueTransformer: valueTransformer,
59
- validators: validators,
60
- );
54
+ }) : super (key: key);
61
55
62
56
@override
63
57
_FormBuilderCheckboxState createState () => _FormBuilderCheckboxState ();
64
58
}
65
59
66
- class _FormBuilderCheckboxState
67
- extends FormBuilderFieldState <FormBuilderCheckbox , bool , bool > {
68
- Widget _checkbox (FormFieldState <bool > field) {
60
+ class _FormBuilderCheckboxState extends State <FormBuilderCheckbox > {
61
+ bool _readOnly = false ;
62
+ final GlobalKey <FormFieldState > _fieldKey = GlobalKey <FormFieldState >();
63
+ FormBuilderState _formState;
64
+ bool _initialValue;
65
+
66
+ @override
67
+ void initState () {
68
+ _formState = FormBuilder .of (context);
69
+ _formState? .registerFieldKey (widget.attribute, _fieldKey);
70
+ _initialValue = widget.initialValue ??
71
+ ((_formState? .initialValue? .containsKey (widget.attribute) ?? false )
72
+ ? _formState.initialValue[widget.attribute]
73
+ : null );
74
+ super .initState ();
75
+ }
76
+
77
+ @override
78
+ void dispose () {
79
+ _formState? .unregisterFieldKey (widget.attribute);
80
+ super .dispose ();
81
+ }
82
+
83
+ Widget _checkbox (FormFieldState <dynamic > field) {
69
84
return Checkbox (
70
85
value: (field.value == null && ! widget.tristate) ? false : field.value,
71
86
activeColor: widget.activeColor,
72
87
checkColor: widget.checkColor,
73
88
materialTapTargetSize: widget.materialTapTargetSize,
74
89
tristate: widget.tristate,
75
- onChanged: readOnly
90
+ onChanged: _readOnly
76
91
? null
77
92
: (bool value) {
78
93
FocusScope .of (context).requestFocus (FocusNode ());
@@ -88,29 +103,40 @@ class _FormBuilderCheckboxState
88
103
);
89
104
}
90
105
91
- Widget _leading (FormFieldState <bool > field) {
106
+ Widget _leading (FormFieldState <dynamic > field) {
92
107
if (widget.leadingInput) return _checkbox (field);
93
108
return null ;
94
109
}
95
110
96
- Widget _trailing (FormFieldState <bool > field) {
111
+ Widget _trailing (FormFieldState <dynamic > field) {
97
112
if (! widget.leadingInput) return _checkbox (field);
98
113
return null ;
99
114
}
100
115
101
116
@override
102
117
Widget build (BuildContext context) {
103
- return FormField <bool >(
104
- key: fieldKey,
105
- enabled: widget.enabled,
106
- initialValue: initialValue,
107
- autovalidateMode: widget.autovalidateMode,
108
- validator: (val) => validate (val),
109
- onSaved: (val) => save (val),
110
- builder: (FormFieldState <bool > field) {
118
+ _readOnly = _formState? .readOnly == true || widget.readOnly;
119
+
120
+ return FormField (
121
+ key: _fieldKey,
122
+ enabled: ! _readOnly,
123
+ initialValue: _initialValue,
124
+ validator: (val) =>
125
+ FormBuilderValidators .validateValidators (val, widget.validators),
126
+ onSaved: (val) {
127
+ var transformed;
128
+ if (widget.valueTransformer != null ) {
129
+ transformed = widget.valueTransformer (val);
130
+ _formState? .setAttributeValue (widget.attribute, transformed);
131
+ } else {
132
+ _formState? .setAttributeValue (widget.attribute, val);
133
+ }
134
+ widget.onSaved? .call (transformed ?? val);
135
+ },
136
+ builder: (FormFieldState <dynamic > field) {
111
137
return InputDecorator (
112
138
decoration: widget.decoration.copyWith (
113
- enabled: widget.enabled ,
139
+ enabled: ! _readOnly ,
114
140
errorText: field.errorText,
115
141
),
116
142
child: ListTile (
@@ -120,7 +146,7 @@ class _FormBuilderCheckboxState
120
146
title: widget.label,
121
147
leading: _leading (field),
122
148
trailing: _trailing (field),
123
- onTap: readOnly
149
+ onTap: _readOnly
124
150
? null
125
151
: () {
126
152
FocusScope .of (context).requestFocus (FocusNode ());
0 commit comments