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

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/utils/markdown.dart';
import 'package:gitjournal/widgets/highlighted_text.dart';
class NoteTile extends StatelessWidget {
final Note note;
@ -45,12 +46,14 @@ class NoteTile extends StatelessWidget {
child: Column(
children: <Widget>[
if (note.title != null && note.title.isNotEmpty)
Text(
note.title,
HighlightedText(
text: note.title,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: textTheme.headline6
.copyWith(fontSize: textTheme.headline6.fontSize * 0.80),
highlightText: searchTerm,
highlightTextLowerCase: searchTermLowerCase,
),
if (note.title != null && note.title.isNotEmpty)
const SizedBox(height: 8.0),
@ -106,49 +109,16 @@ class NoteTile extends StatelessWidget {
}
Widget _buildBody(BuildContext context, String text) {
var theme = Theme.of(context);
var textTheme = theme.textTheme;
var style = textTheme.subtitle1
.copyWith(fontSize: textTheme.subtitle1.fontSize * 0.90);
var textTheme = Theme.of(context).textTheme;
if (searchTerm.isEmpty) {
return Text(
text,
maxLines: _maxLines - 1,
overflow: TextOverflow.ellipsis,
style: style,
);
}
var i = text.toLowerCase().indexOf(searchTermLowerCase);
if (i == -1) {
return Text(
text,
maxLines: _maxLines - 1,
overflow: TextOverflow.ellipsis,
style: style,
);
}
var highlightStyle = textTheme.subtitle1.copyWith(
fontSize: textTheme.subtitle1.fontSize * 0.90,
backgroundColor: theme.highlightColor,
);
var before = text.substring(0, i);
var after = text.substring(i + searchTerm.length);
return RichText(
text: TextSpan(
children: [
TextSpan(text: before, style: style),
TextSpan(
text: searchTerm,
style: highlightStyle,
),
TextSpan(text: after, style: style),
],
),
return HighlightedText(
text: text,
highlightText: searchTerm,
highlightTextLowerCase: searchTermLowerCase,
style: textTheme.subtitle1
.copyWith(fontSize: textTheme.subtitle1.fontSize * 0.90),
overflow: TextOverflow.ellipsis,
maxLines: _maxLines - 1,
);
}
}

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),
],
),
);
}
}