Persian numbers in Flutter App displayed english

467 Views Asked by At

I'm developing a flutter app in Persian language. I used a Persian font that changes all Persian alphabets but it doesn't change numbers. They remain like this: 1, 2, 3 ,.. Is there any solution to change all numbers in app?

used font works in editors. when I have a String in app including '۱،۲,..' it displayed correctly. Challenge is when user enters value in textfield or response returns from server.

4

There are 4 best solutions below

0
yalda mohasseli On BEST ANSWER

Problem solved by using FD version of font.

4
secret On

To ensure consistent display of numbers in your Flutter app with a Persian font, you can use the flutter_localizations package along with the intl package to handle number formatting and localization. Here's how you can achieve this:

for pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: ^0.17.0

Run flutter pub get to fetch the packages. Import the required packages in your Dart file:

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart';

Set up localization delegates and supported locales in your MaterialApp widget. In the supportedLocales parameter, include the Locale for Persian (fa):

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('fa'), // Persian
      ],
      title: 'My App',
      home: MyHomePage(),
    );
  }
}

Use the NumberFormat class from the intl package to format numbers with Persian digits. You can create a helper method that converts numbers to a localized Persian format:

String formatNumber(int number) {
  final numberFormat = NumberFormat.decimalPattern('fa');
  return numberFormat.format(number);
}

When displaying numbers in your app, use the formatNumber method to ensure that they are correctly formatted:

final myNumber = 1234567;
final formattedNumber = formatNumber(myNumber);
print(formattedNumber); // Output: ۱,۲۳۴,۵۶۷

I tried it on iOS and Android everything is working stable

1
Ziero On

you need to add a function to replacing the numbers cause for some people it's seem imposibble to import a pesian / arabic numbers as data for a Text() so this function could help but you need FD version of whatever font you are using :

String replaceFarsiNumber(String input) {
  const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

  for (int i = 0; i < english.length; i++) {
    input = input.replaceAll(english[i], farsi[i]);
  }

  return input;
}

main() {
  print(replaceFarsiNumber('0-1-2-3-4-5-6-7-8-9'));  // ==>  ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
}
0
Golnar sheikh bahaie On

you can use this package to convert Persian to English or reverse.

https://pub.dev/packages/persian_number_utility