Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Widget coloredSearchText(BuildContext context,
- {String? searchKey, required String searchText, TextStyle? style}) {
- final search = searchKey?.toLowerCase();
- final text = searchText;
- final textSpans = <TextSpan>[];
- var lastEnd = 0;
- for (final match in (search ?? '').allMatches(text.toLowerCase())) {
- if (match.start > lastEnd) {
- textSpans.add(
- TextSpan(
- text: text.substring(lastEnd, match.start),
- style: style ?? DefaultTextStyle.of(context).style,
- ),
- );
- }
- textSpans.add(
- TextSpan(
- text: text.substring(match.start, match.end),
- style: style?.copyWith(color: context.primaryColor),
- ),
- );
- lastEnd = match.end;
- }
- if (lastEnd < text.length) {
- textSpans.add(
- TextSpan(
- text: text.substring(lastEnd),
- style: style ?? DefaultTextStyle.of(context).style,
- ),
- );
- }
- return Text.rich(TextSpan(children: textSpans, style: style));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement