Skip to main content

Text

Struct Text 

Source
pub struct Text {
Show 16 fields pub key: Option<KeyRef>, pub data: Option<String>, pub text_span: Option<InlineSpanRef>, pub style: Option<TextStyle>, pub strut_style: Option<StrutStyle>, pub text_align: Option<TextAlign>, pub text_direction: Option<TextDirection>, pub soft_wrap: Option<bool>, pub overflow: Option<TextOverflow>, pub text_scaler: Option<TextScaler>, pub max_lines: Option<i32>, pub semantics_label: Option<String>, pub semantics_identifier: Option<String>, pub text_width_basis: Option<TextWidthBasis>, pub text_height_behavior: Option<TextHeightBehavior>, pub selection_color: Option<Color>,
}
Expand description

A run of text with a single style.

The Text widget displays a string of text with single style. The string might break across multiple lines or might all be displayed on the same line depending on the layout constraints.

The style argument is optional. When omitted, the text will use the style from the closest enclosing DefaultTextStyle. If the given style’s TextStyle.inherit property is true (the default), the given style will be merged with the closest enclosing DefaultTextStyle. This merging behavior is useful, for example, to make the text bold while using the default font family and size.

This example shows how to display text using the Text widget with the overflow set to TextOverflow::Ellipsis.

Text::new("Hello, how are you?").overflow(TextOverflow::Ellipsis)

Setting max_lines to 1 is not equivalent to disabling soft wrapping with soft_wrap. This is apparent when using TextOverflow::Fade: with max_lines(1) a second line overflows and the fade appears in a horizontal direction at the bottom; with soft_wrap(false) the single line overflows and the fade appears in a vertical direction at the right.

Using the rich constructor, the Text widget can display a paragraph with differently styled TextSpans.

§Interactivity

To make Text react to touch events, wrap it in a GestureDetector widget with a GestureDetector.on_tap handler.

To make sections of the text interactive, use RichText and specify a TapGestureRecognizer as the TextSpan.recognizer of the relevant part of the text.

§Selection

Text is not selectable by default. To make a Text selectable, one can wrap a subtree with a SelectionArea widget.

See also:

Dart’s named constructor arguments are the fluent setters (Text::new("Hi").max_lines(1)).

Fields§

§key: Option<KeyRef>§data: Option<String>

The text to display.

This will be None if a text_span is provided instead.

§text_span: Option<InlineSpanRef>

The text to display as an InlineSpan.

This will be None if data is provided instead.

§style: Option<TextStyle>

If non-null, the style to use for this text.

If the style’s “inherit” property is true, the style will be merged with the closest enclosing DefaultTextStyle. Otherwise, the style will replace the closest enclosing DefaultTextStyle.

§strut_style: Option<StrutStyle>

The strut style to use. Strut style defines the strut, which sets minimum vertical layout metrics.

Omitting or providing None will disable strut.

The user or platform may override this strut’s StrutStyle::height via a MediaQuery ancestor’s MediaQueryData.lineHeightScaleFactorOverride.

§text_align: Option<TextAlign>

How the text should be aligned horizontally.

§text_direction: Option<TextDirection>

The directionality of the text.

This decides how text_align values like TextAlign::Start and TextAlign::End are interpreted.

This is also used to disambiguate how to render bidirectional text. For example, if the data is an English phrase followed by a Hebrew phrase, in a TextDirection::Ltr context the English phrase will be on the left and the Hebrew phrase to its right, while in a TextDirection::Rtl context, the English phrase will be on the right and the Hebrew phrase on its left.

Defaults to the ambient Directionality, if any.

§soft_wrap: Option<bool>

Whether the text should break at soft line breaks.

If false, the glyphs in the text will be positioned as if there was unlimited horizontal space.

§overflow: Option<TextOverflow>

How visual overflow should be handled.

If this is None, TextStyle.overflow will be used, otherwise the value from the nearest DefaultTextStyle ancestor will be used.

§text_scaler: Option<TextScaler>

The font scaling strategy to use when laying out and rendering the text.

§max_lines: Option<i32>

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated according to overflow.

If this is 1, text will not wrap. Otherwise, text will be wrapped at the edge of the box.

If this is None, but there is an ambient DefaultTextStyle that specifies an explicit number for its DefaultTextStyle.max_lines, then the DefaultTextStyle value will take precedence. You can use a RichText widget directly to entirely override the DefaultTextStyle.

§semantics_label: Option<String>

An alternative semantics label for this text.

If present, the semantics of this widget will contain this value instead of the actual text. This will overwrite any of the semantics labels applied directly to the TextSpans.

