Skip to content
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
48 changes: 35 additions & 13 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class MyHomePage extends StatelessWidget {
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
isDense: true,
labelText: 'Integer Number'),
border: OutlineInputBorder(),
isDense: true,
labelText: 'Integer Number',
),
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(15),
Expand All @@ -56,9 +57,10 @@ class MyHomePage extends StatelessWidget {
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
isDense: true,
labelText: 'Decimal Number'),
border: OutlineInputBorder(),
isDense: true,
labelText: 'Decimal Number',
),
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(15),
Expand All @@ -71,9 +73,10 @@ class MyHomePage extends StatelessWidget {
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
isDense: true,
labelText: 'Card Number'),
border: OutlineInputBorder(),
isDense: true,
labelText: 'Card Number',
),
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(19),
Expand All @@ -86,17 +89,36 @@ class MyHomePage extends StatelessWidget {
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
isDense: true,
hintText: 'dd/MM/yyyy'),
border: OutlineInputBorder(),
isDense: true,
hintText: 'dd/MM/yyyy',
),
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'\d+|-|/')),
DateInputFormatter(),
],
style: TextStyle(fontSize: 16.0, color: Colors.black),
),
)
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
isDense: true,
labelText: 'Currency Number Format',
),
keyboardType: TextInputType.number,
inputFormatters: [
CurrencyFormatter(
locale: 'de_DE', // locale of country
symbol: '€', // symbol beside value
),
],
style: TextStyle(fontSize: 16.0, color: Colors.black),
),
),
],
),
),
Expand Down
74 changes: 74 additions & 0 deletions lib/currency_formatter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'dart:math';

import 'package:flutter/services.dart';
import 'package:intl/intl.dart';

class CurrencyFormatter extends TextInputFormatter {
CurrencyFormatter({
this.locale,
this.name,
this.symbol,
this.isGrouped = false,
});

final String? locale;
final String? name;
final String? symbol;
final bool isGrouped;

@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
final isInserted = oldValue.text.length + 1 == newValue.text.length && newValue.text.startsWith(oldValue.text);
final isRemoved = oldValue.text.length - 1 == newValue.text.length && oldValue.text.startsWith(newValue.text);

if (!isInserted && !isRemoved) {
return oldValue;
}

final format = NumberFormat.currency(
locale: locale,
name: name,
symbol: symbol,
);

if (isGrouped) {
format.turnOffGrouping();
}

final isNegativeValue = newValue.text.startsWith('-');
var newText = newValue.text.replaceAll(RegExp('[^0-9]'), '');

if (isRemoved && !_lastCharacterIsDigit(oldValue.text)) {
final length = newText.length - 1;
newText = newText.substring(0, length > 0 ? length : 0);
}

if (newText.trim() == '') {
return newValue.copyWith(
text: isNegativeValue ? '-' : '',
selection: TextSelection.collapsed(offset: isNegativeValue ? 1 : 0),
);
} else if (newText == '00' || newText == '000') {
return TextEditingValue(
text: isNegativeValue ? '-' : '',
selection: TextSelection.collapsed(offset: isNegativeValue ? 1 : 0),
);
}

num newInt = int.parse(newText);
if (format.decimalDigits! > 0) {
newInt /= pow(10, format.decimalDigits!);
}

final newString = (isNegativeValue ? '-' : '') + format.format(newInt).trim();
return TextEditingValue(
text: newString,
selection: TextSelection.collapsed(offset: newString.length),
);
}

static bool _lastCharacterIsDigit(String text) {
final lastChar = text.substring(text.length - 1);
return RegExp('[0-9]').hasMatch(lastChar);
}
}
1 change: 1 addition & 0 deletions lib/pattern_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ library pattern_input_formatter;

export 'numeric_formatter.dart';
export 'date_formatter.dart';
export 'currency_formatter.dart';