This package is used to highlight, in a completely dynamic way, keywords, or phrases, wherever they are present in a specified text.
DynamicTextHighlighting is also the name of the package widget. It returns a RichText widget and it accepts four basic parameters:
text
: the whole texthighlights
: the words to be highlightedcolor
: the highlight colorstyleHighlights
: the default text style to the words to be highlightedstyle
: the default text stylecaseSensitive
: the case sensitive option
In addition to these parameters, there are others related to RichText widget:
TextAlign
textAlignTextDirection
textDirectionbool
softWrapTextOverflow
overflowdouble
textScaleFactorint
maxLinesLocale
localeStrutStyle
strutStyleTextWidthBasis
textWidthBasisTextHeightBehavior
textHeightBehavior
Widget buildDTH(String text, List<String> highlights) {
return DynamicTextHighlighting(
text: text,
highlights: highlights,
color: Colors.yellow,
style: TextStyle(
fontSize: 18.0,
fontStyle: FontStyle.italic,
),
styleHighlights: TextStyle(
color: Colors.redAccent,
fontSize: 18.0,
fontStyle: FontStyle.italic,
),
caseSensitive: false,
);
}
It is a stateless widget, so for any changes
just call setState(() {...})
.
void applyChanges(List<String> newHighlights) {
setState(() {
highlights = newHighlights;
});
}