Skip to content

Commit 3baa90d

Browse files
committed
2 parents 28299b5 + 0b2c8ed commit 3baa90d

File tree

4 files changed

+61
-35
lines changed

4 files changed

+61
-35
lines changed

lib/src/fields/form_builder_checkbox_group.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ class _FormBuilderCheckboxGroupState<T>
143143
focusColor: widget.focusColor,
144144
checkColor: widget.checkColor,
145145
materialTapTargetSize: widget.materialTapTargetSize,
146-
disabled: widget.disabled,
146+
disabled: _readOnly
147+
? widget.options.map((e) => e.value).toList()
148+
: widget.disabled,
147149
hoverColor: widget.hoverColor,
148150
tristate: widget.tristate,
149151
wrapAlignment: widget.wrapAlignment,

lib/src/fields/form_builder_phone_field.dart

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class FormBuilderPhoneFieldState extends State<FormBuilderPhoneField> {
129129
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
130130
String _initialValue;
131131
Country _selectedDialogCountry;
132+
bool phoneNumberValid = true;
132133

133134
String get fullNumber {
134135
// When there is no phone number text, the field is empty -- the country
@@ -151,30 +152,37 @@ class FormBuilderPhoneFieldState extends State<FormBuilderPhoneField> {
151152
_effectiveController = widget.controller ?? TextEditingController();
152153
_selectedDialogCountry = CountryPickerUtils.getCountryByIsoCode(
153154
widget.defaultSelectedCountryIsoCode);
154-
_parsePhone();
155+
_validatePhoneNumber(_initialValue, setVal: true);
155156
}
156157

157-
Future<void> _parsePhone() async {
158-
if (_initialValue != null && _initialValue.isNotEmpty) {
158+
void _validatePhoneNumber(String val, {bool setVal = false}) async {
159+
if (val != null && val.isNotEmpty) {
159160
try {
160-
var parseResult = await PhoneNumber().parse(_initialValue);
161-
print(parseResult);
162-
if (parseResult != null) {
161+
final result = await PhoneNumber().parse(val);
162+
if (result != null) {
163163
setState(() {
164-
_selectedDialogCountry = CountryPickerUtils.getCountryByPhoneCode(
165-
parseResult['country_code']);
164+
phoneNumberValid = true;
165+
if (setVal) {
166+
_selectedDialogCountry = CountryPickerUtils.getCountryByPhoneCode(
167+
result['country_code']);
168+
_effectiveController.text = result['national_number'];
169+
}
166170
});
167-
_effectiveController.text = parseResult['national_number'];
168171
}
169-
} catch (error) {
170-
print(error);
171-
_effectiveController.text = _initialValue.replaceFirst('+', '');
172+
} on PlatformException catch (_) {
173+
setState(() {
174+
phoneNumberValid = false;
175+
});
172176
}
173-
setState(() {});
177+
} else {
178+
setState(() {
179+
phoneNumberValid = true;
180+
});
174181
}
175182
}
176183

177184
void _invokeChange(FormFieldState field) {
185+
_validatePhoneNumber(fullNumber);
178186
field.didChange(fullNumber);
179187
widget.onChanged?.call(fullNumber);
180188
}
@@ -187,8 +195,14 @@ class FormBuilderPhoneFieldState extends State<FormBuilderPhoneField> {
187195
key: _fieldKey,
188196
initialValue: _initialValue,
189197
autovalidate: widget.autovalidate,
190-
validator: (val) =>
191-
FormBuilderValidators.validateValidators(val, widget.validators),
198+
validator: (val) {
199+
if (phoneNumberValid) {
200+
return FormBuilderValidators.validateValidators(
201+
val, widget.validators);
202+
} else {
203+
return 'This field requires a valid phone number.';
204+
}
205+
},
192206
onSaved: (val) {
193207
var transformed;
194208
if (widget.valueTransformer != null) {

lib/src/fields/form_builder_signature_pad.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
108108
key: _fieldKey,
109109
enabled: !_readOnly,
110110
initialValue: _initialValue,
111-
validator: (val) =>
112-
FormBuilderValidators.validateValidators(val, widget.validators),
111+
validator: (val) {
112+
if (_savedValue != null && val == null) return;
113+
FormBuilderValidators.validateValidators(val, widget.validators);
114+
},
113115
onSaved: (val) {
116+
if (_savedValue != null && val == null) return;
114117
var transformed;
115118
if (widget.valueTransformer != null) {
116119
transformed = widget.valueTransformer(val);
@@ -141,7 +144,7 @@ class _FormBuilderSignaturePadState extends State<FormBuilderSignaturePad> {
141144
elevation: 0,
142145
color: widget.backgroundColor,
143146
child: Image.memory(
144-
_savedValue,
147+
_savedValue ?? Uint8List(0),
145148
height: widget.height,
146149
width: widget.width,
147150
),

lib/src/widgets/image_source_sheet.dart

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class ImageSourceBottomSheet extends StatelessWidget {
3939
final Widget cameraLabel;
4040
final Widget galleryLabel;
4141
final EdgeInsets bottomSheetPadding;
42+
43+
bool _isPickingImage = false;
4244

4345
ImageSourceBottomSheet({
4446
Key key,
@@ -57,6 +59,8 @@ class ImageSourceBottomSheet extends StatelessWidget {
5759
super(key: key);
5860

5961
Future<void> _onPickImage(ImageSource source) async {
62+
if(_isPickingImage) return;
63+
_isPickingImage = true;
6064
final imagePicker = ImagePicker();
6165
final pickedFile = await imagePicker.getImage(
6266
source: source,
@@ -65,6 +69,7 @@ class ImageSourceBottomSheet extends StatelessWidget {
6569
imageQuality: imageQuality,
6670
preferredCameraDevice: preferredCameraDevice,
6771
);
72+
_isPickingImage = false;
6873
if (null != pickedFile) {
6974
if (kIsWeb) {
7075
if (null != onImage) {
@@ -84,22 +89,24 @@ class ImageSourceBottomSheet extends StatelessWidget {
8489

8590
@override
8691
Widget build(BuildContext context) {
87-
return Container(
88-
padding: bottomSheetPadding,
89-
child: Wrap(
90-
children: <Widget>[
91-
ListTile(
92-
leading: cameraIcon,
93-
title: cameraLabel,
94-
onTap: () => _onPickImage(ImageSource.camera),
95-
),
96-
ListTile(
97-
leading: galleryIcon,
98-
title: galleryLabel,
99-
onTap: () => _onPickImage(ImageSource.gallery),
100-
)
101-
],
102-
),
92+
return WillPopScope(
93+
onWillPop: () async => !_isPickingImage,
94+
child: Container(
95+
padding: bottomSheetPadding,
96+
child: Wrap(
97+
children: <Widget>[
98+
ListTile(
99+
leading: cameraIcon,
100+
title: cameraLabel,
101+
onTap: () => _onPickImage(ImageSource.camera),
102+
),
103+
ListTile(
104+
leading: galleryIcon,
105+
title: galleryLabel,
106+
onTap: () => _onPickImage(ImageSource.gallery),
107+
)
108+
],
109+
),
103110
);
104111
}
105112
}

0 commit comments

Comments
 (0)