Merge pull request #178 from gentlemanxzh/optimize_highlight

Optimize highlight function
This commit is contained in:
wanghongenpin
2024-03-31 17:01:58 +08:00
committed by GitHub
4 changed files with 84 additions and 11 deletions

View File

@@ -50,6 +50,8 @@ class DomainWidgetState extends State<DomainList> with AutomaticKeepAliveClientM
//搜索的内容
SearchModel? searchModel;
bool changing = false; //是否存在刷新任务
//关键词高亮监听
late VoidCallback highlightListener;
changeState() {
if (!changing) {
@@ -70,6 +72,19 @@ class DomainWidgetState extends State<DomainList> with AutomaticKeepAliveClientM
DomainRequests domainRequests = getDomainRequests(request);
domainRequests.addRequest(request.requestId, request);
}
highlightListener = () {
//回调时机在高亮设置页面dispose之后。所以需要在下一帧刷新否则会报错
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
highlightHandler();
});
};
KeywordHighlightDialog.keywordsController.addListener(highlightListener);
}
@override
dispose() {
KeywordHighlightDialog.keywordsController.removeListener(highlightListener);
super.dispose();
}
@override
@@ -115,6 +130,19 @@ class DomainWidgetState extends State<DomainList> with AutomaticKeepAliveClientM
return result;
}
///高亮处理
highlightHandler() {
//获取所有请求Widget
List<RequestWidget> requests = containerMap.values
.map((e) => e.body)
.expand((element) => element)
.toList();
for (RequestWidget request in requests) {
GlobalKey key = request.key as GlobalKey<State>;
key.currentState?.setState(() {});
}
}
///添加请求
add(Channel channel, HttpRequest request) {
container.add(request);

View File

@@ -92,7 +92,7 @@ class _RequestWidgetState extends State<RequestWidget> {
return highlightColor;
}
return _KeywordHighlight.getHighlightColor(widget.request.uri);
return KeywordHighlightDialog.getHighlightColor(widget.request.uri);
}
void changeState() {
@@ -220,7 +220,7 @@ class _RequestWidgetState extends State<RequestWidget> {
MenuItem(
label: localizations.keyword,
onClick: (_) {
showDialog(context: context, builder: (BuildContext context) => const _KeywordHighlight());
showDialog(context: context, builder: (BuildContext context) => const KeywordHighlightDialog());
}),
],
);
@@ -287,8 +287,9 @@ class _RequestWidgetState extends State<RequestWidget> {
}
//配置关键词高亮
class _KeywordHighlight extends StatelessWidget {
class KeywordHighlightDialog extends StatefulWidget {
static Map<Color, String> keywords = {};
static ValueNotifier keywordsController = ValueNotifier<Map>(keywords);
static Color? getHighlightColor(String key) {
for (var entry in keywords.entries) {
@@ -299,8 +300,13 @@ class _KeywordHighlight extends StatelessWidget {
return null;
}
const _KeywordHighlight();
const KeywordHighlightDialog({super.key});
@override
State<KeywordHighlightDialog> createState() => _KeywordHighlightState();
}
class _KeywordHighlightState extends State<KeywordHighlightDialog> {
@override
Widget build(BuildContext context) {
AppLocalizations localizations = AppLocalizations.of(context)!;
@@ -312,7 +318,7 @@ class _KeywordHighlight extends StatelessWidget {
Colors.grey: localizations.gray,
};
var map = Map.of(keywords);
var map = Map.of(KeywordHighlightDialog.keywords);
return AlertDialog(
title: ListTile(
@@ -329,7 +335,7 @@ class _KeywordHighlight extends StatelessWidget {
TextButton(
child: Text(localizations.done),
onPressed: () {
keywords = map;
KeywordHighlightDialog.keywords = map;
Navigator.of(context).pop();
},
),
@@ -373,4 +379,10 @@ class _KeywordHighlight extends StatelessWidget {
border: const OutlineInputBorder(),
);
}
@override
void dispose() {
KeywordHighlightDialog.keywordsController.value = Map.from(KeywordHighlightDialog.keywords);
super.dispose();
}
}

View File

@@ -13,6 +13,7 @@ import 'package:network_proxy/network/host_port.dart';
import 'package:network_proxy/network/http/http.dart';
import 'package:network_proxy/ui/desktop/left/model/search_model.dart';
import 'package:network_proxy/ui/mobile/request/request.dart';
import 'package:network_proxy/ui/mobile/widgets/highlight.dart';
import 'package:network_proxy/utils/har.dart';
import 'package:network_proxy/utils/listenable_list.dart';
import 'package:share_plus/share_plus.dart';
@@ -152,10 +153,26 @@ class RequestSequenceState extends State<RequestSequence> with AutomaticKeepAliv
//搜索的内容
SearchModel? searchModel;
//关键词高亮监听
late VoidCallback highlightListener;
@override
initState() {
super.initState();
view.addAll(widget.container.source.reversed);
highlightListener = () {
//回调时机在高亮设置页面dispose之后。所以需要在下一帧刷新否则会报错
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
setState(() {});
});
};
KeywordHighlight.keywordsController.addListener(highlightListener);
}
@override
dispose() {
KeywordHighlight.keywordsController.removeListener(highlightListener);
super.dispose();
}
///添加请求

View File

@@ -2,9 +2,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:network_proxy/ui/component/widgets.dart';
class KeywordHighlight extends StatelessWidget {
class KeywordHighlight extends StatefulWidget {
static Map<Color, String> keywords = {};
static bool enabled = true;
static ValueNotifier keywordsController = ValueNotifier<Map>(keywords);
static Color? getHighlightColor(String? key) {
if (key == null || !enabled) {
@@ -20,6 +21,11 @@ class KeywordHighlight extends StatelessWidget {
const KeywordHighlight({super.key});
@override
State<KeywordHighlight> createState() => _KeywordHighlightState();
}
class _KeywordHighlightState extends State<KeywordHighlight> {
@override
Widget build(BuildContext context) {
AppLocalizations localizations = AppLocalizations.of(context)!;
@@ -36,7 +42,7 @@ class KeywordHighlight extends StatelessWidget {
title: Text(localizations.keyword + localizations.highlight,
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
actions: [
SwitchWidget(scale: 0.7, value: enabled, onChanged: (val) => enabled = val),
SwitchWidget(scale: 0.7, value: KeywordHighlight.enabled, onChanged: (val) => KeywordHighlight.enabled = val),
const SizedBox(width: 10)
],
),
@@ -51,12 +57,12 @@ class KeywordHighlight extends StatelessWidget {
child: TextFormField(
minLines: 2,
maxLines: 2,
initialValue: keywords[e.key],
initialValue: KeywordHighlight.keywords[e.key],
onChanged: (value) {
if (value.isEmpty) {
keywords.remove(e.key);
KeywordHighlight.keywords.remove(e.key);
} else {
keywords[e.key] = value;
KeywordHighlight.keywords[e.key] = value;
}
},
decoration: decoration(localizations.keyword),
@@ -75,4 +81,14 @@ class KeywordHighlight extends StatelessWidget {
border: const OutlineInputBorder(),
);
}
@override
void dispose() {
if(KeywordHighlight.enabled){
KeywordHighlight.keywordsController.value = Map.from(KeywordHighlight.keywords);
} else {
KeywordHighlight.keywordsController.value = {};
}
super.dispose();
}
}