flutter 转换英语数字与波斯语或阿拉伯数字在 dart

eagi6jfj  于 2022-12-27  发布在  Flutter
关注(0)|答案(6)|浏览(204)

我是Dartflutter的新手。
我想用波斯语数字替换英语数字。如何实现?

1-2-3-4-5-6-7-8-9 ==> ‍‍۱-۲-۳-۴-۵-۶-۷-۸-۹
dphi5xsq

dphi5xsq1#

示例:

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'));  // ==>  ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
}
iklwldmw

iklwldmw2#

static String ConvertDigitsToLatin(String s) {
var sb = new StringBuffer();
for (int i = 0; i < s.length; i++) {
  switch (s[i]) {
    //Persian digits
    case '\u06f0':
      sb.write('0');
      break;
    case '\u06f1':
      sb.write('1');
      break;
    case '\u06f2':
      sb.write('2');
      break;
    case '\u06f3':
      sb.write('3');
      break;
    case '\u06f4':
      sb.write('4');
      break;
    case '\u06f5':
      sb.write('5');
      break;
    case '\u06f6':
      sb.write('6');
      break;
    case '\u06f7':
      sb.write('7');
      break;
    case '\u06f8':
      sb.write('8');
      break;
    case '\u06f9':
      sb.write('9');
      break;

    //Arabic digits
    case '\u0660':
      sb.write('0');
      break;
    case '\u0661':
      sb.write('1');
      break;
    case '\u0662':
      sb.write('2');
      break;
    case '\u0663':
      sb.write('3');
      break;
    case '\u0664':
      sb.write('4');
      break;
    case '\u0665':
      sb.write('5');
      break;
    case '\u0666':
      sb.write('6');
      break;
    case '\u0667':
      sb.write('7');
      break;
    case '\u0668':
      sb.write('8');
      break;
    case '\u0669':
      sb.write('9');
      break;
    default:
      sb.write(s[i]);
      break;
  }
}
return sb.toString();
}
b4qexyjb

b4qexyjb3#

不确定你会在什么上下文中使用这些数字,但我宁愿定义一个常量Map:const numberMap = {0: '۰', 1: '۱', 2:'۲', 3:'۳', 4:'٤', 5:'۵', 6:'٦', 7:'۷', 8:'۸',9: '۹'};然后你可以调用numberMap[number]来重用它。

628mspwn

628mspwn4#

根据此答案:https://stackoverflow.com/a/63934087/14409491

extension NumberConverter on num {
  static const Map<String, String> arabicDigits = <String, String>{
    '0': '\u0660',
    '1': '\u0661',
    '2': '\u0662',
    '3': '\u0663',
    '4': '\u0664',
    '5': '\u0665',
    '6': '\u0666',
    '7': '\u0667',
    '8': '\u0668',
    '9': '\u0669',
  };

  String toArabicDigits() {
    final String number = toString();
    StringBuffer sb = StringBuffer();
    for (int i = 0; i < number.length; i++) {
      sb.write(arabicDigits[number[i]] ?? number[i]);
    }
    return sb.toString();
  }
}
jtoj6r0c

jtoj6r0c5#

下面是我的解决方案:

String engNumberToFarsi(String number) {

    Map numbers = {
        '0' : '۰',
        '1' : '۱',
        '2' : '۲',
        '3' : '۳',
        '4' : '۴',
        '5' : '۵',
        '6' : '۶',
        '7' : '۷',
        '8' : '۸',
        '9' : '۹',
    };

    numbers.forEach((key, value) => number = number.replaceAll(key, value));

    return number;
}
pes8fvy9

pes8fvy96#

import 'package:alert_dialog/alert_dialog.dart';
import 'package:flutter/material.dart';

class Home2 extends StatefulWidget {
  const Home2({Key? key}) : super(key: key);

  @override
  State<Home2> createState() => _Home2State();
}

class _Home2State extends State<Home2> {
  String TransformerNum = 'محمود 2026';
  TextEditingController TransformerNumController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('ArbicNumbers'),),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top:220,left: 650),
              child: Text('${TransformerNum}',style: TextStyle(fontFamily: '(A) Arslan Wessam A', fontSize: 25)),
            ),
            Container(
              margin: EdgeInsets.only(top:220,left: 650),
              child: ElevatedButton(
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text('${TransformerNum}',style: TextStyle(fontFamily: '(A) Arslan Wessam A', fontSize: 25)),
                    SizedBox(width: 10,),
                    Text('اضف بيانات المحول ',style: TextStyle(fontFamily: '(A) Arslan Wessam A', fontSize: 25)),

                  ],
                ),
                onPressed: () async{
                  return alert(
                      context,
                      title: Column(
                        children: [

                          Container(
// margin: EdgeInsets.only(left:200, top: 200, right: 300),
                              child: TextFormField(
                                controller: TransformerNumController,
                                decoration: InputDecoration(
                                    labelText: 'ادخل البيانات',
                                    hintMaxLines: 1,
                                    enabledBorder: const OutlineInputBorder(
                                        borderSide: BorderSide(
                                          color: Colors.blue,
                                        )),
                                    border: OutlineInputBorder(
                                        borderRadius: BorderRadius.circular(200)),
                                    prefixIcon: const Icon(
                                      Icons.add_circle_outline_outlined,
                                      color: Colors.blue,
                                    ),
                                    fillColor: Colors.lightBlue[50],
                                    filled: true,
                                    hintText: '....... مثال 1او 2 او'),

                              )
                          ),

                        ],
                      ),

                      textOK: IconButton(onPressed: (){
                        setState((){
                          TransformerNum = replaceFarsiNumber(TransformerNumController.text);
                          //TransformerNum = TransformerNumController.text;
                        });
                        Navigator.of(context).pop();
                      }, icon: Icon(Icons.add_circle))
                  );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
  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;
  }
}

相关问题