我使用Flutter超过一周,并想创建一个阿拉伯语(从右到左)的应用程序。
我正在阅读Internationalizing Flutter Apps,但没有提及如何设置布局方向。
那么,如何在Flutter中显示从右到左(RTL)的布局?
答案 0 :(得分:20)
首先,您必须将flutter_localizations
软件包添加到pubspec.yml
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
现在您有两种选择:
1。在所有设备上强制使用语言环境(和方向)
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。根据设备区域设置布局方向
(如果用户电话的语言环境是RTL
语言并且以supportedLocales
存在,则您的应用以RTL
模式运行,否则您的应用为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语言是:
static const List<String> _rtlLanguages = <String>[
'ar', // Arabic
'fa', // Farsi
'he', // Hebrew
'ps', // Pashto
'ur', // Urdu
];
答案 1 :(得分:14)
您需要创建Builder并使用TextDirection.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,
);
},
),
);
},
.
.
.
);
答案 2 :(得分:1)
请检查Internationalization Flutter应用程序(阿拉伯语RTL) https://proandroiddev.com/internationalization-flutter-app-arabic-rtl-fe99bfae696e
答案 3 :(得分:1)
通过在textDirection
小部件中将rtl
属性设置为Directionality
,SfRangeSelector支持从右向左更改小部件的布局方向。
SfRangeValues _initialValues = SfRangeValues(4.0, 8.0);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Directionality(
textDirection: TextDirection.rtl,
child: Center(
child: SfRangeSelector(
min: 2.0,
max: 10.0,
interval: 1,
showLabels: true,
showTicks: true,
initialValues: _initialValues,
child: Container(
color: Colors.pink[200],
height: 150,
),
),
)
),
)
);
}
答案来自:https://help.syncfusion.com/flutter/range-selector/right-to-left
答案 4 :(得分:0)
如果在文本字段中需要从右到左或从左到右。 您可以在TextField中使用TextDirection
TextField(
textDirection: TextDirection.rtl,
decoration: InputDecoration(
labelText: "Enter Pashto Name",
),
),
答案 5 :(得分:0)
为整个应用设置RTL
配置的最佳和最短的方法。
void main() {
runApp(
MaterialApp(
home: Directionality( // add this
textDirection: TextDirection.rtl, // set this property
child: HomePage(),
),
),
);
}
答案 6 :(得分:0)
只需将此附加到您的材料应用程序:
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,
);
},
),
);
},