Home > OS >  Currency is not updated when Flutter currency_text_input_formatter showModalBottomSheet is opened
Currency is not updated when Flutter currency_text_input_formatter showModalBottomSheet is opened

Time:05-14

I'm trying to make a TextFormField to enter money with the enter image description here

When you make a change with the keyboard:

enter image description here

As you can see, this time the icon of the TRY currency has arrived. When I open the showModalBottomSheet, I want the TRY currency icon to appear. I need to make a change on the keyboard in order for the actual currency, TRY, to appear. When the showModalBottomSheet opens, I want it to show (TRY) icon correctly.

How can I solve this problem?

textFormField codes:

TextFormField(
  style: TextStyle(fontSize: 19),
  decoration: InputDecoration(
    border: OutlineInputBorder(),
  ),
  initialValue: _fiyat.format(snapshot.data!.docs[index].data()["urunFiyati"]),
  inputFormatters: <TextInputFormatter>[
    CurrencyTextInputFormatter(
      locale: 'tr_TR',
      decimalDigits: 2,
      symbol: '₺',
    ),
  ],
  keyboardType: TextInputType.number,
),

Thanks in advance for the help :)

CodePudding user response:

Try to initialize CurrencyTextInputFormatter before build(BuildContext context).UPD: code updated, so showModalBottomSheet is also used

import 'package:flutter/material.dart';
import 'package:currency_text_input_formatter/currency_text_input_formatter.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return  MaterialApp(home: HomePage());
  }
}

class HomePage extends StatelessWidget {
   HomePage({Key? key}) : super(key: key);
  final CurrencyTextInputFormatter _formatter = CurrencyTextInputFormatter(
    locale: 'tr_TR',
    decimalDigits: 2,
    symbol: '₺',
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: ElevatedButton(
          child: const Text('showModalBottomSheet'),
          onPressed: () {
            showModalBottomSheet<void>(
              context: context,
              builder: (BuildContext context) {
                return Container(
                  height: 200,
                  color: Colors.grey[100],
                  child: Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 30,vertical: 10),
                          child: TextFormField(
                            initialValue: _formatter.format('2000'),
                            inputFormatters: <TextInputFormatter>[_formatter],
                            keyboardType: TextInputType.number,
                          ),
                        ),
                        const Text('Modal BottomSheet'),
                        ElevatedButton(
                          child: const Text('Close BottomSheet'),
                          onPressed: () => Navigator.pop(context),
                        )
                      ],
                    ),
                  ),
                );
              },
            );
          },
        ),
      )
    );
  }
}
  • Related