This commit is contained in:
2023-11-28 15:55:42 +08:00
parent b9259f5086
commit 91a447b016
4 changed files with 27 additions and 3 deletions

View File

@@ -0,0 +1 @@
export 'replace_current_route_none.dart' if (dart.library.html) 'replace_current_route_web.dart';

View File

@@ -0,0 +1,3 @@
void replaceCurrentRoute(String query) {
throw UnimplementedError();
}

View File

@@ -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");
}
}

View File

@@ -1,5 +1,6 @@
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:dio_image_provider/dio_image_provider.dart'; import 'package:dio_image_provider/dio_image_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart'; import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@@ -12,6 +13,7 @@ import 'package:photo_view/photo_view_gallery.dart';
import '../api/file.dart'; import '../api/file.dart';
import '../api/gallery.dart'; import '../api/gallery.dart';
import '../globals.dart'; import '../globals.dart';
import '../platform/replace_current_route.dart';
final _log = Logger("SinglePageViewer"); final _log = Logger("SinglePageViewer");
@@ -84,6 +86,7 @@ class _SinglePageViewer extends State<SinglePageViewer> with ThemeModeWidget {
} }
setState(() { setState(() {
_files = fileData; _files = fileData;
_error = null;
_isLoading = false; _isLoading = false;
}); });
} }
@@ -99,9 +102,15 @@ class _SinglePageViewer extends State<SinglePageViewer> with ThemeModeWidget {
} }
void _onPageChanged(BuildContext context) { 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)); extra: SinglePageViewerExtra(data: _data, files: _files));
GoRouter.optionURLReflectsImperativeAPIs = true;
_page_changed = false; _page_changed = false;
if (kIsWeb) {
replaceCurrentRoute(q);
}
} }
@override @override
@@ -152,7 +161,6 @@ class _SinglePageViewer extends State<SinglePageViewer> with ThemeModeWidget {
bindings: [ bindings: [
KeyAction(LogicalKeyboardKey.arrowLeft, "previous page", () { KeyAction(LogicalKeyboardKey.arrowLeft, "previous page", () {
if (_index > 0) { if (_index > 0) {
_index -= 1;
_pageController.previousPage( _pageController.previousPage(
duration: const Duration(milliseconds: 200), duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut); curve: Curves.easeInOut);
@@ -160,7 +168,6 @@ class _SinglePageViewer extends State<SinglePageViewer> with ThemeModeWidget {
}), }),
KeyAction(LogicalKeyboardKey.arrowRight, "next page", () { KeyAction(LogicalKeyboardKey.arrowRight, "next page", () {
if (_index < _data!.pages.length - 1) { if (_index < _data!.pages.length - 1) {
_index += 1;
_pageController.nextPage( _pageController.nextPage(
duration: const Duration(milliseconds: 200), duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut); curve: Curves.easeInOut);