Skip to content

Commit b6b5a7e

Browse files
committed
Move formatting
1 parent 8e8c169 commit b6b5a7e

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

lib/src/base_spin_box.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ mixin SpinBoxMixin<T extends BaseSpinBox> on State<T> {
5252
SpinController get controller => _controller;
5353
TextEditingController get editor => _editor;
5454
FocusNode get focusNode => _focusNode;
55-
TextInputFormatter get formatter => SpinFormatter(_controller);
55+
SpinFormatter get formatter => SpinFormatter(_controller);
56+
57+
String _formatValue(double value) {
58+
return formatter.formatValue(value,
59+
decimals: widget.decimals, digits: widget.digits);
60+
}
5661

5762
Map<ShortcutActivator, VoidCallback> get bindings {
5863
return {
@@ -76,10 +81,9 @@ mixin SpinBoxMixin<T extends BaseSpinBox> on State<T> {
7681
pageStep: widget.pageStep,
7782
value: widget.value,
7883
decimals: widget.decimals,
79-
digits: widget.digits,
8084
);
8185
_controller.addListener(_handleValueChange);
82-
_editor = TextEditingController(text: _controller.format(widget.value));
86+
_editor = TextEditingController(text: _formatValue(widget.value));
8387
_editor.addListener(_handleTextChange);
8488
_focusNode = widget.focusNode ?? FocusNode();
8589
_focusNode.addListener(_handleFocusChange);
@@ -112,7 +116,7 @@ mixin SpinBoxMixin<T extends BaseSpinBox> on State<T> {
112116
}
113117

114118
void _updateText(double newValue) {
115-
final text = _controller.format(newValue);
119+
final text = _formatValue(newValue);
116120
final selection = _editor.selection;
117121
final oldOffset = _controller.value.isNegative ? 1 : 0;
118122
final newOffset = _controller.parse(text)?.isNegative == true ? 1 : 0;
@@ -130,7 +134,7 @@ mixin SpinBoxMixin<T extends BaseSpinBox> on State<T> {
130134
void fixupValue(String text) {
131135
final v = _controller.parse(text);
132136
if (v == null) {
133-
_editor.text = _controller.format(_controller.value);
137+
_editor.text = _formatValue(_controller.value);
134138
} else if (v < _controller.min || v > _controller.max) {
135139
_controller.value = v.clamp(_controller.min, _controller.max);
136140
}

lib/src/spin_controller.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class SpinController extends ValueNotifier<double> {
3030
this.step = 1,
3131
this.pageStep,
3232
this.decimals = 0,
33-
this.digits = 0,
3433
}) : _min = min,
3534
_max = max,
3635
super(value);
@@ -41,7 +40,6 @@ class SpinController extends ValueNotifier<double> {
4140
final double step;
4241
final double? pageStep;
4342
final int decimals;
44-
final int digits;
4543

4644
double get min => _min;
4745
set min(double min) {
@@ -61,10 +59,6 @@ class SpinController extends ValueNotifier<double> {
6159
value = value.clamp(min, max);
6260
}
6361

64-
String format(double value) {
65-
return value.toStringAsFixed(decimals).padLeft(digits, '0');
66-
}
67-
6862
double? parse(String text) {
6963
if (decimals > 0) {
7064
return double.tryParse(text);

lib/src/spin_formatter.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class SpinFormatter extends TextInputFormatter {
3131

3232
final SpinController _controller;
3333

34+
String formatValue(
35+
double value, {
36+
required int decimals,
37+
required int digits,
38+
}) {
39+
return value.toStringAsFixed(decimals).padLeft(digits, '0');
40+
}
41+
3442
@override
3543
TextEditingValue formatEditUpdate(
3644
TextEditingValue oldValue,

0 commit comments

Comments
 (0)