Skip to content

Commit 07d16c8

Browse files
akhokhlushinjpnurmi
authored andcommitted
Added digits feature
You can set minimum digits for integer values so that "1" displays as "01" or "001" depending on the `digits` value.
1 parent 7b4c099 commit 07d16c8

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

example/lib/main.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ class HorizontalSpinBoxPage extends StatelessWidget {
4848
),
4949
padding: const EdgeInsets.all(16),
5050
),
51+
Padding(
52+
child: SpinBox(
53+
value: 5,
54+
digits: 2,
55+
decoration: InputDecoration(labelText: '2 digits'),
56+
),
57+
padding: const EdgeInsets.all(16),
58+
),
5159
Padding(
5260
child: SpinBox(
5361
max: 10.0,

lib/src/base_spin_box.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ abstract class BaseSpinBox extends StatefulWidget {
3535
double get step;
3636
double get value;
3737
int get decimals;
38+
int get digits;
3839
ValueChanged<double>? get onChanged;
3940
bool Function(double value)? get canChange;
4041
VoidCallback? get beforeChange;
@@ -57,7 +58,9 @@ abstract class BaseSpinBoxState<T extends BaseSpinBox> extends State<T> {
5758
min: widget.min, max: widget.max, decimals: widget.decimals);
5859

5960
static double _parseValue(String text) => double.tryParse(text) ?? 0;
60-
String _formatText(double value) => value.toStringAsFixed(widget.decimals);
61+
String _formatText(double value) {
62+
return value.toStringAsFixed(widget.decimals).padLeft(widget.digits, '0');
63+
}
6164

6265
Map<ShortcutActivator, VoidCallback> get bindings {
6366
return {
@@ -100,7 +103,7 @@ abstract class BaseSpinBoxState<T extends BaseSpinBox> extends State<T> {
100103
if (v == _value) return;
101104

102105
if (widget.canChange?.call(v) == false) {
103-
controller.text = _cachedValue.toStringAsFixed(widget.decimals);
106+
controller.text = _formatText(_cachedValue);
104107
setState(() {
105108
_value = _cachedValue;
106109
});

lib/src/cupertino/spin_box.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class CupertinoSpinBox extends BaseSpinBox {
5555
this.value = 0,
5656
this.interval = const Duration(milliseconds: 100),
5757
this.acceleration,
58+
this.digits = 0,
5859
this.decimals = 0,
5960
bool? enabled,
6061
this.readOnly = false,
@@ -130,6 +131,12 @@ class CupertinoSpinBox extends BaseSpinBox {
130131
@override
131132
final int decimals;
132133

134+
/// The number of digits used for formatting the value.
135+
///
136+
/// Defaults to `0`.
137+
@override
138+
final int digits;
139+
133140
/// The interval used for auto-incrementing and -decrementing.
134141
///
135142
/// When holding down the increment and decrement buttons, respectively.

lib/src/material/spin_box.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class SpinBox extends BaseSpinBox {
5757
this.interval = const Duration(milliseconds: 100),
5858
this.acceleration,
5959
this.decimals = 0,
60+
this.digits = 0,
6061
bool? enabled,
6162
this.readOnly = false,
6263
this.autofocus = false,
@@ -132,6 +133,12 @@ class SpinBox extends BaseSpinBox {
132133
@override
133134
final int decimals;
134135

136+
/// The number of digits used for formatting the value.
137+
///
138+
/// Defaults to `0`.
139+
@override
140+
final int digits;
141+
135142
/// The interval used for auto-incrementing and -decrementing.
136143
///
137144
/// When holding down the increment and decrement buttons, respectively.

0 commit comments

Comments
 (0)