diff --git a/lib/ui/desktop/left/list.dart b/lib/ui/desktop/left/list.dart index 7bed6b6..5245608 100644 --- a/lib/ui/desktop/left/list.dart +++ b/lib/ui/desktop/left/list.dart @@ -50,6 +50,8 @@ class DomainWidgetState extends State with AutomaticKeepAliveClientM //搜索的内容 SearchModel? searchModel; bool changing = false; //是否存在刷新任务 + //关键词高亮监听 + late VoidCallback highlightListener; changeState() { if (!changing) { @@ -70,6 +72,19 @@ class DomainWidgetState extends State 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 with AutomaticKeepAliveClientM return result; } + ///高亮处理 + highlightHandler() { + //获取所有请求Widget + List requests = containerMap.values + .map((e) => e.body) + .expand((element) => element) + .toList(); + for (RequestWidget request in requests) { + GlobalKey key = request.key as GlobalKey; + key.currentState?.setState(() {}); + } + } + ///添加请求 add(Channel channel, HttpRequest request) { container.add(request); diff --git a/lib/ui/desktop/left/request.dart b/lib/ui/desktop/left/request.dart index 00b16b1..11f9423 100644 --- a/lib/ui/desktop/left/request.dart +++ b/lib/ui/desktop/left/request.dart @@ -92,7 +92,7 @@ class _RequestWidgetState extends State { return highlightColor; } - return _KeywordHighlight.getHighlightColor(widget.request.uri); + return KeywordHighlightDialog.getHighlightColor(widget.request.uri); } void changeState() { @@ -220,7 +220,7 @@ class _RequestWidgetState extends State { 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 { } //配置关键词高亮 -class _KeywordHighlight extends StatelessWidget { +class KeywordHighlightDialog extends StatefulWidget { static Map keywords = {}; + static ValueNotifier keywordsController = ValueNotifier(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 createState() => _KeywordHighlightState(); +} + +class _KeywordHighlightState extends State { @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(); + } } diff --git a/lib/ui/mobile/request/list.dart b/lib/ui/mobile/request/list.dart index d066afa..90d1cb4 100644 --- a/lib/ui/mobile/request/list.dart +++ b/lib/ui/mobile/request/list.dart @@ -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 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(); } ///添加请求 diff --git a/lib/ui/mobile/widgets/highlight.dart b/lib/ui/mobile/widgets/highlight.dart index 26e29d6..0c50c53 100644 --- a/lib/ui/mobile/widgets/highlight.dart +++ b/lib/ui/mobile/widgets/highlight.dart @@ -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 keywords = {}; static bool enabled = true; + static ValueNotifier keywordsController = ValueNotifier(keywords); static Color? getHighlightColor(String? key) { if (key == null || !enabled) { @@ -20,6 +21,11 @@ class KeywordHighlight extends StatelessWidget { const KeywordHighlight({super.key}); + @override + State createState() => _KeywordHighlightState(); +} + +class _KeywordHighlightState extends State { @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(); + } }