Trying to increment using button
Tichz opened this issue · 3 comments
I'm trying to increment the value of CurrencyTextInputFormatter using a button but I haven't come up with a way to make it increment and display the formatting, would I have some direction to offer
I don't know if this is the right solution but I reached the expected goal using the code below, I leave it as an answer if anyone has a question similar to mine.
`import 'package:currency_text_input_formatter/currency_text_input_formatter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final CurrencyTextInputFormatter formatter =
CurrencyTextInputFormatter(locale: 'eu', decimalDigits: 2, symbol: '%');
final TextEditingController controller = TextEditingController();
@OverRide
Widget build(BuildContext context) {
// Built-in Methods
return
MaterialApp(
title: 'Currency Text Input Formatter',
home: Scaffold(
appBar: AppBar(
title: const Text('Currency Text Input Formatter'),
),
body: Center(
child: Column(
children: [
TextField(
controller: controller,
inputFormatters: <TextInputFormatter>[
formatter,
],
keyboardType: TextInputType.number,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
formatter.formatEditUpdate(
TextEditingValue(
text: formatter.getUnformattedValue().toStringAsFixed(2),
),
TextEditingValue(
text: (formatter.getUnformattedValue() + 0.1)
.toStringAsFixed(2),
),
);
controller.text = formatter.getFormattedValue();
},
child: const Text('Increment'),
),
ElevatedButton(
onPressed: () {
formatter.formatEditUpdate(
TextEditingValue(
text: formatter.getUnformattedValue().toStringAsFixed(2),
),
TextEditingValue(
text: (formatter.getUnformattedValue() - 0.1)
.toStringAsFixed(2),
),
);
controller.text = formatter.getFormattedValue();
},
child: const Text('Decrement'),
),
],
),
],
),
),
),
);
}
}
`