From 7ead81e3bffc7ef9d08f0a48292115411a3fa7ae Mon Sep 17 00:00:00 2001 From: adesr Date: Wed, 23 Feb 2022 16:11:55 +0700 Subject: [PATCH] Make text selectable --- lib/substring_highlight.dart | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/substring_highlight.dart b/lib/substring_highlight.dart index c7d50ab..1c816f8 100644 --- a/lib/substring_highlight.dart +++ b/lib/substring_highlight.dart @@ -22,8 +22,8 @@ class SubstringHighlight extends StatelessWidget { ), this.wordDelimiters = ' .,;?!<>[]~`@#\$%^&*()+-=|\/_', this.words = - false // default is to match substrings (hence the package name!) - + false, // default is to match substrings (hence the package name!) + this.selectable = false }) : assert(term != null || terms != null); @@ -64,6 +64,9 @@ class SubstringHighlight extends StatelessWidget { /// If true then match complete words only (instead of characters or substrings within words). This feature is in ALPHA... use 'words' AT YOUR OWN RISK!!! final bool words; + + /// If true then text is selectable + final bool selectable; @override Widget build(BuildContext context) { @@ -158,11 +161,17 @@ class SubstringHighlight extends StatelessWidget { } } - return RichText( - maxLines: maxLines, - overflow: overflow, - text: TextSpan(children: children, style: textStyle), - textAlign: textAlign, - textScaleFactor: MediaQuery.of(context).textScaleFactor); + return selectable + ? SelectableText.rich(TextSpan(children: children, style: textStyle), + maxLines: maxLines, + // overflow: overflow, + textAlign: textAlign, + textScaleFactor: MediaQuery.of(context).textScaleFactor) + : RichText( + maxLines: maxLines, + overflow: overflow, + text: TextSpan(children: children, style: textStyle), + textAlign: textAlign, + textScaleFactor: MediaQuery.of(context).textScaleFactor); } }