Skip to content

Commit 16207a1

Browse files
committed
Fix issue where initialValue not displayed on SignaturePad.
1 parent 8f87430 commit 16207a1

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

lib/src/fields/form_builder_signature_pad.dart

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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';
67
import 'package:signature/signature.dart';
@@ -77,16 +78,20 @@ class FormBuilderSignaturePad extends FormBuilderField<Uint8List> {
7778
width: width,
7879
decoration: BoxDecoration(
7980
border: border,
80-
image: state.enabled ? null : DecorationImage(
81-
image: MemoryImage(state.value),
82-
),
81+
image: (state.enabled &&
82+
(null != initialValue &&
83+
initialValue != state.value))
84+
? null
85+
: DecorationImage(
86+
image: MemoryImage(state.value),
87+
),
8388
),
8489
child: state.enabled
8590
? GestureDetector(
8691
onHorizontalDragUpdate: (_) {},
8792
onVerticalDragUpdate: (_) {},
8893
child: Signature(
89-
controller: state._controller,
94+
controller: state.effectiveController,
9095
width: width,
9196
height: height,
9297
backgroundColor: backgroundColor,
@@ -100,7 +105,7 @@ class FormBuilderSignaturePad extends FormBuilderField<Uint8List> {
100105
TextButton.icon(
101106
onPressed: state.enabled
102107
? () {
103-
state._controller.clear();
108+
state.effectiveController.clear();
104109
field.didChange(null);
105110
}
106111
: null,
@@ -127,31 +132,41 @@ class _FormBuilderSignaturePadState
127132
extends FormBuilderFieldState<FormBuilderSignaturePad, Uint8List> {
128133
SignatureController _controller;
129134

135+
SignatureController get effectiveController => _controller;
136+
130137
@override
131138
void initState() {
132139
super.initState();
133140
_controller = widget.controller ?? SignatureController();
134-
_controller.addListener(() async {
141+
effectiveController.addListener(() async {
135142
requestFocus();
136-
final _value = await _controller.toImage() != null
137-
? await _controller.toPngBytes()
138-
: null;
143+
final _value = await _getControllerValue();
139144
didChange(_value);
140145
});
146+
SchedulerBinding.instance.addPostFrameCallback((Duration duration) async {
147+
// Get initialValue or if points are set, use the points
148+
didChange(initialValue ?? await _getControllerValue());
149+
});
150+
}
151+
152+
Future<Uint8List> _getControllerValue() async {
153+
return await effectiveController.toImage() != null
154+
? await effectiveController.toPngBytes()
155+
: null;
156+
}
157+
158+
@override
159+
void reset() {
160+
effectiveController?.clear();
161+
super.reset();
141162
}
142163

143164
@override
144165
void dispose() {
145166
// Dispose the _controller when initState created it
146167
if (null == widget.controller) {
147-
_controller.dispose();
168+
effectiveController?.dispose();
148169
}
149170
super.dispose();
150171
}
151-
152-
@override
153-
void reset() {
154-
_controller?.clear();
155-
super.reset();
156-
}
157172
}

0 commit comments

Comments
 (0)