setValue method
set a current value
widget
the widget
value
the value
context
the ValuedWidgetContext
Implementation
@override
void setValue(TextFormField widget, dynamic value, ValuedWidgetContext context) {
var newText = value as String;
if (newText != widget.controller?.text) {
final previousSelection = widget.controller!.selection;
// set text
widget.controller?.text = newText;
// clamp selection
final maxOffset = newText.length;
final start = previousSelection.start.clamp(0, maxOffset);
final end = previousSelection.end.clamp(0, maxOffset);
widget.controller!.selection = TextSelection(baseOffset: start, extentOffset: end);
}
}