Skip to content

Commit

Permalink
upd formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
witwash committed Feb 14, 2025
1 parent 8fa8a73 commit 3098318
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
40 changes: 30 additions & 10 deletions optimus/lib/src/form/number_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class OptimusNumberInput extends StatefulWidget {
this.allowNegate = false,
this.isEnabled = true,
this.error,
this.hasFixedDecimalScale = true,
this.helper,
this.isInlined = false,
required this.label,
Expand Down Expand Up @@ -39,7 +38,8 @@ class OptimusNumberInput extends StatefulWidget {
assert(
min < max,
'The minimal allowed value should be lesser then max value',
);
),
assert(precision >= 0, 'Precision can be negative');

/// Whether negative values are allowed.
final bool allowNegate;
Expand All @@ -50,9 +50,6 @@ class OptimusNumberInput extends StatefulWidget {
/// Error message to be displayed.
final String? error;

/// Whether the decimal scale is fixed.
final bool hasFixedDecimalScale;

/// Helper widget to be displayed below the input.
final Widget? helper;

Expand Down Expand Up @@ -110,7 +107,11 @@ class OptimusNumberInput extends StatefulWidget {
}

class _OptimusNumberInputState extends State<OptimusNumberInput> {
late final _formatter = const _NumberInputFormatter();
late final _formatter = _NumberInputFormatter(
precision: widget.precision,
thousandSeparator: widget.thousandSeparator,
decimalSeparator: widget.decimalSeparator,
);

TextEditingController? _controller;

Expand Down Expand Up @@ -233,16 +234,35 @@ class _Divider extends StatelessWidget {
}

class _NumberInputFormatter extends TextInputFormatter {
const _NumberInputFormatter();
const _NumberInputFormatter({
required this.precision,
required this.thousandSeparator,
required this.decimalSeparator,
});

final int precision;
final OptimusNumberSeparatorVariant thousandSeparator;
final OptimusNumberSeparatorVariant decimalSeparator;

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue,
) {
// TODO(witwash): implement formatEditUpdate
throw UnimplementedError();
final newText = newValue.text;
final newTextNumber = num.parse(newText).toStringAsFixed(precision);

return newValue.copyWith(text: newTextNumber);
}
}

enum OptimusNumberSeparatorVariant { comma, stop, none, empty }
enum OptimusNumberSeparatorVariant {
comma(','),
stop('.'),
none(' '),
empty('');

const OptimusNumberSeparatorVariant(this.separator);

final String separator;
}
20 changes: 17 additions & 3 deletions optimus_widgetbook/lib/components/forms/number_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,24 @@ import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;
)
Widget createDefaultStyle(BuildContext _) => const _Content();

class _Content extends StatelessWidget {
class _Content extends StatefulWidget {
const _Content();

@override
State<_Content> createState() => _ContentState();
}

class _ContentState extends State<_Content> {
final double _initialValue = 0;

late double _value;

@override
void initState() {
super.initState();
_value = _initialValue;
}

@override
Widget build(BuildContext context) {
final k = context.knobs;
Expand Down Expand Up @@ -43,12 +58,11 @@ class _Content extends StatelessWidget {
size: k.widgetSizeKnob,
helper: helper?.toWidget(),
isInlined: k.boolean(label: 'Inlined'),
hasFixedDecimalScale: k.boolean(label: 'Has fixed decimal scale'),
isLoading: k.boolean(label: 'Is loading'),
precision: k.int.slider(label: 'Precision'),
prefix: prefix?.toWidget(),
isRequired: k.boolean(label: 'Required'),
initialValue: k.double.slider(label: 'Initial', max: max, min: min),
initialValue: _value,
thousandSeparator: k.list(
label: 'Thousand Separator',
options: OptimusNumberSeparatorVariant.values,
Expand Down

0 comments on commit 3098318

Please sign in to comment.