findInNote: Highlight the current result if a bit differently

This commit is contained in:
Vishesh Handa
2021-10-11 15:08:17 +02:00
parent afc530e4bd
commit 2d4892795a
2 changed files with 50 additions and 2 deletions

View File

@ -245,6 +245,15 @@ class MarkdownEditorState extends State<MarkdownEditor>
@override
void scrollToResult(String text, int num) {
setState(() {
_textController = buildController(
text: _textController.text,
highlightText: text,
theme: widget.theme,
currentPos: num,
);
});
var body = _textController.text.toLowerCase();
text = text.toLowerCase();

View File

@ -8,12 +8,16 @@ import 'package:flutter/material.dart';
class RichTextController extends TextEditingController {
final String highlightText;
final int currentPos;
final Color highlightBackgroundColor;
final Color highlightCurrentBackgroundColor;
RichTextController({
required String text,
required this.highlightText,
required this.currentPos,
required this.highlightBackgroundColor,
required this.highlightCurrentBackgroundColor,
}) : super(text: text);
@override
@ -25,13 +29,19 @@ class RichTextController extends TextEditingController {
var regexp = RegExp(RegExp.escape(highlightText), caseSensitive: false);
var children = <TextSpan>[];
var index = 0;
var _ = text.splitMapJoin(
regexp,
onMatch: (Match m) {
var backgroundColor = index != currentPos
? highlightBackgroundColor
: highlightCurrentBackgroundColor;
children.add(TextSpan(
text: m[0],
style: style?.copyWith(backgroundColor: highlightBackgroundColor),
style: style?.copyWith(backgroundColor: backgroundColor),
));
index++;
return "";
},
onNonMatch: (String span) {
@ -48,14 +58,43 @@ TextEditingController buildController({
required String text,
required String? highlightText,
required ThemeData theme,
int currentPos = -1,
}) {
if (highlightText != null) {
var color = theme.textSelectionTheme.selectionColor!;
var currentColor = theme.brightness != Brightness.light
? color.lighten(0.2)
: color.darken(0.2);
return RichTextController(
text: text,
highlightText: highlightText,
highlightBackgroundColor: theme.textSelectionTheme.selectionColor!,
currentPos: currentPos,
highlightBackgroundColor: color,
highlightCurrentBackgroundColor: currentColor,
);
} else {
return TextEditingController(text: text);
}
}
extension ColorBrightness on Color {
Color darken([double amount = .1]) {
assert(amount >= 0 && amount <= 1);
final hsl = HSLColor.fromColor(this);
final hslDark = hsl.withLightness((hsl.lightness - amount).clamp(0.0, 1.0));
return hslDark.toColor();
}
Color lighten([double amount = .1]) {
assert(amount >= 0 && amount <= 1);
final hsl = HSLColor.fromColor(this);
final hslLight =
hsl.withLightness((hsl.lightness + amount).clamp(0.0, 1.0));
return hslLight.toColor();
}
}