@@ -328,20 +328,14 @@ class FormBuilderTypeAhead<T> extends FormBuilderField<T> {
328
328
),
329
329
focusNode: state.effectiveFocusNode,
330
330
decoration: state.decoration,
331
- ) as TextFieldConfiguration ,
331
+ ),
332
332
// HACK to satisfy strictness
333
333
suggestionsCallback: suggestionsCallback,
334
334
itemBuilder: itemBuilder,
335
335
transitionBuilder: (context, suggestionsBox, controller) =>
336
336
suggestionsBox,
337
337
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);
345
339
onSuggestionSelected? .call (suggestion);
346
340
},
347
341
getImmediateSuggestions: getImmediateSuggestions,
@@ -381,44 +375,54 @@ class _FormBuilderTypeAheadState<T>
381
375
void initState () {
382
376
super .initState ();
383
377
_typeAheadController = widget.controller ??
384
- TextEditingController (text: widget.initialValue ? . toString ( ));
385
- _typeAheadController.addListener (_handleControllerChanged);
378
+ TextEditingController (text: _getTextString (initialValue ));
379
+ // _typeAheadController.addListener(_handleControllerChanged);
386
380
}
387
381
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
+ // }
400
394
401
395
@override
402
396
void didChange (T ? value) {
403
397
super .didChange (value);
398
+ var text = _getTextString (value);
404
399
405
- if (_typeAheadController.text != value ) {
406
- _typeAheadController.text = value. toString () ;
400
+ if (_typeAheadController.text != text ) {
401
+ _typeAheadController.text = text ;
407
402
}
408
403
}
409
404
410
405
@override
411
406
void dispose () {
412
407
// Dispose the _typeAheadController when initState created it
413
- if (null == widget.controller) {
414
- _typeAheadController.dispose ();
415
- }
416
408
super .dispose ();
409
+ _typeAheadController.dispose ();
417
410
}
418
411
419
412
@override
420
413
void reset () {
421
414
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;
423
427
}
424
428
}
0 commit comments