Request update highlight match

This commit is contained in:
wanghongenpin
2024-10-27 17:32:27 +08:00
parent eacac6f45c
commit 1e38f0eee9
3 changed files with 24 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ import 'package:flutter/material.dart';
/// @author: Hongen Wang
class HighlightTextEditingController extends TextEditingController {
RegExp? highlightPattern;
String? splitPattern;
//
bool highlightEnabled = true;
@@ -43,8 +44,20 @@ class HighlightTextEditingController extends TextEditingController {
final highlightStyle = style?.copyWith(color: color);
final normalStyle = style;
List<TextSpan> spans = [];
int start = 0;
if (splitPattern != null) {
var texts = text.split(splitPattern!);
for (var i = 0; i < texts.length; i++) {
matchHighlight(texts[i], spans, normalStyle: normalStyle, highlightStyle: highlightStyle);
spans.add(TextSpan(text: splitPattern, style: normalStyle));
}
} else {
matchHighlight(text, spans, normalStyle: normalStyle, highlightStyle: highlightStyle);
}
return TextSpan(children: spans, style: style);
}
matchHighlight(String text, List<TextSpan> spans, {TextStyle? normalStyle, TextStyle? highlightStyle}) {
int start = 0;
for (final match in highlightPattern!.allMatches(text)) {
if (match.start > start) {
spans.add(TextSpan(text: text.substring(start, match.start), style: normalStyle));
@@ -56,8 +69,6 @@ class HighlightTextEditingController extends TextEditingController {
if (start < text.length) {
spans.add(TextSpan(text: text.substring(start), style: normalStyle));
}
return TextSpan(children: spans, style: style);
}
}

View File

@@ -161,6 +161,7 @@ class _RewriteUpdateAddState extends State<RewriteUpdateAddDialog> {
var typeList = widget.ruleType == RuleType.requestUpdate ? RewriteType.updateRequest : RewriteType.updateResponse;
return AlertDialog(
scrollable: true,
titlePadding: const EdgeInsets.only(top: 10, left: 20),
actionsPadding: const EdgeInsets.only(right: 15, bottom: 15),
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
@@ -183,7 +184,7 @@ class _RewriteUpdateAddState extends State<RewriteUpdateAddDialog> {
],
content: Container(
width: 500,
constraints: const BoxConstraints(maxHeight: 400),
constraints: const BoxConstraints(maxHeight: 420, minHeight: 400),
child: Form(
key: formKey,
child: Column(children: [
@@ -208,8 +209,8 @@ class _RewriteUpdateAddState extends State<RewriteUpdateAddDialog> {
onChanged: (val) {
setState(() {
rewriteType = val!;
initTestData();
});
initTestData();
})),
],
),
@@ -234,6 +235,7 @@ class _RewriteUpdateAddState extends State<RewriteUpdateAddDialog> {
}
initTestData() {
dataController.splitPattern = null;
dataController.highlightEnabled = rewriteType != RewriteType.addQueryParam && rewriteType != RewriteType.addHeader;
bool isRemove = [RewriteType.removeHeader, RewriteType.removeQueryParam].contains(rewriteType);
@@ -253,6 +255,7 @@ class _RewriteUpdateAddState extends State<RewriteUpdateAddDialog> {
}
if (rewriteType == RewriteType.updateQueryParam || rewriteType == RewriteType.removeQueryParam) {
dataController.splitPattern = '&';
dataController.text = Uri.decodeQueryComponent(widget.request?.requestUri?.query ?? '');
return;
}
@@ -278,7 +281,7 @@ class _RewriteUpdateAddState extends State<RewriteUpdateAddDialog> {
onMatch = true;
//高亮显示
Future.delayed(const Duration(milliseconds: 500), () {
Future.delayed(const Duration(milliseconds: 600), () {
onMatch = false;
if (dataController.text.isEmpty) {
if (isMatch) return;

View File

@@ -204,8 +204,8 @@ class _RewriteUpdateAddState extends State<RewriteUpdateEdit> {
onChanged: (val) {
setState(() {
rewriteType = val!;
initTestData();
});
initTestData();
})),
],
),
@@ -226,6 +226,7 @@ class _RewriteUpdateAddState extends State<RewriteUpdateEdit> {
}
initTestData() {
dataController.splitPattern = null;
dataController.highlightEnabled = rewriteType != RewriteType.addQueryParam && rewriteType != RewriteType.addHeader;
bool isRemove = [RewriteType.removeHeader, RewriteType.removeQueryParam].contains(rewriteType);
@@ -245,6 +246,7 @@ class _RewriteUpdateAddState extends State<RewriteUpdateEdit> {
}
if (rewriteType == RewriteType.updateQueryParam || rewriteType == RewriteType.removeQueryParam) {
dataController.splitPattern = '&';
dataController.text = Uri.decodeQueryComponent(widget.request?.requestUri?.query ?? '');
return;
}
@@ -270,7 +272,7 @@ class _RewriteUpdateAddState extends State<RewriteUpdateEdit> {
onMatch = true;
//高亮显示
Future.delayed(const Duration(milliseconds: 500), () {
Future.delayed(const Duration(milliseconds: 600), () {
onMatch = false;
if (dataController.text.isEmpty) {
if (isMatch) return;