From cbc75e45e9271d9fc3195e19c64e2a2bcbc9050c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 7 Sep 2020 17:38:07 +0200 Subject: [PATCH] NoteTile: Also Highlight the title if it matches Related to #14 --- lib/folder_views/note_tile.dart | 58 +++++++---------------------- lib/widgets/highlighted_text.dart | 62 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 44 deletions(-) create mode 100644 lib/widgets/highlighted_text.dart diff --git a/lib/folder_views/note_tile.dart b/lib/folder_views/note_tile.dart index be513d24..0c386f78 100644 --- a/lib/folder_views/note_tile.dart +++ b/lib/folder_views/note_tile.dart @@ -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: [ 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, ); } } diff --git a/lib/widgets/highlighted_text.dart b/lib/widgets/highlighted_text.dart new file mode 100644 index 00000000..4c866171 --- /dev/null +++ b/lib/widgets/highlighted_text.dart @@ -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), + ], + ), + ); + } +}