mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 18:38:36 +08:00
@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
|
|||||||
|
|
||||||
import 'package:gitjournal/core/note.dart';
|
import 'package:gitjournal/core/note.dart';
|
||||||
import 'package:gitjournal/utils/markdown.dart';
|
import 'package:gitjournal/utils/markdown.dart';
|
||||||
|
import 'package:gitjournal/widgets/highlighted_text.dart';
|
||||||
|
|
||||||
class NoteTile extends StatelessWidget {
|
class NoteTile extends StatelessWidget {
|
||||||
final Note note;
|
final Note note;
|
||||||
@ -45,12 +46,14 @@ class NoteTile extends StatelessWidget {
|
|||||||
child: Column(
|
child: Column(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
if (note.title != null && note.title.isNotEmpty)
|
if (note.title != null && note.title.isNotEmpty)
|
||||||
Text(
|
HighlightedText(
|
||||||
note.title,
|
text: note.title,
|
||||||
maxLines: 2,
|
maxLines: 2,
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
style: textTheme.headline6
|
style: textTheme.headline6
|
||||||
.copyWith(fontSize: textTheme.headline6.fontSize * 0.80),
|
.copyWith(fontSize: textTheme.headline6.fontSize * 0.80),
|
||||||
|
highlightText: searchTerm,
|
||||||
|
highlightTextLowerCase: searchTermLowerCase,
|
||||||
),
|
),
|
||||||
if (note.title != null && note.title.isNotEmpty)
|
if (note.title != null && note.title.isNotEmpty)
|
||||||
const SizedBox(height: 8.0),
|
const SizedBox(height: 8.0),
|
||||||
@ -106,49 +109,16 @@ class NoteTile extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildBody(BuildContext context, String text) {
|
Widget _buildBody(BuildContext context, String text) {
|
||||||
var theme = Theme.of(context);
|
var textTheme = Theme.of(context).textTheme;
|
||||||
var textTheme = theme.textTheme;
|
|
||||||
var style = textTheme.subtitle1
|
|
||||||
.copyWith(fontSize: textTheme.subtitle1.fontSize * 0.90);
|
|
||||||
|
|
||||||
if (searchTerm.isEmpty) {
|
return HighlightedText(
|
||||||
return Text(
|
text: text,
|
||||||
text,
|
highlightText: searchTerm,
|
||||||
maxLines: _maxLines - 1,
|
highlightTextLowerCase: searchTermLowerCase,
|
||||||
|
style: textTheme.subtitle1
|
||||||
|
.copyWith(fontSize: textTheme.subtitle1.fontSize * 0.90),
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
style: style,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
var i = text.toLowerCase().indexOf(searchTermLowerCase);
|
|
||||||
if (i == -1) {
|
|
||||||
return Text(
|
|
||||||
text,
|
|
||||||
maxLines: _maxLines - 1,
|
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),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
62
lib/widgets/highlighted_text.dart
Normal file
62
lib/widgets/highlighted_text.dart
Normal 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),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user