Created with <3 with dartpad.dev.
Created
September 21, 2022 14:20
-
-
Save osaxma/823f36a964b747b92833984587f228c6 to your computer and use it in GitHub Desktop.
Replace Western Arabic Numerals with Western ones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
class TextFieldExample extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return TextField( | |
inputFormatters: <TextInputFormatter>[ | |
/// Replaces: any of [٠١٢٣٤٥٦٧٨٩] with its respective [0123456789] | |
...replaceArabicNumeralsFromEastToWestFormatters, | |
FilteringTextInputFormatter.digitsOnly, | |
], | |
); | |
} | |
} | |
/// Use this list as an `inputFormatters` in `TextField` or `TextFormField` | |
/// | |
/// This will replace each Eastern Arabic Numeral with its respective Western | |
/// numeral. | |
final replaceArabicNumeralsFromEastToWestFormatters = | |
arabicNumeralsEastToWest.keys.map( | |
(key) { | |
return FilteringTextInputFormatter.deny( | |
RegExp(key), | |
replacementString: arabicNumeralsEastToWest[key]!, | |
); | |
}, | |
).toList(); | |
/// A map where the keys are the [Eastern Arabic Numerals][] in unicodes | |
/// and values of [Western Arabic Numerals][] | |
/// | |
/// | |
/// [Eastern Arabic Numerals]: https://en.wikipedia.org/wiki/Eastern_Arabic_numerals | |
/// [Western Arabic Numerals]: https://en.wikipedia.org/wiki/Arabic_numerals | |
const arabicNumeralsEastToWest = <String, String>{ | |
'\u0660': '0', // ٠ <-> 0 | |
'\u0661': '1', // ١ <-> 1 | |
'\u0662': '2', // ٢ <-> 2 | |
'\u0663': '3', // ٣ <-> 3 | |
'\u0664': '4', // ٤ <-> 4 | |
'\u0665': '5', // ٥ <-> 5 | |
'\u0666': '6', // ٦ <-> 6 | |
'\u0667': '7', // ٧ <-> 7 | |
'\u0668': '8', // ٨ <-> 8 | |
'\u0669': '9', // ٩ <-> 9 | |
}; | |
/* -------------------------------------------------------------------------- */ | |
/* The APP */ | |
/* -------------------------------------------------------------------------- */ | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData(primarySwatch: Colors.blue), | |
home: const MyHomePage( | |
title: 'Example for Replacing Arabic Numerals from East to West'), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
final String title; | |
const MyHomePage({super.key, required this.title}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text(title)), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Text( | |
'Insert numbers using Arabic Keyboard i.e. ٠١٢٣٤٥٦٧٨٩', | |
), | |
TextField( | |
inputFormatters: <TextInputFormatter>[ | |
/// Replaces: any of [٠١٢٣٤٥٦٧٨٩] with its respective [0123456789] | |
...replaceArabicNumeralsFromEastToWestFormatters, | |
FilteringTextInputFormatter.digitsOnly, | |
], | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment