我用了一个多星期的Flitter,想创建一个阿拉伯语(从右到左)应用程序.

我读的是Internationalizing Flutter Apps,但没有提到如何设置布局方向.

那么,如何在Flutter 中显示从右到左(RTL)的布局呢?

推荐答案

您有两个 Select :

1. force a locale ( and direction ) on all devices

--100与本地化

flutter_localizations个套餐添加到您的pubspec.yml

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

然后

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

MaterialApp(
  localizationsDelegates: [
    GlobalCupertinoLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: [
    Locale("fa", "IR"), // OR Locale('ar', 'AE') OR Other RTL locales
  ],
  locale: Locale("fa", "IR") // OR Locale('ar', 'AE') OR Other RTL locales,
  .
  .
  .
);

--100无本地化

MaterialApp(
  .
  .
  .
  builder: (context, child) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: child,
    );
  },
  .
  .
  .
);

2. set layout direction according to device locale ( if user phone locale is a 100 language and exist in 101, your app run in 100 mode, otherwise your app is 103 )

flutter_localizations个套餐添加到您的pubspec.yml

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

然后

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

MaterialApp(
  localizationsDelegates: [
    GlobalCupertinoLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: [
    Locale("en", "US"),
    Locale("fa", "IR"), // OR Locale('ar', 'AE') OR Other RTL locales
  ],
  .
  .
  .
);

Flutter 中的note:RTL语言包括:

[
  'ar', // Arabic
  'fa', // Farsi
  'he', // Hebrew
  'ps', // Pashto
  'ur', // Urdu
];

Flutter相关问答推荐

在Flutter Hive中将数据存储为对象与字符串的优点和缺点

Flutter bloc -如何使用BlocBuilder?

Flutter -如何将导航轨道标签靠左对齐?

material 3芯片,不能go 除输入芯片小部件上的轮廓

使用Future函数的容器中未显示图像,但在使用图像小工具时显示

为什么我的PUT请求不能正常工作?

Flutter 翼future 建造者不断开火和重建.它在每次重建时都会生成一个新的随机数--我如何防止这种情况?

为什么PUT方法没有发出任何响应?

如何确定Flutter 中的文本 colored颜色 ?

在 flutter 吧蜂巢中拯救主题

抖动问题:检测到0个或2个或更多具有相同值的[DropdownMenuItem]

Riverpod Provider.family 和 HookConsumerWidget 不刷新 UI

如何从Firebase登录获取用户信息,如电话号码、出生日期和性别

如何使 flutter 2 手指zoom ?

如何在向下滚动时隐藏顶部卡片并在向上滚动时出现?

如何防止卡子在底部溢出?

使用 Chaquopy 插件后 Gradle Sync 无法正常工作

如何从dart 中的当前日期时间中减go 1 天?

在 Flutter 中更新对象实例属性的最佳实践是什么?该实例嵌套在提供程序类的映射中,如下所示

设计响应式卡片的最佳方法,以避免像素溢出错误或 _AssertionError