How to get currency and currency symbol in flutter by country code?

馋奶兔 提交于 2021-02-04 15:55:08

问题


I'm converting my app from Java(Android) to Flutter(Dart) and I can't find a method to get the currency from context or from a country code.

Code in Java:

String country = Locale.getDefault().getCountry();
String currency = Currency.getInstance(new Locale("", country)).getCurrencyCode();

Code in Dart:

Locale locale = Localizations.localeOf(context);

String country = locale.countryCode;

回答1:


intl package does the trick

import 'package:intl/intl.dart';

void currency() {
    Locale locale = Localizations.localeOf(context);
    var format = NumberFormat.simpleCurrency(locale: locale.toString());
    print("CURRENCY SYMBOL ${format.currencySymbol}"); // $
    print("CURRENCY NAME ${format.currencyName}"); // USD
}



回答2:


While the accepted answer does work if your app supports the user's locale, it doesn't work if you didn't explicitly add support for the user's locale.

Instead you can use Platform.localeName from the dart:io package which will take the locale of the device itself.

For Example:

import 'package:intl/intl.dart';
import 'dart:io';

String getCurrency() {
  var format = NumberFormat.simpleCurrency(locale: Platform.localeName);
  return format.currencySymbol;
}



回答3:


You can get currency code using currency_pickers by giving it user country code.

CurrencyPickerUtils.getCountryByIsoCode('PK').currencyCode.toString() // PKR
CurrencyPickerUtils.getCountryByIsoCode('US').currencyCode.toString() // USD



回答4:


after a long time of searching on this problem the only solution that solved my problem is USE A FONT THAT SUPPORT YOUR SYMBOLE.
this was my symbole "₺" and the font that supported it is "Roboto", you can specify a font for your text like this:

  Text('₺',
     style: TextStyle(
     fontFamily: 'Roboto',
      )),


来源:https://stackoverflow.com/questions/58766133/how-to-get-currency-and-currency-symbol-in-flutter-by-country-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!