How to select the Text in Flutter?

  1. text: it takes to text that needs to be displayed to the user and is selectable.
  2. onTap: it handles all the on-tap callbacks.
  3. autofocus: default value is false, it is used to enable or disable the focus of the cursor.
  4. maxLine: it defines the total number of lines of the text.
  5. toolbarOptions: it is used to create a toolbar configuration with given options, all options are set to false by default.
  6. enableIntractiveSelection: used to enable or disable the cut/copy/paste menu and select text on long press.
class SelectTextWidget extends StatelessWidget {
final String text;
const SelectTextWidget({
this.text,
});
@override
Widget build(BuildContext context) {
return Center(
child: SelectableText(
text,
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
fontSize: 45,
),
textAlign: TextAlign.center,
onTap: () => print('Tapped'),
toolbarOptions: ToolbarOptions(
copy: true,
selectAll: true,
),
showCursor: true,
cursorWidth: 2,
cursorColor: Colors.red,
cursorRadius: Radius.circular(5),
),
);
}
}
SelectableText("This is text")
class SelectRichTextWidget extends StatelessWidget {
final TextSpan textSpan;
const SelectRichTextWidget({
this.textSpan,
});
@override
Widget build(BuildContext context) {
return Center(
child: SelectableText.rich(
textSpan,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 48),
textAlign: TextAlign.center,
onTap: () => print('Tapped'),
toolbarOptions: ToolbarOptions(copy: true, selectAll: false),
showCursor: true,
cursorWidth: 2,
cursorColor: Colors.black,
cursorRadius: Radius.circular(5),
),
);
}
}
SelectRichTextWidget(
textSpan: TextSpan(
children: <TextSpan>[
TextSpan(
text: ' this ',
style: TextStyle(
fontStyle: FontStyle.italic,
),
),
TextSpan(
text: 'is',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.green,
),
),
TextSpan(
text: 'text',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
],
),
)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store