Shipping Arabic RTL in Flutter without layout bugs
Arabic RTL in Flutter: directional layout APIs, which icons to mirror, phone numbers that survive bidi, digits for Morocco and goldens in both directions.
On this page
Most RTL layout bugs in Flutter come from code that names a physical side: left, right, fromLTRB. Let the locale drive Directionality, write padding, alignment and position as start and end, and the framework mirrors most of the screen for you. What remains is a few decisions (icons, phone numbers, digits, fonts) and tests in both directions, because an RTL regression stays silent until an Arabic reader opens the screen.
I shipped Arabic in a production Flutter app, and this is the setup I reuse. Reading Arabic myself helps in review.
Register GlobalWidgetsLocalizations or nothing flips
Flutter doesn't infer direction from your strings. GlobalWidgetsLocalizations is the delegate that turns an ar locale into TextDirection.rtl for the whole tree. Leave it out and you get Arabic text in a left-to-right layout, the classic "RTL is broken" bug.
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
class App extends StatelessWidget {
const App({super.key, required this.locale});
final Locale locale; // from user settings
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: locale,
supportedLocales: const [Locale('ar'), Locale('fr')],
localizationsDelegates: const [
AppLocalizations.delegate, // generated by gen-l10n
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
// The one that makes Directionality RTL for 'ar'.
GlobalWidgetsLocalizations.delegate,
],
home: const HomeScreen(),
);
}
}Layout code should read Directionality.of(context), and formatting code should read the Locale. A condition like languageCode == 'ar' in a widget breaks the day you add a second RTL language or force a direction in a test.
I also don't wrap screens in their own Directionality. The only override I accept is a deliberate left-to-right island, such as a phone field.
Use the Directional variant of every layout API
Every physical layout API has a directional twin. Stick to the right-hand column and the framework does most of the RTL work.
| Instead of | Use |
|---|---|
EdgeInsets.only(left: 16) | EdgeInsetsDirectional.only(start: 16) |
EdgeInsets.fromLTRB(16, 8, 4, 8) | EdgeInsetsDirectional.fromSTEB(16, 8, 4, 8) |
Alignment.centerLeft | AlignmentDirectional.centerStart |
Positioned(right: 8) | PositionedDirectional(end: 8) |
BorderRadius.only(topLeft: ...) | BorderRadiusDirectional.only(topStart: ...) |
TextAlign.left | TextAlign.start |
abstract final class AppIcons {
// Directional glyph: Icon flips it automatically in RTL.
static const chevronNext =
IconData(0xe900, fontFamily: 'AppIcons', matchTextDirection: true);
}
class ListingCard extends StatelessWidget {
const ListingCard({super.key, required this.title, required this.price});
final String title;
final String price;
@override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
padding: const EdgeInsetsDirectional.fromSTEB(16, 12, 48, 12),
alignment: AlignmentDirectional.centerStart,
decoration: const BoxDecoration(
color: Color(0xFFF4F4F5),
borderRadius: BorderRadiusDirectional.only(topStart: Radius.circular(12)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(title), Text(price)],
),
),
const PositionedDirectional(top: 12, end: 12, child: Icon(AppIcons.chevronNext)),
],
);
}
}Row, CrossAxisAlignment.start, horizontal ListView and PageView already follow Directionality, so don't reverse children yourself based on the locale. The framework flips the row, then you flip it back. What doesn't flip is anything expressed as a raw Offset or drawn by hand: a CustomPainter, a Transform.translate, drag deltas. Multiply those by a sign derived from Directionality.of(context). A SlideTransition from Offset(1, 0) doesn't flip by default either, but it takes a textDirection argument: pass it Directionality.of(context) and it mirrors the x offset for you.
A custom lint rule is the clean way to ban physical APIs, but a grep in CI gets most of the value. It fails the build, and a human decides whether to fix the line or mark it // rtl-ok:
#!/usr/bin/env bash
# Fails CI when physical left/right layout APIs appear in lib/.
# Mark a deliberate exception with a trailing "// rtl-ok" comment.
set -euo pipefail
pattern='EdgeInsets\.(fromLTRB|only\([^)]*(left|right):)|Alignment\.(top|center|bottom)(Left|Right)|TextAlign\.(left|right)|Positioned\(|BorderRadius\.only\('
if grep -rnE "$pattern" lib --include='*.dart' | grep -v 'rtl-ok'; then
echo "Use the Directional (start/end) variants above."
exit 1
fiMirror arrows and chevrons, not play buttons or clocks
Mirroring is decided per icon. IconData carries a matchTextDirection flag, and Icon flips the glyph when the flag is set and the ambient direction is RTL. Image accepts the same flag for arrows shipped as assets.
I mirror what points along the reading direction or describes a sequence: back and forward arrows, chevrons, send and reply, list and indent icons, progress from start to end.
Physical objects and universal symbols stay as they are. Play and pause follow the tape, not the text. Checkmarks, clocks and circular arrows that stand for time, logos, and anything containing Latin letters or digits don't flip either.
Material already sets the flag on directional glyphs such as Icons.arrow_back and Icons.arrow_forward. For the rest, an RTL golden of the screen shows which icons flipped.
Phone numbers break in RTL, prices mostly don't
The Unicode bidi algorithm handles prose well and structured numbers badly. Text('+212 6 12 34 56 78') on an RTL screen renders as 78 56 34 12 6 212+. Each digit group is a left-to-right run, but the spaces and the plus sign take the paragraph's direction, so the groups themselves are laid out right to left.
The fix depends on where the number sits. A standalone value (phone field, IBAN, reference) gets textDirection: TextDirection.ltr on its Text or TextField. A fragment inside an Arabic sentence gets wrapped in Unicode isolates, LRI (U+2066) and PDI (U+2069): the paragraph stays RTL and the fragment keeps its order. Keep the sentence in the ARB file with a placeholder and isolate the value at the call site (ltrIsolated, in the next section).
Prices behave better. A single comma, period or no-break space between digits counts as part of the number, so 1,250.00 stays whole and only its side of the currency follows the paragraph, as an Arabic reader expects. intl's Arabic currency pattern even starts with a right-to-left mark, one more reason not to build these strings by concatenation.
Make the digit choice once, in one formatter
Arabic has two digit sets: Western (0-9) and Arabic-Indic (٠١٢٣). In Morocco, Western digits are the everyday norm in Arabic as in French, so that's my default there. Whichever you pick, make it a product decision written in one place, because intl's locale data has surprises:
arformats with Western digits,.for decimals and,for grouping.ar_EGswitches to Arabic-Indic digits and Arabic separators.- There is no
ar_MAdata.ar_MAquietly falls back toar, whose default currency is the Egyptian pound, so always pass the symbol. DateFormatprints native digits when the locale defines them;useNativeDigits = falseturns that off.- The
armonth names are the generic forms (مايو, أغسطس), not the Moroccan ones (ماي, غشت). Numeric dates sidestep the question.
import 'package:intl/intl.dart' as intl; // prefix: intl also exports a TextDirection
extension BidiIsolate on String {
/// LRI ... PDI: keeps a phone number or IBAN intact inside an RTL sentence.
String get ltrIsolated => '\u2066$this\u2069';
}
/// Product rule for a Moroccan audience: Western digits in every language.
class AppFormats {
AppFormats(String languageCode)
: _money = intl.NumberFormat.currency(
locale: languageCode, // 'ar' prints 0-9; 'ar_MA' would fall back to 'ar'
symbol: languageCode == 'ar' ? 'درهم' : 'DH', // never trust the default (EGP)
decimalDigits: 2,
),
// Guards against locales like 'ar_EG' that default to Arabic-Indic digits.
_date = intl.DateFormat.yMd(languageCode)..useNativeDigits = false;
final intl.NumberFormat _money;
final intl.DateFormat _date;
String money(num amount) => _money.format(amount);
String date(DateTime value) => _date.format(value);
}The import prefix matters: Flutter and intl both export a TextDirection, and unprefixed imports collide. In a plain unit test, call initializeDateFormatting('ar') before formatting dates. In the widget tree, the Material delegates load that data for you.
Bundle the font instead of trusting system Arabic
System fonts render Arabic, but metrics vary between iOS and Android vendors, so screens shift from one device to the next. Bundle a family and set it per script in the theme rather than in widgets.
Pick one that covers both scripts. IBM Plex Sans Arabic and Cairo include Latin glyphs, so a Latin brand name inside an Arabic label keeps the same metrics. With Noto Sans Arabic, add Noto Sans through fontFamilyFallback.
Keep letterSpacing off Arabic text, since it pulls apart letters that are meant to join. And leave vertical room: Arabic marks and descenders get clipped in fixed-height boxes and with tight height values.
Test both directions with the real font
flutter_test renders with a placeholder font, so load your real one, or the goldens show boxes instead of Arabic shaping. Then run layout-sensitive widgets in both directions, with geometric assertions for intent and a golden per direction for the rest.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
Widget harness(Widget child, TextDirection direction) => MaterialApp(
theme: ThemeData(fontFamily: 'IBMPlexSansArabic'),
home: Directionality(
textDirection: direction,
child: Scaffold(body: ListView(children: [child])), // as used in the app
),
);
void main() {
setUpAll(() async {
final loader = FontLoader('IBMPlexSansArabic')
..addFont(rootBundle.load('assets/fonts/IBMPlexSansArabic-Regular.ttf'));
await loader.load();
});
const card = ListingCard(title: 'شقة للبيع', price: '1,250,000 DH');
testWidgets('chevron sits on the end side in RTL', (tester) async {
await tester.pumpWidget(harness(card, TextDirection.rtl));
final box = tester.getRect(find.byType(ListingCard));
final chevron = tester.getRect(find.byIcon(AppIcons.chevronNext));
expect(chevron.center.dx, lessThan(box.center.dx)); // end = left in RTL
});
for (final direction in TextDirection.values) {
testWidgets('ListingCard golden, ${direction.name}', (tester) async {
await tester.pumpWidget(harness(card, direction));
await expectLater(find.byType(ListingCard),
matchesGoldenFile('goldens/listing_card_${direction.name}.png'));
});
}
}Font rasterization differs between macOS and Linux, so generate and compare goldens in one place, usually the CI image.
During development, a debug-only builder on MaterialApp that wraps the app in an RTL Directionality shows the French UI right to left. You can still read every label, and any hardcoded left jumps out.
Checklist for an RTL pull request
GlobalWidgetsLocalizations.delegateis registered, and layout code readsDirectionality.of(context), never the language code.- No
leftorrightin layout code, only theDirectionalvariants andTextAlign.start, with a CI grep behind it. - Directional icons declare
matchTextDirection: true; media, time and brand icons don't. - Phone numbers and IBANs render left to right:
TextDirection.ltrwhen standalone, LRI/PDI isolates inside sentences. - Amounts and dates go through one formatter with an explicit digit choice and currency symbol.
- The bundled font covers Arabic and Latin, with no letter spacing on Arabic and no fixed text heights.
- Layout-sensitive widgets have a golden per direction, generated on one platform.
- Animated and hand-drawn offsets are multiplied by a direction sign.