RepoSelector: Hilight selected text

This commit is contained in:
Vishesh Handa
2020-11-13 16:03:28 +01:00
parent df78b24542
commit 9bf9193ec8
2 changed files with 70 additions and 4 deletions

View File

@ -14,6 +14,7 @@ import 'package:gitjournal/setup/button.dart';
import 'package:gitjournal/setup/error.dart';
import 'package:gitjournal/setup/loading.dart';
import 'package:gitjournal/utils/logger.dart';
import 'package:gitjournal/widgets/highlighted_text.dart';
//import 'package:font_awesome_flutter/font_awesome_flutter.dart';
@ -139,6 +140,7 @@ class GitHostSetupRepoSelectorState extends State<GitHostSetupRepoSelector> {
for (var repo in filteredRepos)
_RepoTile(
repo: repo,
searchText: q,
onTap: () {
setState(() {
selectedRepo = repo;
@ -241,11 +243,13 @@ class GitHostSetupRepoSelectorState extends State<GitHostSetupRepoSelector> {
class _RepoTile extends StatelessWidget {
final GitHostRepo repo;
final String searchText;
final Function onTap;
final bool selected;
_RepoTile({
@required this.repo,
@required this.searchText,
@required this.onTap,
@required this.selected,
});
@ -291,8 +295,28 @@ class _RepoTile extends StatelessWidget {
),
); */
var style = Theme.of(context).textTheme.subtitle1;
Widget title = Text(repo.fullName, style: style);
if (searchText.isNotEmpty) {
title = title = RichText(
text: TextSpan(
children: [
TextSpan(text: repo.username + '/', style: style),
...HighlightTextSpan(
text: repo.name,
highlightText: searchText,
highlightTextLowerCase: searchText,
style: style,
highlightStyle: style.copyWith(fontWeight: FontWeight.bold),
).build(context),
],
),
);
}
return ListTile(
title: Text(repo.fullName),
title: title,
trailing: _SmartDateTime(repo.updatedAt, textTheme.caption),
selected: selected,
contentPadding: const EdgeInsets.all(0.0),

View File

@ -6,6 +6,7 @@ class HighlightedText extends StatelessWidget {
final String highlightTextLowerCase;
final TextStyle style;
final TextStyle highlightStyle;
final TextOverflow overflow;
final int maxLines;
@ -14,6 +15,7 @@ class HighlightedText extends StatelessWidget {
@required this.highlightText,
@required this.highlightTextLowerCase,
@required this.style,
this.highlightStyle,
this.overflow,
this.maxLines,
});
@ -29,9 +31,10 @@ class HighlightedText extends StatelessWidget {
return Text(text, maxLines: maxLines, overflow: overflow, style: style);
}
var highlightStyle = style.copyWith(
backgroundColor: Theme.of(context).highlightColor,
);
var highlightStyle = this.highlightStyle ??
style.copyWith(
backgroundColor: Theme.of(context).highlightColor,
);
var before = text.substring(0, i);
var term = text.substring(i, i + highlightText.length);
@ -49,3 +52,42 @@ class HighlightedText extends StatelessWidget {
);
}
}
class HighlightTextSpan {
final String text;
final String highlightText;
final String highlightTextLowerCase;
final TextStyle style;
final TextStyle highlightStyle;
HighlightTextSpan({
@required this.text,
@required this.highlightText,
@required this.highlightTextLowerCase,
@required this.style,
this.highlightStyle,
});
List<InlineSpan> build(BuildContext context) {
var i = text.toLowerCase().indexOf(highlightTextLowerCase);
if (i == -1) {
return [];
}
var highlightStyle = this.highlightStyle ??
style.copyWith(
backgroundColor: Theme.of(context).highlightColor,
);
var before = text.substring(0, i);
var term = text.substring(i, i + highlightText.length);
var after = text.substring(i + highlightText.length);
return [
TextSpan(text: before, style: style),
TextSpan(text: term, style: highlightStyle),
TextSpan(text: after, style: style),
];
}
}