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
7
import 'package:signature/signature.dart' ;
@@ -77,16 +78,20 @@ class FormBuilderSignaturePad extends FormBuilderField<Uint8List> {
77
78
width: width,
78
79
decoration: BoxDecoration (
79
80
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
+ ),
83
88
),
84
89
child: state.enabled
85
90
? GestureDetector (
86
91
onHorizontalDragUpdate: (_) {},
87
92
onVerticalDragUpdate: (_) {},
88
93
child: Signature (
89
- controller: state._controller ,
94
+ controller: state.effectiveController ,
90
95
width: width,
91
96
height: height,
92
97
backgroundColor: backgroundColor,
@@ -100,7 +105,7 @@ class FormBuilderSignaturePad extends FormBuilderField<Uint8List> {
100
105
TextButton .icon (
101
106
onPressed: state.enabled
102
107
? () {
103
- state._controller .clear ();
108
+ state.effectiveController .clear ();
104
109
field.didChange (null );
105
110
}
106
111
: null ,
@@ -127,31 +132,41 @@ class _FormBuilderSignaturePadState
127
132
extends FormBuilderFieldState <FormBuilderSignaturePad , Uint8List > {
128
133
SignatureController _controller;
129
134
135
+ SignatureController get effectiveController => _controller;
136
+
130
137
@override
131
138
void initState () {
132
139
super .initState ();
133
140
_controller = widget.controller ?? SignatureController ();
134
- _controller .addListener (() async {
141
+ effectiveController .addListener (() async {
135
142
requestFocus ();
136
- final _value = await _controller.toImage () != null
137
- ? await _controller.toPngBytes ()
138
- : null ;
143
+ final _value = await _getControllerValue ();
139
144
didChange (_value);
140
145
});
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 ();
141
162
}
142
163
143
164
@override
144
165
void dispose () {
145
166
// Dispose the _controller when initState created it
146
167
if (null == widget.controller) {
147
- _controller .dispose ();
168
+ effectiveController ? .dispose ();
148
169
}
149
170
super .dispose ();
150
171
}
151
-
152
- @override
153
- void reset () {
154
- _controller? .clear ();
155
- super .reset ();
156
- }
157
172
}
0 commit comments