From f94a1491bfecde8f1772d39864e9259f296b1506 Mon Sep 17 00:00:00 2001 From: chen08209 Date: Sat, 14 Feb 2026 22:00:05 +0800 Subject: [PATCH] cache --- lib/views/profiles/overwrite/overwrite.dart | 15 ++--- lib/views/profiles/preview.dart | 61 +++++++++++++++++++++ lib/views/profiles/profiles.dart | 11 +--- 3 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 lib/views/profiles/preview.dart diff --git a/lib/views/profiles/overwrite/overwrite.dart b/lib/views/profiles/overwrite/overwrite.dart index 3d194cb..455dac0 100644 --- a/lib/views/profiles/overwrite/overwrite.dart +++ b/lib/views/profiles/overwrite/overwrite.dart @@ -2,15 +2,14 @@ import 'package:fl_clash/common/common.dart'; import 'package:fl_clash/controller.dart'; -import 'package:fl_clash/core/controller.dart'; import 'package:fl_clash/enum/enum.dart'; import 'package:fl_clash/features/overwrite/rule.dart'; import 'package:fl_clash/models/models.dart'; -import 'package:fl_clash/pages/editor.dart'; import 'package:fl_clash/providers/database.dart'; import 'package:fl_clash/providers/providers.dart'; import 'package:fl_clash/state.dart'; import 'package:fl_clash/views/config/scripts.dart'; +import 'package:fl_clash/views/profiles/preview.dart'; import 'package:fl_clash/widgets/widgets.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; @@ -37,13 +36,7 @@ class _OverwriteViewState extends ConsumerState { if (profile == null) { return; } - final configMap = await appController.getProfileWithId(profile.id); - final content = await encodeYamlTask(configMap); - if (!mounted) { - return; - } - final previewPage = EditorPage(title: profile.realLabel, content: content); - BaseNavigator.push(context, previewPage); + BaseNavigator.push(context, PreviewProfileView(profile: profile)); } @override @@ -181,10 +174,10 @@ class _StandardContent extends ConsumerStatefulWidget { const _StandardContent(this.profileId); @override - ConsumerState createState() => __StandardContentState(); + ConsumerState createState() => _StandardContentState(); } -class __StandardContentState extends ConsumerState<_StandardContent> { +class _StandardContentState extends ConsumerState<_StandardContent> { final _key = utils.id; Future _handleAddOrUpdate([Rule? rule]) async { diff --git a/lib/views/profiles/preview.dart b/lib/views/profiles/preview.dart new file mode 100644 index 0000000..792b1ca --- /dev/null +++ b/lib/views/profiles/preview.dart @@ -0,0 +1,61 @@ +import 'package:fl_clash/common/task.dart'; +import 'package:fl_clash/controller.dart'; +import 'package:fl_clash/models/profile.dart'; +import 'package:fl_clash/pages/editor.dart'; +import 'package:fl_clash/widgets/loading.dart'; +import 'package:fl_clash/widgets/scaffold.dart'; +import 'package:flutter/material.dart'; + +class PreviewProfileView extends StatefulWidget { + final Profile profile; + + const PreviewProfileView({super.key, required this.profile}); + + @override + State createState() => _PreviewProfileViewState(); +} + +class _PreviewProfileViewState extends State { + final contentNotifier = ValueNotifier(null); + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) async { + final configMap = await appController.getProfileWithId(widget.profile.id); + final content = await encodeYamlTask(configMap); + if (!mounted) { + return; + } + contentNotifier.value = content; + }); + } + + @override + void dispose() { + contentNotifier.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return ValueListenableBuilder( + valueListenable: contentNotifier, + builder: (_, content, _) { + final title = widget.profile.realLabel; + if (content == null) { + return CommonScaffold( + title: title, + body: Center( + child: SizedBox.square( + dimension: 200, + child: CommonCircleLoading(), + ), + ), + ); + } + return EditorPage(title: title, content: content); + }, + ); + } +} diff --git a/lib/views/profiles/profiles.dart b/lib/views/profiles/profiles.dart index 482f5c0..116ff5b 100644 --- a/lib/views/profiles/profiles.dart +++ b/lib/views/profiles/profiles.dart @@ -2,7 +2,6 @@ import 'package:fl_clash/common/common.dart'; import 'package:fl_clash/controller.dart'; import 'package:fl_clash/enum/enum.dart'; import 'package:fl_clash/models/models.dart'; -import 'package:fl_clash/pages/editor.dart'; import 'package:fl_clash/providers/providers.dart'; import 'package:fl_clash/state.dart'; import 'package:fl_clash/views/profiles/overwrite/overwrite.dart'; @@ -13,6 +12,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'add.dart'; import 'edit.dart'; +import 'preview.dart'; class ProfilesView extends StatefulWidget { const ProfilesView({super.key}); @@ -198,14 +198,7 @@ class ProfileItem extends StatelessWidget { } Future _handlePreview(BuildContext context) async { - final configMap = await appController.getProfileWithId(profile.id); - final content = await encodeYamlTask(configMap); - if (!context.mounted) { - return; - } - - final previewPage = EditorPage(title: profile.realLabel, content: content); - BaseNavigator.push(context, previewPage); + BaseNavigator.push(context, PreviewProfileView(profile: profile)); } Future updateProfile() async {