gtgalone / currency_text_input_formatter

Currency Text Input Formatter for Flutter.
https://pub.dev/packages/currency_text_input_formatter
MIT License
50 stars 29 forks source link

Trying to increment using button #30

Closed Tichz closed 3 years ago

Tichz commented 3 years ago

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

Tichz commented 3 years ago

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'),
              ),
            ],
          ),
        ],
      ),
    ),
  ),
);

} } `

gtgalone commented 3 years ago

@Tichz Hello!

Did you complete your work?

Tichz commented 3 years ago

@Tichz Hello!

Did you complete your work?

Yes I ended up finding this way to do it