颤动中的从右到左(RTL)
right-to-left (RTL) in flutter
我使用 Flutter 一个多星期了,想创建一个阿拉伯语(从右到左)应用程序。
我在看Internationalizing Flutter Apps,但是没有提到如何设置布局方向
那么,如何在 Flutter 中显示从右到左 (RTL) 布局?
您需要创建一个 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,
);
},
),
);
},
.
.
.
);
你有两个选择:
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。根据设备区域设置布局方向
(如果用户 phone 语言环境是 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
];
如果您需要反向显示文本,只需将 textdirection
属性 设置为 TextDirection.rtl
。
TextField 小部件的示例代码,
TextField(
textDirection: TextDirection.rtl,
decoration: InputDecoration(
labelText: "Enter Pashto Name",
),
),
文本小部件的示例代码,
Text(
"This text is in the other direction!"
textDirection: TextDirection.rtl,
),
为整个应用设置RTL
配置的最佳和最短方法。
void main() {
runApp(
MaterialApp(
home: Directionality( // add this
textDirection: TextDirection.rtl, // set this property
child: HomePage(),
),
),
);
}
SfRangeSelector支持通过在Directionality
widget中设置textDirection
属性为rtl
来改变widget在right-to-left方向的布局方向.
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
只需将此附加到您的 material 应用:
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,
);
},
),
);
},
我使用 Flutter 一个多星期了,想创建一个阿拉伯语(从右到左)应用程序。
我在看Internationalizing Flutter Apps,但是没有提到如何设置布局方向
那么,如何在 Flutter 中显示从右到左 (RTL) 布局?
您需要创建一个 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,
);
},
),
);
},
.
.
.
);
你有两个选择:
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。根据设备区域设置布局方向
(如果用户 phone 语言环境是 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
];
如果您需要反向显示文本,只需将 textdirection
属性 设置为 TextDirection.rtl
。
TextField 小部件的示例代码,
TextField(
textDirection: TextDirection.rtl,
decoration: InputDecoration(
labelText: "Enter Pashto Name",
),
),
文本小部件的示例代码,
Text(
"This text is in the other direction!"
textDirection: TextDirection.rtl,
),
为整个应用设置RTL
配置的最佳和最短方法。
void main() {
runApp(
MaterialApp(
home: Directionality( // add this
textDirection: TextDirection.rtl, // set this property
child: HomePage(),
),
),
);
}
SfRangeSelector支持通过在Directionality
widget中设置textDirection
属性为rtl
来改变widget在right-to-left方向的布局方向.
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
只需将此附加到您的 material 应用:
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,
);
},
),
);
},