layout - flutter 中从右到左(RTL)

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

我正在阅读 Internationalizing Flutter Apps ,但没有提到如何设置布局方向。

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

最佳答案

你有两个选择:

1.在所有设备上强制使用语言环境(和方向)

-- 方法 1: 本地化

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,
  .
  .
  .
);

-- 方法 2: 无需本地化

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

2。根据设备区域设置布局方向 (如果用户手机语言环境是 RTL 语言并且存在于 supportedLocales 中,则您的应用程序以 RTL 模式运行,否则您的应用程序是 LTR )

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 中的 rtl 语言有:

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

关于layout - flutter 中从右到左(RTL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50535185/

相关文章:

android - 没有权限(plugdev 组中的用户;你的 udev 规则错了吗?)

dart - 有没有办法将高度设置为抽屉页眉?

android - Flutter:升级Play商店的版本代码

logging - Dart/flutter 错误 : 'toStringDeep' isn't d

flutter - 如何在 Ios 上更改状态栏文本颜色

flutter - 如何从 flutter 中获取 .apk 和 .ipa 文件?

dart - 没有 AppBar 的 Flutter TabBar

text - 如何为 Text 小部件 Flutter 实现 OnPressed 回调

dart - 转换时间戳

Flutter - 隐藏 FloatingActionButton