Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use custom decimal separator (ex: comma) #91

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/src/base_spin_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ abstract class BaseSpinBox extends StatefulWidget {
VoidCallback? get afterChange;
bool get readOnly;
FocusNode? get focusNode;
String get decimalSeparator;
}

mixin SpinBoxMixin<T extends BaseSpinBox> on State<T> {
Expand All @@ -57,11 +58,11 @@ mixin SpinBoxMixin<T extends BaseSpinBox> on State<T> {
FocusNode get focusNode => _focusNode;
TextEditingController get controller => _controller;
SpinFormatter get formatter => SpinFormatter(
min: widget.min, max: widget.max, decimals: widget.decimals);
min: widget.min, max: widget.max, decimals: widget.decimals, decimalSeparator: widget.decimalSeparator);

static double _parseValue(String text) => double.tryParse(text) ?? 0;
double _parseValue(String text) => double.tryParse(text.replaceAll(widget.decimalSeparator, '.')) ?? 0;
String _formatText(double value) {
return value.toStringAsFixed(widget.decimals).padLeft(widget.digits, '0');
return value.toStringAsFixed(widget.decimals).padLeft(widget.digits, '0').replaceAll('.', widget.decimalSeparator);
}

Map<ShortcutActivator, VoidCallback> get bindings {
Expand Down
7 changes: 7 additions & 0 deletions lib/src/cupertino/spin_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class CupertinoSpinBox extends BaseSpinBox {
this.beforeChange,
this.afterChange,
this.focusNode,
this.decimalSeparator = ".",
}) : assert(min <= max),
keyboardType = keyboardType ??
TextInputType.numberWithOptions(
Expand Down Expand Up @@ -213,6 +214,12 @@ class CupertinoSpinBox extends BaseSpinBox {
@override
final VoidCallback? afterChange;

/// The decimal separator used for formatting the value.
///
/// Defaults to `.`.
@override
final String decimalSeparator;

/// See [CupertinoTextField.enabled].
final bool enabled;

Expand Down
7 changes: 7 additions & 0 deletions lib/src/material/spin_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class SpinBox extends BaseSpinBox {
this.beforeChange,
this.afterChange,
this.focusNode,
this.decimalSeparator = ".",
}) : assert(min <= max),
keyboardType = keyboardType ??
TextInputType.numberWithOptions(
Expand Down Expand Up @@ -218,6 +219,12 @@ class SpinBox extends BaseSpinBox {
@override
final VoidCallback? afterChange;

/// The decimal separator used for formatting the value.
///
/// Defaults to `.`.
@override
final String decimalSeparator;

/// See [TextField.enabled].
final bool enabled;

Expand Down
5 changes: 3 additions & 2 deletions lib/src/spin_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ import 'package:flutter/services.dart';
// ignore_for_file: public_member_api_docs

class SpinFormatter extends TextInputFormatter {
SpinFormatter({required this.min, required this.max, required this.decimals});
SpinFormatter({required this.min, required this.max, required this.decimals, required this.decimalSeparator});

final double min;
final double max;
final int decimals;
final String decimalSeparator;

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
final input = newValue.text;
final input = newValue.text.replaceAll(decimalSeparator, '.');
if (input.isEmpty) {
return newValue;
}
Expand Down