mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-08-14 17:41:30 +08:00

This results in the highlighted text not always being visible. I'm not sure how to fix that, it seems rather complex.
52 lines
1.3 KiB
Dart
52 lines
1.3 KiB
Dart
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, overflow: overflow, style: style);
|
|
}
|
|
|
|
var i = text.toLowerCase().indexOf(highlightTextLowerCase);
|
|
if (i == -1) {
|
|
return Text(text, maxLines: maxLines, overflow: overflow, style: style);
|
|
}
|
|
|
|
var 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 RichText(
|
|
maxLines: maxLines,
|
|
text: TextSpan(
|
|
children: [
|
|
TextSpan(text: before, style: style),
|
|
TextSpan(text: term, style: highlightStyle),
|
|
TextSpan(text: after, style: style),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|