This is useful for replacing abbreviations or shorthands with the full text value: Text::new("$$").semantics_label("Double dollars").

§semantics_identifier: Option<String>

A unique identifier for the semantics node for this widget.

This is useful for cases where the text widget needs to have a uniquely identifiable ID that is recognized through the automation tools without having a dependency on the actual content of the text that can possibly be dynamic in nature.

§text_width_basis: Option<TextWidthBasis>

Defines how to measure the width of the rendered text.

§text_height_behavior: Option<TextHeightBehavior>

Defines how to apply TextStyle.height over and under text.

§selection_color: Option<Color>

The color to use when painting the selection.

This is ignored if SelectionContainer::maybe_of returns None in the BuildContext of this widget.

If None, the ambient DefaultSelectionStyle is used (if any); failing that, the selection color defaults to DefaultSelectionStyle::DEFAULT_COLOR.

Implementations§

Source§

impl Text

Source

pub fn new(data: impl Into<String>) -> Text

Creates a text widget.

If the style argument is None, the text will use the style from the closest enclosing DefaultTextStyle.

The overflow property’s behavior is affected by the soft_wrap argument. If the soft_wrap is true or None, the glyph causing overflow, and those that follow, will not be rendered. Otherwise, it will be shown with the given overflow option.

Source

pub fn rich(text_span: InlineSpanRef) -> Text

Creates a text widget with an InlineSpan.

The following subclasses of InlineSpan may be used to build rich text:

  • TextSpans define text and children InlineSpans.
  • WidgetSpans define embedded inline widgets.

See RichText which provides a lower-level way to draw text.

Source

pub fn key(self, key: KeyRef) -> Text

Dart Text(key:).

Source

pub fn style(self, style: TextStyle) -> Text

Dart Text(style:).

Source

pub fn strut_style(self, strut_style: StrutStyle) -> Text

Dart Text(strutStyle:).

Source

pub fn text_align(self, text_align: TextAlign) -> Text

Dart Text(textAlign:).

Source

pub fn text_direction(self, text_direction: TextDirection) -> Text

Dart Text(textDirection:).

Source

pub fn soft_wrap(self, soft_wrap: bool) -> Text

Dart Text(softWrap:).

Source

pub fn overflow(self, overflow: TextOverflow) -> Text

Dart Text(overflow:).

Source

pub fn text_scaler(self, text_scaler: TextScaler) -> Text

Dart Text(textScaler:).

Source

pub fn max_lines(self, max_lines: i32) -> Text

Dart Text(maxLines:).

Source

pub fn semantics_label(self, semantics_label: impl Into<String>) -> Text

Dart Text(semanticsLabel:).

Source

pub fn semantics_identifier( self, semantics_identifier: impl Into<String>, ) -> Text

Dart Text(semanticsIdentifier:).

Source

pub fn text_width_basis(self, text_width_basis: TextWidthBasis) -> Text

Dart Text(textWidthBasis:).

Source

pub fn text_height_behavior( self, text_height_behavior: TextHeightBehavior, ) -> Text

Dart Text(textHeightBehavior:).

Source

pub fn selection_color(self, selection_color: Color) -> Text

Dart Text(selectionColor:).

Trait Implementations§

Source§

impl Debug for Text

Source§

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

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

impl StatelessWidget for Text

Source§

fn key(&self) -> Option<&KeyRef>

Source§

fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )

Dart’s debugFillProperties: the properties a dump shows for this widget, in Dart’s order. Dart’s Widget base only sets the dense tree style, which the framework does.
Source§

fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef

Describes the part of the user interface represented by this widget. Read more
Source§

fn debug_check_constructor(&self)

Dart’s constructor asserts that relate two or more arguments. Read more
Source§

fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>

Dart’s this is PreferredSizeWidget: a widget that implements PreferredSizeWidget answers with itself, which is what a parent asking an erased child for its preferred size reads.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Text

§

impl !Send for Text

§

impl !Sync for Text

§

impl !UnwindSafe for Text

§

impl Forget for Text

§

impl Freeze for Text

§

impl Move for Text

§

impl Unpin for Text
where Rc<dyn Key>: Unpin, *const u8 is !null: Unpin, Rc<dyn InlineSpan>: Unpin, &'static dyn ColorExtension: Unpin, Rc<dyn ColorExtension>: Unpin, Rc<Paint>: Unpin, Box<TextScaler>: Unpin,

§

impl UnsafeUnpin for Text

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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<W> IntoWidget<StatelessKind> for W
where W: StatelessWidget,

Source§

fn into_widget(self) -> Rc<dyn Widget>

The widget as a tree node, made where this is called: its creation_location.
Source§

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

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>