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:
RichText, which gives you more control over the text styles.DefaultTextStyle, which sets default styles forTextwidgets.
Dart’s named constructor arguments are the fluent setters
(Text::new("Hi").max_lines(1)).
Fields§
§key: Option<KeyRef>See StatelessWidget::key.
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
impl Text
Sourcepub fn new(data: impl Into<String>) -> Text
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.
Sourcepub fn rich(text_span: InlineSpanRef) -> Text
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 childrenInlineSpans.WidgetSpans define embedded inline widgets.
See RichText which provides a lower-level way to draw text.
Sourcepub fn strut_style(self, strut_style: StrutStyle) -> Text
pub fn strut_style(self, strut_style: StrutStyle) -> Text
Dart Text(strutStyle:).
Sourcepub fn text_align(self, text_align: TextAlign) -> Text
pub fn text_align(self, text_align: TextAlign) -> Text
Dart Text(textAlign:).
Sourcepub fn text_direction(self, text_direction: TextDirection) -> Text
pub fn text_direction(self, text_direction: TextDirection) -> Text
Dart Text(textDirection:).
Sourcepub fn overflow(self, overflow: TextOverflow) -> Text
pub fn overflow(self, overflow: TextOverflow) -> Text
Dart Text(overflow:).
Sourcepub fn text_scaler(self, text_scaler: TextScaler) -> Text
pub fn text_scaler(self, text_scaler: TextScaler) -> Text
Dart Text(textScaler:).
Sourcepub fn semantics_label(self, semantics_label: impl Into<String>) -> Text
pub fn semantics_label(self, semantics_label: impl Into<String>) -> Text
Dart Text(semanticsLabel:).
Sourcepub fn semantics_identifier(
self,
semantics_identifier: impl Into<String>,
) -> Text
pub fn semantics_identifier( self, semantics_identifier: impl Into<String>, ) -> Text
Dart Text(semanticsIdentifier:).
Sourcepub fn text_width_basis(self, text_width_basis: TextWidthBasis) -> Text
pub fn text_width_basis(self, text_width_basis: TextWidthBasis) -> Text
Dart Text(textWidthBasis:).
Sourcepub fn text_height_behavior(
self,
text_height_behavior: TextHeightBehavior,
) -> Text
pub fn text_height_behavior( self, text_height_behavior: TextHeightBehavior, ) -> Text
Dart Text(textHeightBehavior:).
Sourcepub fn selection_color(self, selection_color: Color) -> Text
pub fn selection_color(self, selection_color: Color) -> Text
Dart Text(selectionColor:).
Trait Implementations§
Source§impl StatelessWidget for Text
impl StatelessWidget for Text
Source§fn debug_fill_properties(
&self,
_app: &App,
properties: &mut DiagnosticPropertiesBuilder,
)
fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )
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
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef
Source§fn debug_check_constructor(&self)
fn debug_check_constructor(&self)
Source§fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
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 Textwhere
*const RcInner<dyn Key> is !null: Freeze,
PhantomData<RcInner<dyn Key>>: Freeze,
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
*const RcInner<dyn InlineSpan> is !null: Freeze,
PhantomData<RcInner<dyn InlineSpan>>: Freeze,
&'static dyn ColorExtension: Freeze,
*const RcInner<dyn ColorExtension> is !null: Freeze,
PhantomData<RcInner<dyn ColorExtension>>: Freeze,
PhantomData<String>: Freeze,
*const RcInner<Paint> is !null: Freeze,
PhantomData<RcInner<Paint>>: Freeze,
PhantomData<Shadow>: Freeze,
PhantomData<FontFeature>: Freeze,
PhantomData<FontVariation>: Freeze,
*const TextScaler is !null: Freeze,
PhantomData<TextScaler>: Freeze,
impl Move for Text
impl Unpin for Textwhere
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 Textwhere
*const RcInner<dyn Key> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Key>>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
*const RcInner<dyn InlineSpan> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn InlineSpan>>: UnsafeUnpin,
&'static dyn ColorExtension: UnsafeUnpin,
*const RcInner<dyn ColorExtension> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn ColorExtension>>: UnsafeUnpin,
PhantomData<String>: UnsafeUnpin,
*const RcInner<Paint> is !null: UnsafeUnpin,
PhantomData<RcInner<Paint>>: UnsafeUnpin,
PhantomData<Shadow>: UnsafeUnpin,
PhantomData<FontFeature>: UnsafeUnpin,
PhantomData<FontVariation>: UnsafeUnpin,
*const TextScaler is !null: UnsafeUnpin,
PhantomData<TextScaler>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<W> IntoWidget<StatelessKind> for Wwhere
W: StatelessWidget,
impl<W> IntoWidget<StatelessKind> for Wwhere
W: StatelessWidget,
Source§fn into_widget(self) -> Rc<dyn Widget>
fn into_widget(self) -> Rc<dyn Widget>
creation_location.