使用本地化在列表内 – Flutter

huangapple go评论45阅读模式
英文:

Using Localization inside a list - Flutter

问题

在我的应用程序中,包含了本地化(翻译“en”和“ar”)。在我的翻译JSON文件中,有用户在注册过程中选择的城市,这些城市将显示在一个DropDownButtonFormField小部件中,如下所示:

final provincesField = DropdownButtonFormField<String>(
  value: dropDownValue,
  onChanged: (String? newValue) {
    setState(() {
      dropDownValue = newValue!;
    });
  },
  items: provinces.map<DropdownMenuItem<String>>((String value) {
    return DropdownMenuItem<String>(
        value: value,
        child: SizedBox(
          width: 100.0,
          child: Text(value, textAlign: TextAlign.center),
        ));
  }).toList(),
  elevation: 4,
  decoration: InputDecoration(
    prefixIcon: Icon(Icons.flag_circle),
    labelText: AppLocalizations.of(context)!.chooseProvinence,
    alignLabelWithHint: true,
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(10),
    ),
  ),
);

这些城市在以下列表中声明:

List<String> provinces = [
  'عمان',
  'إربد',
  'الزرقاء',
  'المفرق',
  'الطفيلة',
  'عجلون',
  'معان',
  'الكرك',
  'مادبا',
  'العقبة',
  'البلقاء',
  'جرش'
];
String dropDownValue = 'عمان';

我尝试通过在列表内使用AppLocalizations.of(context)!.city,但它不起作用,所以我相信有一种方法可以根据本地化声明列表值,我会感激任何帮助。

英文:

My application contains localizations (translation "en","ar"), in my translation JSON files there are cities that users will choose while registration process these cities appears in a DropDownButtonFormField widget as follows:

final provincesField = DropdownButtonFormField&lt;String&gt;(
      value: dropDownValue,
      onChanged: (String? newValue) {
        setState(() {
          dropDownValue = newValue!;
        });
      },
      items: provinces.map&lt;DropdownMenuItem&lt;String&gt;&gt;((String value) {
        return DropdownMenuItem&lt;String&gt;(
            value: value,
            child: SizedBox(
              width: 100.0,
              child: Text(value, textAlign: TextAlign.center),
            ));
      }).toList(),
      elevation: 4,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.flag_circle),
        labelText: AppLocalizations.of(context)!.chooseProvinence,
        alignLabelWithHint: true,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
        ),
      ),
    );

the cities are declared in this list:

List&lt;String&gt; provinces = [
    &#39;عمان&#39;,
    &#39;إربد&#39;,
    &#39;الزرقاء&#39;,
    &#39;المفرق&#39;,
    &#39;الطفيلة&#39;,
    &#39;عجلون&#39;,
    &#39;معان&#39;,
    &#39;الكرك&#39;,
    &#39;مادبا&#39;,
    &#39;العقبة&#39;,
    &#39;البلقاء&#39;,
    &#39;جرش&#39;
  ];
  String dropDownValue = &#39;عمان&#39;;

what am trying to achieve is that cities will appear to user based on the app language. I have tried to use AppLocalizations.of(context)!.city inside of the list but its not working so i believe there is some way to declare the list values based on localization, ill appreciate any help

答案1

得分: 1

      &quot;دكا&quot; : &quot;دكا&quot;,
      &quot;شيتاغونغ&quot; : &quot;شيتاغونغ&quot;,
      &quot;كوميلا&quot; : &quot;كوميلا&quot;,
      &quot;راجشاهي&quot; : &quot;راجشاهي&quot;,
      &quot;بولا&quot; : &quot;بولا&quot;,

      List&lt;String&gt; provinces = [
        &quot;دكا&quot;,
        &quot;شيتاغونغ&quot;,
        &quot;كوميلا&quot;,
        &quot;راجشاهي&quot;,
        &quot;بولا&quot;,
      ];
      String dropDownValue = &#39;دكا&#39;;


      Widget TestWidget() {
        return DropdownButtonFormField&lt;String&gt;(
          value: dropDownValue,
          onChanged: (String? newValue) {
            // To print the value with localization
            print(newValue!.tr);
          },
          items: provinces.map&lt;DropdownMenuItem&lt;String&gt;&gt;((String value) {
            return DropdownMenuItem&lt;String&gt;(value: value, child: Text(value.tr));
          }).toList(),
          elevation: 4,
          decoration: InputDecoration(
            prefixIcon: Icon(Icons.flag_circle),
            labelText: &#39;Select&#39;,
            alignLabelWithHint: true,
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
            ),
          ),
        );
      }
英文:

Suppose below localizations data is your cities list

English:

  &quot;Dhaka&quot; : &quot;Dhaka&quot;,
  &quot;Chittagong&quot; : &quot;Chittagong&quot;,
  &quot;Cumilla&quot; : &quot;Cumilla&quot;,
  &quot;Rajshahi&quot; : &quot;Rajshahi&quot;,
  &quot;Bhola&quot; : &quot;Bhola&quot;,

Arabic:

  &quot;Dhaka&quot; : &quot;دكا&quot;,
  &quot;Chittagong&quot; : &quot;شيتاغونغ&quot;,
  &quot;Cumilla&quot; : &quot;كوميلا&quot;,
  &quot;Rajshahi&quot; : &quot;راجشاهي&quot;,
  &quot;Bhola&quot; : &quot;بولا&quot;,

initialize your the localization keys of your cities and dropDownValue

  List&lt;String&gt; provinces = [
    &quot;Dhaka&quot;,
    &quot;Chittagong&quot;,
    &quot;Cumilla&quot;,
    &quot;Rajshahi&quot;,
    &quot;Bhola&quot;,
  ];
  String dropDownValue = &#39;Dhaka&#39;;

here is the widget. I used getx localization. So I used 'tr' after the child text of DropdownMenuItem. I hope it's can help u.

  Widget TestWidget() {
    return DropdownButtonFormField&lt;String&gt;(
      value: dropDownValue,
      onChanged: (String? newValue) {
        // To print the value with localization
        print(newValue!.tr);
      },
      items: provinces.map&lt;DropdownMenuItem&lt;String&gt;&gt;((String value) {
        return DropdownMenuItem&lt;String&gt;(value: value, child: Text(value.tr));
      }).toList(),
      elevation: 4,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.flag_circle),
        labelText: &#39;Select&#39;,
        alignLabelWithHint: true,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
        ),
      ),
    );
  }

If you need to store the city in database or anywhere with localization then you can use it like [dropDownValue.tr]

huangapple
  • 本文由 发表于 2023年2月8日 23:48:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定