mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 18:38:36 +08:00
RepoSelector: Hilight selected text
This commit is contained in:
@ -14,6 +14,7 @@ import 'package:gitjournal/setup/button.dart';
|
|||||||
import 'package:gitjournal/setup/error.dart';
|
import 'package:gitjournal/setup/error.dart';
|
||||||
import 'package:gitjournal/setup/loading.dart';
|
import 'package:gitjournal/setup/loading.dart';
|
||||||
import 'package:gitjournal/utils/logger.dart';
|
import 'package:gitjournal/utils/logger.dart';
|
||||||
|
import 'package:gitjournal/widgets/highlighted_text.dart';
|
||||||
|
|
||||||
//import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
//import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||||
|
|
||||||
@ -139,6 +140,7 @@ class GitHostSetupRepoSelectorState extends State<GitHostSetupRepoSelector> {
|
|||||||
for (var repo in filteredRepos)
|
for (var repo in filteredRepos)
|
||||||
_RepoTile(
|
_RepoTile(
|
||||||
repo: repo,
|
repo: repo,
|
||||||
|
searchText: q,
|
||||||
onTap: () {
|
onTap: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
selectedRepo = repo;
|
selectedRepo = repo;
|
||||||
@ -241,11 +243,13 @@ class GitHostSetupRepoSelectorState extends State<GitHostSetupRepoSelector> {
|
|||||||
|
|
||||||
class _RepoTile extends StatelessWidget {
|
class _RepoTile extends StatelessWidget {
|
||||||
final GitHostRepo repo;
|
final GitHostRepo repo;
|
||||||
|
final String searchText;
|
||||||
final Function onTap;
|
final Function onTap;
|
||||||
final bool selected;
|
final bool selected;
|
||||||
|
|
||||||
_RepoTile({
|
_RepoTile({
|
||||||
@required this.repo,
|
@required this.repo,
|
||||||
|
@required this.searchText,
|
||||||
@required this.onTap,
|
@required this.onTap,
|
||||||
@required this.selected,
|
@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(
|
return ListTile(
|
||||||
title: Text(repo.fullName),
|
title: title,
|
||||||
trailing: _SmartDateTime(repo.updatedAt, textTheme.caption),
|
trailing: _SmartDateTime(repo.updatedAt, textTheme.caption),
|
||||||
selected: selected,
|
selected: selected,
|
||||||
contentPadding: const EdgeInsets.all(0.0),
|
contentPadding: const EdgeInsets.all(0.0),
|
||||||
|
@ -6,6 +6,7 @@ class HighlightedText extends StatelessWidget {
|
|||||||
final String highlightTextLowerCase;
|
final String highlightTextLowerCase;
|
||||||
|
|
||||||
final TextStyle style;
|
final TextStyle style;
|
||||||
|
final TextStyle highlightStyle;
|
||||||
final TextOverflow overflow;
|
final TextOverflow overflow;
|
||||||
final int maxLines;
|
final int maxLines;
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ class HighlightedText extends StatelessWidget {
|
|||||||
@required this.highlightText,
|
@required this.highlightText,
|
||||||
@required this.highlightTextLowerCase,
|
@required this.highlightTextLowerCase,
|
||||||
@required this.style,
|
@required this.style,
|
||||||
|
this.highlightStyle,
|
||||||
this.overflow,
|
this.overflow,
|
||||||
this.maxLines,
|
this.maxLines,
|
||||||
});
|
});
|
||||||
@ -29,9 +31,10 @@ class HighlightedText extends StatelessWidget {
|
|||||||
return Text(text, maxLines: maxLines, overflow: overflow, style: style);
|
return Text(text, maxLines: maxLines, overflow: overflow, style: style);
|
||||||
}
|
}
|
||||||
|
|
||||||
var highlightStyle = style.copyWith(
|
var highlightStyle = this.highlightStyle ??
|
||||||
backgroundColor: Theme.of(context).highlightColor,
|
style.copyWith(
|
||||||
);
|
backgroundColor: Theme.of(context).highlightColor,
|
||||||
|
);
|
||||||
|
|
||||||
var before = text.substring(0, i);
|
var before = text.substring(0, i);
|
||||||
var term = text.substring(i, i + highlightText.length);
|
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),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user