Skip to content

Commit f5a128b

Browse files
committed
Use signature package instead of self-maintained
1 parent d04bda6 commit f5a128b

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

lib/src/fields/form_builder_signature_pad.dart

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'dart:typed_data';
22

33
import 'package:flutter/material.dart';
4+
import 'package:flutter/scheduler.dart';
45
import 'package:flutter/widgets.dart';
56
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';
88

99
class FormBuilderSignaturePad extends StatefulWidget {
1010
final String attribute;
@@ -14,15 +14,19 @@ class FormBuilderSignaturePad extends StatefulWidget {
1414
final InputDecoration decoration;
1515
final ValueTransformer valueTransformer;
1616
final ValueChanged onChanged;
17+
final FormFieldSetter onSaved;
1718

19+
@Deprecated("Set points within SignatureController")
1820
final List<Point> points;
1921
final double width;
2022
final double height;
2123
final Color backgroundColor;
24+
@Deprecated("Set penColor within SignatureController")
2225
final Color penColor;
26+
@Deprecated("Set penStrokeWidth within SignatureController")
2327
final double penStrokeWidth;
2428
final String clearButtonText;
25-
final FormFieldSetter onSaved;
29+
final SignatureController controller;
2630

2731
FormBuilderSignaturePad({
2832
Key key,
@@ -41,6 +45,7 @@ class FormBuilderSignaturePad extends StatefulWidget {
4145
this.valueTransformer,
4246
this.onChanged,
4347
this.onSaved,
48+
this.controller,
4449
}) : super(key: key);
4550

4651
@override
@@ -50,20 +55,40 @@ class FormBuilderSignaturePad extends StatefulWidget {
5055

5156
class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
5257
bool _readOnly = false;
53-
Uint8List _value;
54-
List<Point> _points;
58+
Uint8List _initialValue;
5559
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
56-
final GlobalKey<SignatureState> _signatureKey = GlobalKey<SignatureState>();
5760
FormBuilderState _formState;
61+
SignatureController _effectiveController;
5862

5963
@override
6064
void initState() {
6165
_formState = FormBuilder.of(context);
6266
_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+
});
6483
super.initState();
6584
}
6685

86+
_getControllerValue() async {
87+
return await _effectiveController.toImage() != null
88+
? await _effectiveController.toPngBytes()
89+
: null;
90+
}
91+
6792
@override
6893
void dispose() {
6994
_formState?.unregisterFieldKey(widget.attribute);
@@ -77,7 +102,7 @@ class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
77102
return FormField<Uint8List>(
78103
key: _fieldKey,
79104
enabled: !_readOnly,
80-
initialValue: _value,
105+
initialValue: _initialValue,
81106
validator: (val) {
82107
for (int i = 0; i < widget.validators.length; i++) {
83108
if (widget.validators[i](val) != null)
@@ -111,24 +136,10 @@ class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
111136
child: GestureDetector(
112137
onVerticalDragUpdate: (_) {},
113138
child: Signature(
114-
key: _signatureKey,
115-
points: _points,
116139
width: widget.width,
117140
height: widget.height,
118141
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,
132143
),
133144
),
134145
),
@@ -137,12 +148,8 @@ class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
137148
Expanded(child: SizedBox()),
138149
FlatButton.icon(
139150
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);
146153
},
147154
label: Text(
148155
widget.clearButtonText,

pubspec.lock

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,14 @@ packages:
199199
name: image_picker
200200
url: "https://pub.dartlang.org"
201201
source: hosted
202-
version: "0.6.5+3"
202+
version: "0.6.6+1"
203+
image_picker_platform_interface:
204+
dependency: transitive
205+
description:
206+
name: image_picker_platform_interface
207+
url: "https://pub.dartlang.org"
208+
source: hosted
209+
version: "1.0.0"
203210
intl:
204211
dependency: "direct main"
205212
description:
@@ -312,6 +319,13 @@ packages:
312319
url: "https://pub.dartlang.org"
313320
source: hosted
314321
version: "2.4.0"
322+
plugin_platform_interface:
323+
dependency: transitive
324+
description:
325+
name: plugin_platform_interface
326+
url: "https://pub.dartlang.org"
327+
source: hosted
328+
version: "1.0.2"
315329
pool:
316330
dependency: transitive
317331
description:
@@ -368,6 +382,13 @@ packages:
368382
url: "https://pub.dartlang.org"
369383
source: hosted
370384
version: "0.2.3"
385+
signature:
386+
dependency: "direct main"
387+
description:
388+
name: signature
389+
url: "https://pub.dartlang.org"
390+
source: hosted
391+
version: "3.1.0"
371392
sky_engine:
372393
dependency: transitive
373394
description: flutter

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ dependencies:
1616
flutter_chips_input: ^1.8.1
1717
datetime_picker_formfield: ^1.0.0
1818
flutter_colorpicker: ^0.3.4
19-
# signature: ^2.0.0 - Doesn't implement onChanged, submitted PR. Meanwhile using local clone with onChanged
19+
signature: ^3.1.0
2020
validators: ^2.0.0+1
2121
date_range_picker: ^1.0.6
2222
flutter_touch_spin: ^1.0.1
23-
image_picker: ^0.6.5+3
23+
image_picker: ^0.6.6+1
2424
rating_bar: ^0.2.0
2525

2626
dev_dependencies:

0 commit comments

Comments
 (0)