right-to-left (RTL) in flutter

58,338

Solution 1

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
];

Solution 2

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

Solution 3

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

Solution 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,
    ),

Solution 5

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

Share:
58,338
AbdulMomen عبدالمؤمن
Author by

AbdulMomen عبدالمؤمن

Hello there! ;) I like computers, mathematics, philosophy, logic, music and religion.

Updated on February 16, 2022

Comments

  • AbdulMomen عبدالمؤمن
    AbdulMomen عبدالمؤمن over 2 years

    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?

  • Son of Stackoverflow
    Son of Stackoverflow almost 5 years
    You can even use it for Text widget
  • CopsOnRoad
    CopsOnRoad over 4 years
    @dod_basim did any of the other solution work for you?
  • Elia Weiss
    Elia Weiss over 4 years
    _rtlLanguages is not use
  • Elia Weiss
    Elia Weiss over 4 years
    error: The argument type 'TextDirection (where TextDirection is defined in D:\flutter\.pub-cache\hosted\pub.dartlang.org\intl-0.15.8\li‌​b\src\intl\bidi_util‌​s.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)
  • CopsOnRoad
    CopsOnRoad over 4 years
    @EliaWeiss Make sure you don't import any other conflicting package, this is the one you should be using import package:flutter/material.dart;
  • aderchox
    aderchox over 4 years
    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.
  • Pradeep
    Pradeep almost 4 years
    How to set dynamic when button click from any page? As user wants to change RTL to LTR then how to do?
  • Bhargav Sejpal
    Bhargav Sejpal almost 4 years
    The getter 'rtl' isn't defined for the type 'TextDirection'.
  • DJafari
    DJafari almost 4 years
    @BhargavSejpal if you imported intlpackage, you must import intl with name : import 'package:intl/intl.dart' as intl
  • mm sh
    mm sh almost 3 years
    @mohammad Hi , please answer my question in stackoverflow.com/questions/68528499
  • mm sh
    mm sh almost 3 years
    @DJafari Hi, please answer my question in stackoverflow.com/questions/68528499
  • Miguel Beltran
    Miguel Beltran almost 3 years
    Why do you need to override the text scale factor?
  • jksevend
    jksevend about 2 years
    @DJafari is there any way of obtaining or changing the current textDirection ?
  • DJafari
    DJafari about 2 years
    @jksevend please ask your question in another question and insert link here