right-to-left (RTL) in flutter
Asked Answered
R

7

93

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?

Richella answered 25/5, 2018 at 18:45 Comment(2)
If you just need to set the text direction set the textDirection property to TextDirection.rtl your TextField or Text widget.Henry
How to restrict a widget to change its children's alignment when the locale is changed? Could you please solve this #65181115Primaveria
L
204

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
];
Loot answered 14/2, 2019 at 11:54 Comment(8)
_rtlLanguages is not useMyosotis
The getter 'rtl' isn't defined for the type 'TextDirection'.Burchette
@BhargavSejpal if you imported intlpackage, you must import intl with name : import 'package:intl/intl.dart' as intlLoot
@mohammad Hi , please answer my question in stackoverflow.com/questions/68528499Tucky
@Loot Hi, please answer my question in stackoverflow.com/questions/68528499Tucky
@Loot is there any way of obtaining or changing the current textDirection ?Sperry
@Sperry please ask your question in another question and insert link hereLoot
is it possible to RTL a MaterialBanner ? i asked this Question here: #74020951Maffa
J
52

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(),
      ),
    ),
  );
}
Jeanniejeannine answered 10/9, 2019 at 4:8 Comment(5)
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)Myosotis
@EliaWeiss Make sure you don't import any other conflicting package, this is the one you should be using import package:flutter/material.dart;Jeanniejeannine
How to set dynamic when button click from any page? As user wants to change RTL to LTR then how to do?Trunkfish
this answer is incorect, because this make only home page rtl, other pages must wrap with Directionality too, check my second method of choise 1Loot
@Loot This will make all pages rtl not just the HomePage. If you navigate to other pages from your HomePage, you'll see this in effect.Jeanniejeannine
R
44

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,
                    );
                  },
                ),
              );
            },
          .
          .
          .
        );
Richella answered 25/5, 2018 at 18:45 Comment(2)
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.Colettacolette
Why do you need to override the text scale factor?Stereograph
C
12

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,
    ),
Cockhorse answered 1/9, 2019 at 18:35 Comment(2)
You can even use it for Text widgetHenry
best Answer , Worked for me shortcutImplication
S
2

Just append this to your material app:

 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,
                    );
                  },
                ),
              );
            },
Sward answered 4/7, 2021 at 15:38 Comment(0)
M
1

if you use flutter_localizations: sdk: flutter

add these line to change your app direction

 supportedLocales: [
    Locale("fa", "IR"), 
    Locale("en", 'US'),
  ],
  locale: Locale("fa", "IR")  // this is important line if not add this Apps will not change direction
Mackenziemackerel answered 4/8, 2022 at 5:34 Comment(0)
W
1

GetMaterialApp( textDirection: TextDirection.rtl, home: SignUpScreen() // const HomeExpert() );

Whiplash answered 18/12, 2022 at 14:55 Comment(3)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Eddington
This is only for Get StateManager , what about provider or generic?Glede
@SayedIdrees No working for both GetMaterialApp and MaterialAppWhiplash

© 2022 - 2024 — McMap. All rights reserved.