Skip to content

Commit 125fb35

Browse files
committed
Merge branch 'split_packages' of https://github.com/danvick/flutter_form_builder into split_packages
2 parents 315285d + fcb98cf commit 125fb35

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

packages/form_builder_extra_fields/lib/src/fields/form_builder_typeahead.dart

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -328,20 +328,14 @@ class FormBuilderTypeAhead<T> extends FormBuilderField<T> {
328328
),
329329
focusNode: state.effectiveFocusNode,
330330
decoration: state.decoration,
331-
) as TextFieldConfiguration,
331+
),
332332
// HACK to satisfy strictness
333333
suggestionsCallback: suggestionsCallback,
334334
itemBuilder: itemBuilder,
335335
transitionBuilder: (context, suggestionsBox, controller) =>
336336
suggestionsBox,
337337
onSuggestionSelected: (T suggestion) {
338-
if (selectionToTextTransformer != null) {
339-
state._typeAheadController.text =
340-
selectionToTextTransformer(suggestion);
341-
} else {
342-
state._typeAheadController.text =
343-
suggestion != null ? suggestion.toString() : '';
344-
}
338+
state.didChange(suggestion);
345339
onSuggestionSelected?.call(suggestion);
346340
},
347341
getImmediateSuggestions: getImmediateSuggestions,
@@ -381,44 +375,54 @@ class _FormBuilderTypeAheadState<T>
381375
void initState() {
382376
super.initState();
383377
_typeAheadController = widget.controller ??
384-
TextEditingController(text: widget.initialValue?.toString());
385-
_typeAheadController.addListener(_handleControllerChanged);
378+
TextEditingController(text: _getTextString(initialValue));
379+
// _typeAheadController.addListener(_handleControllerChanged);
386380
}
387381

388-
void _handleControllerChanged() {
389-
// Suppress changes that originated from within this class.
390-
//
391-
// In the case where a controller has been passed in to this widget, we
392-
// register this change listener. In these cases, we'll also receive change
393-
// notifications for changes originating from within this class -- for
394-
// example, the reset() method. In such cases, the FormField value will
395-
// already have been set.
396-
if (_typeAheadController.text != value) {
397-
didChange(_typeAheadController.text as T);
398-
}
399-
}
382+
// void _handleControllerChanged() {
383+
// Suppress changes that originated from within this class.
384+
//
385+
// In the case where a controller has been passed in to this widget, we
386+
// register this change listener. In these cases, we'll also receive change
387+
// notifications for changes originating from within this class -- for
388+
// example, the reset() method. In such cases, the FormField value will
389+
// already have been set.
390+
// if (_typeAheadController.text != value) {
391+
// didChange(_typeAheadController.text as T);
392+
// }
393+
// }
400394

401395
@override
402396
void didChange(T? value) {
403397
super.didChange(value);
398+
var text = _getTextString(value);
404399

405-
if (_typeAheadController.text != value) {
406-
_typeAheadController.text = value.toString();
400+
if (_typeAheadController.text != text) {
401+
_typeAheadController.text = text;
407402
}
408403
}
409404

410405
@override
411406
void dispose() {
412407
// Dispose the _typeAheadController when initState created it
413-
if (null == widget.controller) {
414-
_typeAheadController.dispose();
415-
}
416408
super.dispose();
409+
_typeAheadController.dispose();
417410
}
418411

419412
@override
420413
void reset() {
421414
super.reset();
422-
_typeAheadController.text = initialValue?.toString() ?? '';
415+
416+
_typeAheadController.text = _getTextString(initialValue);
417+
}
418+
419+
String _getTextString(T? value) {
420+
var text = value == null
421+
? ''
422+
: widget.selectionToTextTransformer != null
423+
? widget.selectionToTextTransformer!(value!)
424+
: value.toString();
425+
426+
return text;
423427
}
424428
}

0 commit comments

Comments
 (0)