From 91a447b016a67fc536005633a75bc52b3e5a27ed Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 28 Nov 2023 15:55:42 +0800 Subject: [PATCH] Fix bug --- lib/platform/replace_current_route.dart | 1 + lib/platform/replace_current_route_none.dart | 3 +++ lib/platform/replace_current_route_web.dart | 13 +++++++++++++ lib/viewer/single.dart | 13 ++++++++++--- 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 lib/platform/replace_current_route.dart create mode 100644 lib/platform/replace_current_route_none.dart create mode 100644 lib/platform/replace_current_route_web.dart diff --git a/lib/platform/replace_current_route.dart b/lib/platform/replace_current_route.dart new file mode 100644 index 0000000..9835554 --- /dev/null +++ b/lib/platform/replace_current_route.dart @@ -0,0 +1 @@ +export 'replace_current_route_none.dart' if (dart.library.html) 'replace_current_route_web.dart'; \ No newline at end of file diff --git a/lib/platform/replace_current_route_none.dart b/lib/platform/replace_current_route_none.dart new file mode 100644 index 0000000..24ba811 --- /dev/null +++ b/lib/platform/replace_current_route_none.dart @@ -0,0 +1,3 @@ +void replaceCurrentRoute(String query) { + throw UnimplementedError(); +} diff --git a/lib/platform/replace_current_route_web.dart b/lib/platform/replace_current_route_web.dart new file mode 100644 index 0000000..5c07d3d --- /dev/null +++ b/lib/platform/replace_current_route_web.dart @@ -0,0 +1,13 @@ +// ignore: avoid_web_libraries_in_flutter +import 'dart:html'; + +void replaceCurrentRoute(String query) { + const usePathUrl = bool.fromEnvironment("usePathUrl"); + if (usePathUrl) { + final q = query.substring(1); + final base = document.baseUri ?? "/"; + window.history.replaceState(null, "", "$base$q"); + } else { + window.history.replaceState(null, "", "#$query"); + } +} diff --git a/lib/viewer/single.dart b/lib/viewer/single.dart index 587cac6..09aac80 100644 --- a/lib/viewer/single.dart +++ b/lib/viewer/single.dart @@ -1,5 +1,6 @@ import 'package:dio/dio.dart'; import 'package:dio_image_provider/dio_image_provider.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; @@ -12,6 +13,7 @@ import 'package:photo_view/photo_view_gallery.dart'; import '../api/file.dart'; import '../api/gallery.dart'; import '../globals.dart'; +import '../platform/replace_current_route.dart'; final _log = Logger("SinglePageViewer"); @@ -84,6 +86,7 @@ class _SinglePageViewer extends State with ThemeModeWidget { } setState(() { _files = fileData; + _error = null; _isLoading = false; }); } @@ -99,9 +102,15 @@ class _SinglePageViewer extends State with ThemeModeWidget { } void _onPageChanged(BuildContext context) { - context.replace("/gallery/${widget.gid}/page/${_index + 1}", + GoRouter.optionURLReflectsImperativeAPIs = false; + final q = "/gallery/${widget.gid}/page/${_index + 1}"; + context.replace(q, extra: SinglePageViewerExtra(data: _data, files: _files)); + GoRouter.optionURLReflectsImperativeAPIs = true; _page_changed = false; + if (kIsWeb) { + replaceCurrentRoute(q); + } } @override @@ -152,7 +161,6 @@ class _SinglePageViewer extends State with ThemeModeWidget { bindings: [ KeyAction(LogicalKeyboardKey.arrowLeft, "previous page", () { if (_index > 0) { - _index -= 1; _pageController.previousPage( duration: const Duration(milliseconds: 200), curve: Curves.easeInOut); @@ -160,7 +168,6 @@ class _SinglePageViewer extends State with ThemeModeWidget { }), KeyAction(LogicalKeyboardKey.arrowRight, "next page", () { if (_index < _data!.pages.length - 1) { - _index += 1; _pageController.nextPage( duration: const Duration(milliseconds: 200), curve: Curves.easeInOut);