SelectableText class
A run of selectable text with a single style.
The SelectableText widget displays a string of text with a 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.
SelectableText(
'Hello! How are you?',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold),
)
Using the SelectableText.rich constructor, the SelectableText widget can display a paragraph with differently styled TextSpans. The sample that follows displays "Hello beautiful world" with different styles for each word.
const SelectableText.rich(
TextSpan(
text: 'Hello', // default text style
children: <TextSpan>[
TextSpan(text: ' beautiful ', style: TextStyle(fontStyle: FontStyle.italic)),
TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold)),
],
),
)
Interactivity
To make SelectableText react to touch events, use callback onTap to achieve the desired behavior.
See also:
- Inheritance
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- SelectableText
Constructors
-
SelectableText(String data,
{Key key, FocusNode focusNode, TextStyle style, StrutStyle strutStyle, TextAlign textAlign, TextDirection textDirection, double textScaleFactor, bool showCursor: false, bool autofocus: false, ToolbarOptions toolbarOptions, int minLines, int maxLines, double cursorWidth: 2.0, double cursorHeight, Radius cursorRadius, Color cursorColor, DragStartBehavior dragStartBehavior: DragStartBehavior.start, bool enableInteractiveSelection: true, GestureTapCallback onTap, ScrollPhysics scrollPhysics, TextHeightBehavior textHeightBehavior, TextWidthBasis textWidthBasis} ) -
Creates a selectable text widget. [...]
const
-
SelectableText.rich(TextSpan textSpan,
{Key key, FocusNode focusNode, TextStyle style, StrutStyle strutStyle, TextAlign textAlign, TextDirection textDirection, double textScaleFactor, bool showCursor: false, bool autofocus: false, ToolbarOptions toolbarOptions, int minLines, int maxLines, double cursorWidth: 2.0, double cursorHeight, Radius cursorRadius, Color cursorColor, DragStartBehavior dragStartBehavior: DragStartBehavior.start, bool enableInteractiveSelection: true, GestureTapCallback onTap, ScrollPhysics scrollPhysics, TextHeightBehavior textHeightBehavior, TextWidthBasis textWidthBasis} ) -
Creates a selectable text widget with a TextSpan. [...]
const
Properties
- autofocus → bool
-
Whether this text field should focus itself if nothing else is already
focused. [...]
final
- cursorColor → Color
-
The color to use when painting the cursor. [...]
final
- cursorHeight → double
-
How tall the cursor will be. [...]
final
- cursorRadius → Radius
-
How rounded the corners of the cursor should be. [...]
final
- cursorWidth → double
-
How thick the cursor will be. [...]
final
- data → String
-
The text to display. [...]
final
- dragStartBehavior → DragStartBehavior
-
Determines the way that drag start behavior is handled. [...]
final
- enableInteractiveSelection → bool
-
Whether to enable user interface affordances for changing the
text selection. [...]
final
- focusNode → FocusNode
-
Defines the focus for this widget. [...]
final
- hashCode → int
-
The hash code for this object. [...]
@nonVirtual, read-only, inherited
- key → Key
-
Controls how one widget replaces another widget in the tree. [...]
final, inherited
- maxLines → int
-
The maximum number of lines for the text to span, wrapping if necessary. [...]
final
- minLines → int
-
The minimum number of lines to occupy when the content spans fewer lines. [...]
final
- onTap → GestureTapCallback
-
Called when the user taps on this selectable text. [...]
final
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
- scrollPhysics → ScrollPhysics
-
The ScrollPhysics to use when vertically scrolling the input. [...]
final
- selectionEnabled → bool
-
Same as enableInteractiveSelection. [...]
read-only
- showCursor → bool
-
Whether to show cursor. [...]
final
- strutStyle → StrutStyle
-
The strut style used for the vertical layout. [...]
final
- style → TextStyle
-
The style to use for the text. [...]
final
- textAlign → TextAlign
-
How the text should be aligned horizontally. [...]
final
- textDirection → TextDirection
-
The directionality of the text. [...]
final
- textHeightBehavior → TextHeightBehavior
-
Defines how the paragraph will apply TextStyle.height to the ascent of the
first line and descent of the last line. [...]
final
- textScaleFactor → double
-
The number of font pixels for each logical pixel. [...]
final
- textSpan → TextSpan
-
The text to display as a TextSpan. [...]
final
- textWidthBasis → TextWidthBasis
-
Defines how to measure the width of the rendered text.
final
- toolbarOptions → ToolbarOptions
-
Configuration of toolbar options. [...]
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree. [...]
inherited
-
createState(
) → _SelectableTextState -
Creates the mutable state for this widget at a given location in the tree. [...]
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children. [...]
@protected, inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties ) → void -
Add additional properties associated with the node. [...]
override
-
noSuchMethod(
Invocation invocation ) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
toDiagnosticsNode(
{String name, DiagnosticsTreeStyle style} ) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep. [...]
inherited
-
toString(
{DiagnosticLevel minLevel: DiagnosticLevel.info} ) → String -
Returns a string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug} ) → String -
Returns a string representation of this node and its descendants. [...]
inherited
-
toStringShallow(
{String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug} ) → String -
Returns a one-line detailed description of the object. [...]
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other ) → bool -
The equality operator. [...]
@nonVirtual, inherited