我使用Flutter已经有一个多星期的时间,并且希望创建一个阿拉伯语(从右到左)的应用程序。
我正在阅读Internationalizing Flutter Apps,但它没有提及如何设置布局方向。
那么,在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,
.
.
.
);
-- 方法二: 不使用本地化
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
];
设置整个应用程序的 RTL
配置 的最佳和最短途径。
void main() {
runApp(
MaterialApp(
home: Directionality( // add this
textDirection: TextDirection.rtl, // set this property
child: HomePage(),
),
),
);
}
package:flutter/material.dart
; - CopsOnRoadTextDirection.rtl
传递布局方向。new MaterialApp(
title: 'Flutter RTL',
color: Colors.grey,
builder: (BuildContext context, Widget child) {
return new Directionality(
textDirection: TextDirection.rtl,
child: new Builder(
builder: (BuildContext context) {
return new MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: 1.0,
),
child: child,
);
},
),
);
},
.
.
.
);
textdirection
属性设置为TextDirection.rtl
。TextField(
textDirection: TextDirection.rtl,
decoration: InputDecoration(
labelText: "Enter Pashto Name",
),
),
Text(
"This text is in the other direction!"
textDirection: TextDirection.rtl,
),
builder: (BuildContext context, Widget child) {
return new Directionality(
textDirection: TextDirection.rtl,
child: new Builder(
builder: (BuildContext context) {
return new MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: 1.0,
),
child: child,
);
},
),
);
},
supportedLocales: [
Locale("fa", "IR"),
Locale("en", 'US'),
],
locale: Locale("fa", "IR") // this is important line if not add this Apps will not change direction
GetMaterialApp(textDirection:TextDirection.rtl,home:SignUpScreen() // const HomeExpert());
textDirection
属性设置为 TextDirection.rtl,应用于您的 TextField 或 Text widget。 - Son of Stackoverflow