颤动中的从右到左 (RTL) [英] right-to-left (RTL) in flutter
问题描述
我使用 Flutter 一个多星期,想创建一个阿拉伯语(从右到左)应用.
I was using Flutter more than a week, and wanted to create an Arabic (right-to-left) app.
我正在阅读Internationalizing Flutter Apps,但没有提到如何设置布局方向.
I was reading Internationalizing Flutter Apps, but it didn't mention how to set the layout direction.
那么,如何在 Flutter 中显示从右到左 (RTL) 的布局?
So, how to show right-to-left (RTL) layout in Flutter?
推荐答案
你有两个选择:
1.在所有设备上强制使用区域设置(和方向)
-- 方法一:本地化
-- method 1: with localization
将 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: 没有本地化
-- method 2: without localization
MaterialApp(
.
.
.
builder: (context, child) {
return Directionality(
textDirection: TextDirection.rtl,
child: child,
);
},
.
.
.
);
2.根据设备区域设置布局方向(如果用户手机区域设置是 RTL
语言并且存在于 supportedLocales
中,则您的应用以 RTL
模式运行,否则您的应用是 LTR
)
2. set layout direction according to device locale
( if user phone locale is a RTL
language and exist in supportedLocales
, your app run in RTL
mode, otherwise your app is 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 语言是:
note : rtl languages in flutter are:
[
'ar', // Arabic
'fa', // Farsi
'he', // Hebrew
'ps', // Pashto
'ur', // Urdu
];
这篇关于颤动中的从右到左 (RTL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!