从右到左(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.
我正在阅读国际化Flutter应用,但没有提及如何设置布局方向
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?
推荐答案
首先,您必须将flutter_localizations
程序包添加到pubspec.yml
first you must add flutter_localizations
package to your pubspec.yml
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
现在您有两种选择:
1.在所有设备上强制使用语言环境(和方向)
- 方法1: (已本地化)
-- method 1: with localization
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
MaterialApp(
localizationsDelegates: [
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: ,无需本地化(如果使用此方法,则无需添加flutter_localizations
)
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
)
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale("en", "US"),
Locale("fa", "IR"), // OR Locale('ar', 'AE') OR Other RTL locales
],
.
.
.
);
注释:颤抖的rtl语言是:
note : rtl languages in flutter is :
[
'ar', // Arabic
'fa', // Farsi
'he', // Hebrew
'ps', // Pashto
'ur', // Urdu
];
这篇关于从右到左(RTL)颤动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!