51

I was using Flutter more than a week, and wanted to create an Arabic (right-to-left) app.

I was reading Internationalizing Flutter Apps, but it didn't mention how to set the layout direction.

So, how to show right-to-left (RTL) layout in Flutter?

  • 1
    If you just need to set the text direction set the `textDirection` property to TextDirection.rtl your TextField or Text widget. – Son of Stackoverflow Sep 01 '19 at 19:20
  • How to restrict a widget to change its children's alignment when the locale is changed? Could you please solve this https://stackoverflow.com/questions/65180615/restrict-a-widget-to-change-its-childrens-alignment-when-locale-is-changed-flut – Febin K R Dec 07 '20 at 15:16

5 Answers5

117

you have two choices :

1. force a locale ( and direction ) on all devices

-- method 1: with localization

add flutter_localizations package to your pubspec.yml

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

then

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,
  .
  .
  .
);

-- method 2: without localization

MaterialApp(
  .
  .
  .
  builder: (context, child) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: child,
    );
  },
  .
  .
  .
);

2. set layout direction according to device locale ( if user phone locale is a RTL language and exist in supportedLocales, your app run in RTL mode, otherwise your app is LTR )

add flutter_localizations package to your pubspec.yml

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

then

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
  ],
  .
  .
  .
);

note : rtl languages in flutter are:

[
  'ar', // Arabic
  'fa', // Farsi
  'he', // Hebrew
  'ps', // Pashto
  'ur', // Urdu
];
mohammad
  • 1,129
  • 1
  • 9
  • 24
DJafari
  • 9,798
  • 8
  • 36
  • 56
28

You need to create a Builder and pass it the layout direction using 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
    Although it's working, I don't like this answer. You have provided no explanation about how it works or how doing this affects other layouting calculations and etc. – aderchox Jan 20 '20 at 16:55
22

The best and shortest way to set RTL configuration for the entire app.

void main() {
  runApp(
    MaterialApp(
      home: Directionality( // add this
        textDirection: TextDirection.rtl, // set this property 
        child: HomePage(),
      ),
    ),
  );
}
CopsOnRoad
  • 109,635
  • 30
  • 367
  • 257
  • @dod_basim did any of the other solution work for you? – CopsOnRoad Sep 28 '19 at 05:32
  • error: The argument type 'TextDirection (where TextDirection is defined in D:\flutter\.pub-cache\hosted\pub.dartlang.org\intl-0.15.8\lib\src\intl\bidi_utils.dart)' can't be assigned to the parameter type 'TextDirection (where TextDirection is defined in D:\flutter\bin\cache\pkg\sky_engine\lib\ui\text.dart)'. (argument_type_not_assignable at [zapit] lib\main.dart:186) – Elia Weiss Dec 04 '19 at 16:25
  • @EliaWeiss Make sure you don't import any other conflicting package, this is the one you should be using import `package:flutter/material.dart`; – CopsOnRoad Dec 05 '19 at 03:31
  • How to set dynamic when button click from any page? As user wants to change RTL to LTR then how to do? – Pradeep Jul 14 '20 at 11:17
4

If you need to display text in reverse direction then just set it's textdirection property to TextDirection.rtl.

Example code for TextField widget,

TextField(
  textDirection: TextDirection.rtl,
  decoration: InputDecoration(
    labelText: "Enter Pashto Name",
  ),
),

Example code for Text widget,

    Text(
      "This text is in the other direction!"
      textDirection: TextDirection.rtl,
    ),
Abdurahman Popal
  • 1,562
  • 16
  • 15
3

The SfRangeSelector supports changing the layout direction of the widget in the right-to-left direction by setting the textDirection property to rtl in the Directionality widget.

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,
                 ),
              ),
            )
         ),
      )
  );
}

enter image description here

Ans From: https://help.syncfusion.com/flutter/range-selector/right-to-left

Paresh Mangukiya
  • 14,668
  • 7
  • 90
  • 90