Skip to content

Commit

Permalink
upd formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
witwash committed Feb 14, 2025
1 parent 57a1eda commit 6ae4ea0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
49 changes: 45 additions & 4 deletions optimus/lib/src/form/number_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ class _OptimusNumberInputState extends State<OptimusNumberInput> {
@override
void initState() {
super.initState();
_effectiveController.text = widget.initialValue.toString();
_effectiveController.text = widget.initialValue.toString().format(
precision: widget.precision,
thousandSeparator: widget.thousandSeparator,
decimalSeparator: widget.decimalSeparator,
);
}

void _handleIncrease() {
Expand All @@ -140,7 +144,23 @@ class _OptimusNumberInputState extends State<OptimusNumberInput> {
_updateCurrentValue(result);
}

double get _currentValue => double.parse(_effectiveController.text);
double get _currentValue {
final text = _effectiveController.text;
String cleanedText = text;

if (widget.precision > 0) {
final lastIndex =
cleanedText.lastIndexOf(widget.decimalSeparator.separator);
if (lastIndex != -1) {
cleanedText = cleanedText.replaceRange(lastIndex, lastIndex + 1, '.');
}
}

cleanedText =
cleanedText.replaceAll(widget.thousandSeparator.separator, '');

return double.parse(cleanedText);
}

void _updateCurrentValue(double value) {
setState(
Expand All @@ -155,6 +175,7 @@ class _OptimusNumberInputState extends State<OptimusNumberInput> {

@override
void didUpdateWidget(OptimusNumberInput oldWidget) {
// TODO(witwash): fix value reset on widget prop change
super.didUpdateWidget(oldWidget);
if (oldWidget.min != widget.min && _currentValue < widget.min) {
_effectiveController.text = widget.min.toString().format(
Expand Down Expand Up @@ -290,6 +311,26 @@ extension on String {
required int precision,
required OptimusNumberSeparatorVariant thousandSeparator,
required OptimusNumberSeparatorVariant decimalSeparator,
}) =>
num.parse(this).toStringAsFixed(precision);
}) {
final fixedLength = num.parse(this).toStringAsFixed(precision);
final parts = fixedLength.split('.');
final wholePart = parts.first;
final decimalPart = parts.length > 1 ? parts.last : '';

final buffer = StringBuffer();
for (int i = 0; i < wholePart.length; i++) {
if (i > 0 && (wholePart.length - i) % 3 == 0) {
buffer.write(thousandSeparator.separator);
}
buffer.write(wholePart[i]);
}

if (decimalPart.isNotEmpty) {
buffer
..write(decimalSeparator.separator)
..write(decimalPart);
}

return buffer.toString();
}
}
2 changes: 1 addition & 1 deletion optimus_widgetbook/lib/components/forms/number_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class _ContentState extends State<_Content> {
initialOption: OptimusNumberSeparatorVariant.stop,
),
suffix: suffix?.toWidget(),
step: k.double.slider(label: 'Step', initialValue: 1),
step: k.double.input(label: 'Step', initialValue: 1),
);
}
}

0 comments on commit 6ae4ea0

Please sign in to comment.