1
1
import 'dart:typed_data' ;
2
2
3
3
import 'package:flutter/material.dart' ;
4
+ import 'package:flutter/scheduler.dart' ;
4
5
import 'package:flutter/widgets.dart' ;
5
6
import 'package:flutter_form_builder/flutter_form_builder.dart' ;
6
- import 'package:flutter_form_builder/src/widgets/signature.dart' ;
7
- // import 'package:signature/signature.dart';
7
+ import 'package:signature/signature.dart' ;
8
8
9
9
class FormBuilderSignaturePad extends StatefulWidget {
10
10
final String attribute;
@@ -14,15 +14,19 @@ class FormBuilderSignaturePad extends StatefulWidget {
14
14
final InputDecoration decoration;
15
15
final ValueTransformer valueTransformer;
16
16
final ValueChanged onChanged;
17
+ final FormFieldSetter onSaved;
17
18
19
+ @Deprecated ("Set points within SignatureController" )
18
20
final List <Point > points;
19
21
final double width;
20
22
final double height;
21
23
final Color backgroundColor;
24
+ @Deprecated ("Set penColor within SignatureController" )
22
25
final Color penColor;
26
+ @Deprecated ("Set penStrokeWidth within SignatureController" )
23
27
final double penStrokeWidth;
24
28
final String clearButtonText;
25
- final FormFieldSetter onSaved ;
29
+ final SignatureController controller ;
26
30
27
31
FormBuilderSignaturePad ({
28
32
Key key,
@@ -41,6 +45,7 @@ class FormBuilderSignaturePad extends StatefulWidget {
41
45
this .valueTransformer,
42
46
this .onChanged,
43
47
this .onSaved,
48
+ this .controller,
44
49
}) : super (key: key);
45
50
46
51
@override
@@ -50,20 +55,40 @@ class FormBuilderSignaturePad extends StatefulWidget {
50
55
51
56
class _FormBuilderSignaturePadState extends State <FormBuilderSignaturePad > {
52
57
bool _readOnly = false ;
53
- Uint8List _value;
54
- List <Point > _points;
58
+ Uint8List _initialValue;
55
59
final GlobalKey <FormFieldState > _fieldKey = GlobalKey <FormFieldState >();
56
- final GlobalKey <SignatureState > _signatureKey = GlobalKey <SignatureState >();
57
60
FormBuilderState _formState;
61
+ SignatureController _effectiveController;
58
62
59
63
@override
60
64
void initState () {
61
65
_formState = FormBuilder .of (context);
62
66
_formState? .registerFieldKey (widget.attribute, _fieldKey);
63
- _points = widget.points;
67
+ _effectiveController = widget.controller ??
68
+ SignatureController (
69
+ points: widget.controller? .points ?? widget.points,
70
+ penColor: widget.controller? .penColor ?? widget.penColor,
71
+ penStrokeWidth:
72
+ widget.controller? .penStrokeWidth ?? widget.penStrokeWidth,
73
+ );
74
+ SchedulerBinding .instance.addPostFrameCallback ((Duration duration) async {
75
+ _initialValue = await _getControllerValue ();
76
+ });
77
+ _effectiveController.addListener (() async {
78
+ FocusScope .of (context).requestFocus (FocusNode ());
79
+ var value = await _getControllerValue ();
80
+ _fieldKey.currentState.didChange (value);
81
+ if (widget.onChanged != null ) widget.onChanged (value);
82
+ });
64
83
super .initState ();
65
84
}
66
85
86
+ _getControllerValue () async {
87
+ return await _effectiveController.toImage () != null
88
+ ? await _effectiveController.toPngBytes ()
89
+ : null ;
90
+ }
91
+
67
92
@override
68
93
void dispose () {
69
94
_formState? .unregisterFieldKey (widget.attribute);
@@ -77,7 +102,7 @@ class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
77
102
return FormField <Uint8List >(
78
103
key: _fieldKey,
79
104
enabled: ! _readOnly,
80
- initialValue: _value ,
105
+ initialValue: _initialValue ,
81
106
validator: (val) {
82
107
for (int i = 0 ; i < widget.validators.length; i++ ) {
83
108
if (widget.validators[i](val) != null )
@@ -111,24 +136,10 @@ class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
111
136
child: GestureDetector (
112
137
onVerticalDragUpdate: (_) {},
113
138
child: Signature (
114
- key: _signatureKey,
115
- points: _points,
116
139
width: widget.width,
117
140
height: widget.height,
118
141
backgroundColor: widget.backgroundColor,
119
- penColor: widget.penColor,
120
- penStrokeWidth: widget.penStrokeWidth,
121
- onChanged: (points) async {
122
- FocusScope .of (context).requestFocus (FocusNode ());
123
- var signature =
124
- await _signatureKey.currentState.exportBytes ();
125
- setState (() {
126
- _value = signature;
127
- _points = _signatureKey.currentState.exportPoints ();
128
- });
129
- field.didChange (_value);
130
- if (widget.onChanged != null ) widget.onChanged (_value);
131
- },
142
+ controller: _effectiveController,
132
143
),
133
144
),
134
145
),
@@ -137,12 +148,8 @@ class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
137
148
Expanded (child: SizedBox ()),
138
149
FlatButton .icon (
139
150
onPressed: () {
140
- _signatureKey.currentState.clear ();
141
- setState (() {
142
- _points = _signatureKey.currentState.exportPoints ();
143
- _value = null ;
144
- });
145
- field.didChange (_value);
151
+ _effectiveController.clear ();
152
+ field.didChange (null );
146
153
},
147
154
label: Text (
148
155
widget.clearButtonText,
0 commit comments