updateField method
Implementation
void updateField() {
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));
}
// Update state provider.
ref.watch(widget.stateProvider.notifier).state = v;
}