Skip to content

Commit 265013e

Browse files
committed
Fixed erratic keyboard behavior on TextField
1 parent 1190501 commit 265013e

File tree

5 files changed

+43
-100
lines changed

5 files changed

+43
-100
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [4.0.0-alpha.8] - 23-Jul-2020
2+
* Fixed erratic keyboard behavior on `FormBuilderTextField`
3+
* Added documentation for `FormBuilder` & `FormBuilderField` attributes
4+
* Fixed issue where `FormBuilderRadioGroup` not submitting value
5+
16
## [4.0.0-alpha.7] - 22-Jul-2020
27
* Added new field - `FormBuilderCheckboxGroup`. Closes #188,
38
* New `FormBuilderRadioGroup` implementation similar to `FormBuilderCheckboxGroup`. Fixes issue where `FormBuilderFieldOption.child` is ignored Closes #335

example/lib/sources/complete_form.dart

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ class CompleteFormState extends State<CompleteForm> {
1919
bool readOnly = false;
2020
bool showSegmentedControl = true;
2121
final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
22-
final GlobalKey<FormFieldState> _specifyTextFieldKey =
23-
GlobalKey<FormFieldState>();
22+
final GlobalKey<FormBuilderFieldState> _genderKey = GlobalKey();
23+
final GlobalKey<FormBuilderFieldState> _ageKey = GlobalKey();
24+
bool _ageHasError = false;
25+
bool _genderHasError = false;
2426

2527
final ValueChanged _onChanged = (val) => print(val);
2628
var genderOptions = ['Male', 'Female', 'Other'];
2729

2830
@override
2931
Widget build(BuildContext context) {
30-
print(Localizations.localeOf(context));
3132
return Padding(
3233
padding: EdgeInsets.all(10),
3334
child: SingleChildScrollView(
@@ -223,9 +224,22 @@ class CompleteFormState extends State<CompleteForm> {
223224
]),
224225
),
225226
FormBuilderTextField(
227+
key: _ageKey,
228+
autovalidate: true,
226229
attribute: 'age',
227-
decoration: InputDecoration(labelText: 'Age'),
228-
onChanged: _onChanged,
230+
decoration: InputDecoration(
231+
labelText: 'Age',
232+
suffixIcon: _ageHasError
233+
? Icon(Icons.error, color: Colors.red)
234+
: Icon(Icons.check, color: Colors.green),
235+
),
236+
onChanged: (val) {
237+
setState(() {
238+
print(val);
239+
_ageHasError = !_ageKey.currentState.validate();
240+
// _genderHasError = !_fbKey.currentState.fields['gender'].currentState.validate();
241+
});
242+
},
229243
// valueTransformer: (text) => num.tryParse(text),
230244
validator: FormBuilderValidators.compose([
231245
FormBuilderValidators.required(context),
@@ -237,9 +251,14 @@ class CompleteFormState extends State<CompleteForm> {
237251
textInputAction: TextInputAction.next,
238252
),
239253
FormBuilderDropdown(
254+
key: _genderKey,
255+
// autovalidate: true,
240256
attribute: 'gender',
241257
decoration: InputDecoration(
242258
labelText: 'Gender',
259+
suffix: _genderHasError
260+
? Icon(Icons.error)
261+
: Icon(Icons.check),
243262
),
244263
// initialValue: 'Male',
245264
allowClear: true,
@@ -252,6 +271,13 @@ class CompleteFormState extends State<CompleteForm> {
252271
child: Text('$gender'),
253272
))
254273
.toList(),
274+
onChanged: (val) {
275+
print(val);
276+
setState(() {
277+
_genderHasError = !_genderKey.currentState.validate();
278+
// _genderHasError = _genderKey.currentState.hasError;
279+
});
280+
},
255281
),
256282
FormBuilderTypeAhead(
257283
decoration: InputDecoration(
@@ -357,57 +383,6 @@ class CompleteFormState extends State<CompleteForm> {
357383
color: Colors.red,
358384
),
359385
),
360-
FormBuilderField(
361-
attribute: 'custom',
362-
valueTransformer: (val) {
363-
if (val == 'Other') {
364-
return _specifyTextFieldKey.currentState.value;
365-
}
366-
return val;
367-
},
368-
builder: (FormFieldState<String> field) {
369-
var languages = ['English', 'Spanish', 'Somali', 'Other'];
370-
return InputDecorator(
371-
decoration: InputDecoration(
372-
labelText: 'What\'s your preferred language?',
373-
),
374-
child: Column(
375-
children: languages
376-
.map(
377-
(lang) => Row(
378-
children: <Widget>[
379-
Radio<dynamic>(
380-
value: lang,
381-
groupValue: field.value,
382-
onChanged: (dynamic value) {
383-
field.didChange(lang);
384-
},
385-
),
386-
lang != 'Other'
387-
? Text(lang)
388-
: Expanded(
389-
child: Row(
390-
children: <Widget>[
391-
Text(
392-
lang,
393-
),
394-
SizedBox(width: 20),
395-
Expanded(
396-
child: TextFormField(
397-
key: _specifyTextFieldKey,
398-
),
399-
),
400-
],
401-
),
402-
),
403-
],
404-
),
405-
)
406-
.toList(growable: false),
407-
),
408-
);
409-
},
410-
),
411386
FormBuilderSignaturePad(
412387
decoration: InputDecoration(
413388
labelText: 'Signature',

lib/src/fields/form_builder_text_field.dart

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,16 @@ class FormBuilderTextField extends FormBuilderField {
140140
decoration: decoration,
141141
builder: (FormFieldState field) {
142142
final _FormBuilderTextFieldState state = field;
143-
final effectiveDecoration = (decoration ?? const InputDecoration())
144-
.applyDefaults(Theme.of(field.context).inputDecorationTheme);
143+
/*final effectiveDecoration = (decoration ?? const InputDecoration())
144+
.applyDefaults(Theme.of(field.context).inputDecorationTheme);*/
145145
void onChangedHandler(String value) {
146-
if (onChanged != null) {
147-
onChanged(value);
148-
}
149-
field.didChange(value);
146+
state.didChange(value);
150147
}
151148

152149
return TextField(
153150
controller: state._effectiveController,
154151
focusNode: focusNode,
155-
decoration: effectiveDecoration.copyWith(
152+
decoration: decoration.copyWith(
156153
errorText: decoration?.errorText ?? field.errorText,
157154
),
158155
keyboardType: keyboardType,
@@ -217,32 +214,11 @@ class _FormBuilderTextFieldState extends FormBuilderFieldState {
217214
super.initState();
218215
if (widget.controller == null) {
219216
_controller = TextEditingController(text: initialValue);
220-
} else {
221-
widget.controller.addListener(_handleControllerChanged);
222-
}
223-
}
224-
225-
@override
226-
void didUpdateWidget(FormBuilderTextField oldWidget) {
227-
super.didUpdateWidget(oldWidget);
228-
if (widget.controller != oldWidget.controller) {
229-
oldWidget.controller?.removeListener(_handleControllerChanged);
230-
widget.controller?.addListener(_handleControllerChanged);
231-
232-
if (oldWidget.controller != null && widget.controller == null) {
233-
_controller =
234-
TextEditingController.fromValue(oldWidget.controller.value);
235-
}
236-
if (widget.controller != null) {
237-
setValue(widget.controller.text);
238-
if (oldWidget.controller == null) _controller = null;
239-
}
240217
}
241218
}
242219

243220
@override
244221
void dispose() {
245-
widget.controller?.removeListener(_handleControllerChanged);
246222
_controller?.dispose();
247223
super.dispose();
248224
}
@@ -254,17 +230,4 @@ class _FormBuilderTextFieldState extends FormBuilderFieldState {
254230
_effectiveController.text = initialValue ?? '';
255231
});
256232
}
257-
258-
void _handleControllerChanged() {
259-
// Suppress changes that originated from within this class.
260-
//
261-
// In the case where a controller has been passed in to this widget, we
262-
// register this change listener. In these cases, we'll also receive change
263-
// notifications for changes originating from within this class -- for
264-
// example, the reset() method. In such cases, the FormField value will
265-
// already have been set.
266-
if (_effectiveController.text != value) {
267-
didChange(_effectiveController.text);
268-
}
269-
}
270233
}

lib/src/utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ String enumValueToString(Object o) => o.toString().split('.').last;
22

33
T enumValueFromString<T>(String key, Iterable<T> values) => values.firstWhere(
44
(v) => v != null && key == enumValueToString(v),
5-
orElse: () => null,
6-
);
5+
orElse: () => null,
6+
);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_form_builder
22
description: Package to build Material Form with fields like TextField, DropDown, Switches etc. with ability to create custom FormFields and composability and reuse validation functions.
3-
version: 4.0.0-alpha.7
3+
version: 4.0.0-alpha.8
44
homepage: https://github.com/danvick/flutter_form_builder
55

66
environment:

0 commit comments

Comments
 (0)