
Arabic Support Is More Than Translation: RTL Layouts Done Right in Flutter
- كاتب موثّق
- Flutter 3.x
محتويات المقال
Locale('ar') to the app, most of it flips, but the order list doesn't look right. The 16-pixel gap now sits next to the arrow instead of where the Arabic text starts, the titles hug the wrong edge, the discount badge is stuck in the top-left corner, and every row's arrow points back into the text.
The common misconception
Row lays out its children from right to left, ListTile and AppBar swap their leading and trailing sides, and the default text alignment follows the direction. A demo screen built only from those widgets flips perfectly. But it only flips when the app knows its locale is RTL, and only the parts written in terms of "start" and "end". Real apps are full of left: 16.
What actually happens
Direction is a value in the tree, set by the locale
Directionality, an that "determines the ambient directionality of text and text-direction-sensitive render objects". You rarely add one yourself: MaterialApp builds a Localizations widget, and that widget inserts the Directionality for the current locale.
GlobalWidgetsLocalizations.delegate, from the flutter_localizations package, that "defines the default text direction, either left-to-right or right-to-left". And the app only follows the device's locale if its language is in supportedLocales. Your strings can come from anywhere; the direction doesn't come from them.
MaterialApp showing Arabic text with no locale setup read TextDirection.ltr. Setting locale: Locale('ar') without the delegates still gave ltr, plus a warning that the locale "is not supported by all of its localization delegates". Only with the delegates and Locale('ar') in supportedLocales did it become rtl.
void main() { runApp(const MaterialApp( // Includes GlobalWidgetsLocalizations, which marks Arabic as RTL localizationsDelegates: GlobalMaterialLocalizations.delegates, supportedLocales: [Locale('ar'), Locale('en')], home: OrdersScreen(), ));}Physical values don't flip
EdgeInsets docs say its left and right values "are not affected by the TextDirection" and point you to EdgeInsetsDirectional, whose start is "the side from which the user will start reading text". The same split runs through the framework: Alignment and AlignmentDirectional, Positioned and PositionedDirectional, TextAlign.left and TextAlign.start.
Icons flip only if they're marked
Icon mirrors in RTL only when its IconData has matchTextDirection set to true. In Flutter's Icons class, arrow_back and chevron_right have it; keyboard_arrow_right, which looks almost the same, doesn't. That flag matches Apple's guidance: "Flip an interface icon that shows forward or backward motion", so "a back button must point to the right" in RTL. But "Don't flip logos or universal signs and marks", a clock stays a clock, and you should "avoid flipping images like photographs".
Text follows its own language
TextAlign.start follows the ambient direction, so for user-generated text, pass a textDirection that matches the content. The intl package's Bidi.detectRtlDirectionality estimates it: in a quick test, it returned false for "Great food, slow delivery" and true for an Arabic sentence.
Fixing the order row
class OrderRow extends StatelessWidget { const OrderRow({super.key, required this.title}); final String title; @override Widget build(BuildContext context) => Padding( // Physical sides: they stay put when the layout flips padding: const EdgeInsets.only(left: 16, right: 8), child: Row(children: [ Expanded(child: Text(title, textAlign: TextAlign.left)), const Icon(Icons.keyboard_arrow_right), // never mirrored ]), );}class OrderRow extends StatelessWidget { const OrderRow({super.key, required this.title}); final String title; @override Widget build(BuildContext context) => Padding( // start and end resolve against the ambient Directionality padding: const EdgeInsetsDirectional.only(start: 16, end: 8), child: Row(children: [ Expanded(child: Text(title, textAlign: TextAlign.start)), const Icon(Icons.chevron_right), // matchTextDirection: true ]), );}Row flipped both versions: the icon moved to the left end. But the wrong row kept 16 pixels on the left, next to the icon, and only 8 on the right, where Arabic text starts. Its title hugged the left edge of its space, and the arrow still pointed right, back into the text. The right row had 16 pixels on the right, a right-aligned title and a mirrored chevron. In LTR, the two rows were pixel-identical, which is why this bug survives every review done in English.
EdgeInsets.only(left: 16) | EdgeInsetsDirectional.only(start: 16) |
Alignment.centerLeft | AlignmentDirectional.centerStart |
Positioned(left: 0) | PositionedDirectional(start: 0) |
TextAlign.left | TextAlign.start |
BorderRadius.only(topLeft: ...) | BorderRadiusDirectional.only(topStart: ...) |
How to apply it in your project
Make the locale RTL-aware first Add flutter_localizationsfrom the SDK, passGlobalMaterialLocalizations.delegatesand listLocale('ar')insupportedLocales. CheckDirectionality.of(context)on a real screen before touching any layout.Search for physical values Look for EdgeInsets.only(left,fromLTRB,Alignment.centerLeft,Positioned(left,TextAlign.leftandBorderRadius.only(topLeft. Convert each one to its directional version unless the direction is meant to be physical.Audit your icons Arrows that mean back, forward or "next" must mirror: use icons with matchTextDirection, or set it on your ownIconData. Leave logos, checkmarks, clocks and photos alone.Mind custom drawing and motion A CustomPainteror a slide animation with anOffsetworks in physical coordinates. ReadDirectionality.of(context)and flip the math yourself where it carries meaning.Test both directions Run your widget and golden tests in LTR and RTL, and do a manual pass with real Arabic text, since its length rarely matches the English.
Key takeaways
Direction comes from the locale through Directionality, not from the language of your strings.Without flutter_localizationsandLocale('ar')insupportedLocales, an Arabic app stays LTR.Directional APIs ( start,end,EdgeInsetsDirectional) flip; physical ones (left,right) never do.Only icons with matchTextDirectionmirror; numbers, logos and photos shouldn't.Test every screen in RTL, because an LTR review can't see these bugs.
التعليقات (0)