updateField method
Implementation
void updateField() async {
// Store current cursor position.
_previousSelection = widget.controller.selection;
String updatedText = widget.controller.text;
num? v = num.tryParse(updatedText);
if (v == null) {
// If the parsing fails, set to 0 or min.
v = widget.min ?? 0;
} else {
// Apply min and max constraints.
if (widget.min != null) {
v = max(v, widget.min!);
}
if (widget.max != null) {
v = min(v, widget.max!);
}
}
// Controller text automatically updates to
// the range of [widget.min] and [widget.max].
widget.controller.text = v.toString();
// Apply decimal places if needed.
if (widget.decimalPlaces > 0) {
v = double.parse(v.toStringAsFixed(widget.decimalPlaces));
}
_isInternalChange = true;
// Update the state.
ref.read(widget.stateProvider.notifier).state = v;
// Save to SharedPreferences if key is provided.
if (widget.sharedPrefsKey != null) {
await _saveToSharedPrefs(v);
}
// Call onValueChanged callback if provided.
if (widget.onValueChanged != null) {
await widget.onValueChanged!(v.toString());
}
// Restore cursor position if needed.
if (_previousSelection != null && _focusNode.hasFocus) {
widget.controller.selection = TextSelection.collapsed(
offset: min(
_previousSelection!.baseOffset,
widget.controller.text.length,
),
);
}
// Trigger the callback after all updates are done.
_triggerUpDownCallback();
_isInternalChange = false;
}