Skip to main content

TextDirection

Enum TextDirection 

Source
pub enum TextDirection {
    Rtl,
    Ltr,
}
Expand description

A direction in which text flows.

Some languages are written from the left to the right (for example, English, Tamil, or Chinese), while others are written from the right to the left (for example Aramaic, Hebrew, or Urdu). Some are also written in a mixture, for example Arabic is mostly written right-to-left, with numerals written left-to-right.

The text direction must be provided to APIs that render text or lay out boxes horizontally, so that they can determine which direction to start in: either right-to-left, TextDirection::Rtl; or left-to-right, TextDirection::Ltr.

§Design discussion

Flutter is designed to address the needs of applications written in any of the world’s currently-used languages, whether they use a right-to-left or left-to-right writing direction. Flutter does not support other writing modes, such as vertical text or boustrophedon text, as these are rarely used in computer programs.

It is common when developing user interface frameworks to pick a default text direction — typically left-to-right, the direction most familiar to the engineers working on the framework — because this simplifies the development of applications on the platform. Unfortunately, this frequently results in the platform having unexpected left-to-right biases or assumptions, as engineers will typically miss places where they need to support right-to-left text. This then results in bugs that only manifest in right-to-left environments.

In an effort to minimize the extent to which Flutter experiences this category of issues, the lowest levels of the Flutter framework do not have a default text reading direction. Any time a reading direction is necessary, for example when text is to be displayed, or when a writing-direction-dependent value is to be interpreted, the reading direction must be explicitly specified. Where possible, such as in match statements, the right-to-left case is listed first, to avoid the impression that it is an afterthought.

At the higher levels (specifically starting at the widgets library), an ambient Directionality is introduced, which provides a default. Thus, for instance, a Text widget in the scope of a MaterialApp widget does not need to be given an explicit writing direction. The Directionality.of static method can be used to obtain the ambient text direction for a particular BuildContext.

§Known left-to-right biases in Flutter

Despite the design intent described above, certain left-to-right biases have nonetheless crept into Flutter’s design. These include:

  • The Canvas origin is at the top left, and the x-axis increases in a left-to-right direction.

  • The default localization in the widgets and material libraries is American English, which is left-to-right.

§Visual properties vs directional properties

Many classes in the Flutter framework are offered in two versions, a visually-oriented variant, and a text-direction-dependent variant. For example, EdgeInsets is described in terms of top, left, right, and bottom, while EdgeInsetsDirectional is described in terms of top, start, end, and bottom, where start and end correspond to right and left in right-to-left text and left and right in left-to-right text.

There are distinct use cases for each of these variants.

Text-direction-dependent variants are useful when developing user interfaces that should “flip” with the text direction. For example, a paragraph of text in English will typically be left-aligned and a quote will be indented from the left, while in Arabic it will be right-aligned and indented from the right. Both of these cases are described by the direction-dependent TextAlign::Start and EdgeInsetsDirectional start.

In contrast, the visual variants are useful when the text direction is known and not affected by the reading direction. For example, an application giving driving directions might show a “turn left” arrow on the left and a “turn right” arrow on the right — and would do so whether the application was localized to French (left-to-right) or Hebrew (right-to-left).

In practice, it is also expected that many developers will only be targeting one language, and in that case it may be simpler to think in visual terms.

The order of this enum must match the order of the values in TextDirection.h’s TextDirection.

Variants§

§

Rtl

The text flows from right to left (e.g. Arabic, Hebrew).

§

Ltr

The text flows from left to right (e.g., English, French).

Trait Implementations§

Source§

impl Clone for TextDirection

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for TextDirection

Source§

impl Debug for TextDirection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for TextDirection

Source§

impl Hash for TextDirection

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for TextDirection

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TextDirection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,