NoteTile: Also Highlight the title if it matches

Related to #14
This commit is contained in:
Vishesh Handa
2020-09-07 17:38:07 +02:00
parent a84da10d77
commit cbc75e45e9
2 changed files with 76 additions and 44 deletions

View File

@ -0,0 +1,62 @@
import 'package:flutter/material.dart';
class HighlightedText extends StatelessWidget {
final String text;
final String highlightText;
final String highlightTextLowerCase;
final TextStyle style;
final TextOverflow overflow;
final int maxLines;
HighlightedText({
@required this.text,
@required this.highlightText,
@required this.highlightTextLowerCase,
@required this.style,
this.overflow,
this.maxLines,
});
@override
Widget build(BuildContext context) {
if (highlightText.isEmpty) {
return Text(
text,
maxLines: maxLines - 1,
overflow: overflow,
style: style,
);
}
var i = text.toLowerCase().indexOf(highlightTextLowerCase);
if (i == -1) {
return Text(
text,
maxLines: maxLines - 1,
overflow: overflow,
style: style,
);
}
var highlightStyle = style.copyWith(
backgroundColor: Theme.of(context).highlightColor,
);
var before = text.substring(0, i);
var after = text.substring(i + highlightText.length);
return RichText(
text: TextSpan(
children: [
TextSpan(text: before, style: style),
TextSpan(
text: highlightText,
style: highlightStyle,
),
TextSpan(text: after, style: style),
],
),
);
}
}