The SelectableText widget is a Flutter widget that displays a piece of text and allows the user to select and copy the text. It’s a read-only text widget that supports text selection, similar to the Text
widget, but with the added functionality of text selection. A Text that the user may pick and copy is shown using the SelectableText widget in Flutter. It offers a method of displaying static text with text selection and clipboard copying supported right out of the box.
Here are some key features of the SelectableText widget:
- Text display: Displays a piece of text.
- Text selection: Allows users to select text using a long press or double tap.
- Copy to clipboard: Allows users to copy the selected text to the clipboard.
- Customizable: Can be customized with various properties, such as text style, font size, color, and more.
- Read-only: The text cannot be edited by the user. It will be only readable
When and Where to Use Selectable Text:
- Displaying terms and conditions or privacy policies
- Showing code snippets or examples
- Displaying text that users may want to share or copy
A simple example of using the SelectableText widget:
SelectableText(
'This is some selectable text You Can Copy .',
style: TextStyle(fontSize: 20),
cursorColor: Colors.green,
)
Comparison of SelectableText and Text widgets in Flutter :
SelectableText:
- Allows text selection and copying
- Supports cursor and text selection handles
- Can be used for displaying text that users may want to copy
- Has a cursor color property to customize the cursor color
- Has a selectionControls property to customize the selection handles
Text Widget:
- Does not allow text selection or copying by default
- Can be made selectable using the selectable property
- Does not support cursor and text selection handles
- Suitable for displaying text that doesn’t need to be copied
- Has a wider range of styling options, such as textAlign, overflow, and softWrap
Code Comparison:
SelectableText(
'Selectable text', // The text displayed by the widget, which users can select and copy.
cursorColor: Colors.blue, // Sets the color of the text cursor when selecting text.
)
Text(
'Non-selectable text', // The text displayed by the widget, which is static and cannot be selected.
style: TextStyle(
fontSize: 20, // Sets the font size of the text.
),
)