Fix gallery's title can not display correct on chrome/firefox

This commit is contained in:
2023-09-14 21:00:33 +08:00
parent 0ffc08c624
commit a58732f788
4 changed files with 55 additions and 20 deletions

View File

@@ -123,7 +123,8 @@ class _GalleriesPage extends State<GalleriesPage> with ThemeModeWidget {
return ListTile( return ListTile(
title: Text(item.preferredTitle), title: Text(item.preferredTitle),
onTap: () { onTap: () {
context.push("/gallery/${item.gid}"); context.push("/gallery/${item.gid}",
extra: GalleryPageExtra(title: item.preferredTitle));
}, },
); );
}), }),

View File

@@ -1,3 +1,5 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
@@ -9,12 +11,18 @@ import 'globals.dart';
final _log = Logger("GalleryPage"); final _log = Logger("GalleryPage");
class GalleryPageExtra {
const GalleryPageExtra({this.title});
final String? title;
}
class GalleryPage extends StatefulWidget { class GalleryPage extends StatefulWidget {
const GalleryPage(int gid, {Key? key}) const GalleryPage(int gid, {Key? key, this.title})
: _gid = gid, : _gid = gid,
super(key: key); super(key: key);
final int _gid; final int _gid;
final String? title;
static const String routeName = '/gallery/:gid'; static const String routeName = '/gallery/:gid';
@override @override
@@ -27,26 +35,33 @@ class _GalleryPage extends State<GalleryPage> with ThemeModeWidget {
GalleryData? _data; GalleryData? _data;
EhFiles? _files; EhFiles? _files;
Object? _error; Object? _error;
CancelToken? _cancel;
bool _isLoading = false; bool _isLoading = false;
Future<void> _fetchData() async { Future<void> _fetchData() async {
try { try {
_cancel = CancelToken();
_isLoading = true; _isLoading = true;
final data = (await api.getGallery(_gid)).unwrap(); final data = (await api.getGallery(_gid)).unwrap();
_data = data; _data = data;
final fileData = final fileData = (await api.getFiles(
(await api.getFiles(data.pages.map((e) => e.token).toList())) data.pages.map((e) => e.token).toList(),
.unwrap(); cancel: _cancel))
setState(() { .unwrap();
_files = fileData; if (!_cancel!.isCancelled) {
_isLoading = false; setState(() {
}); _files = fileData;
_isLoading = false;
});
}
} catch (e) { } catch (e) {
_log.severe("Failed to load gallery $_gid:", e); if (!_cancel!.isCancelled) {
setState(() { _log.severe("Failed to load gallery $_gid:", e);
_error = e; setState(() {
_isLoading = false; _error = e;
}); _isLoading = false;
});
}
} }
} }
@@ -69,8 +84,15 @@ class _GalleryPage extends State<GalleryPage> with ThemeModeWidget {
: _data != null : _data != null
? _data!.meta.preferredTitle ? _data!.meta.preferredTitle
: i18n.gallery; : i18n.gallery;
setCurrentTitle(title, Theme.of(context).primaryColor.value, if (!kIsWeb || (_data != null && kIsWeb)) {
includePrefix: false); setCurrentTitle(title, Theme.of(context).primaryColor.value,
includePrefix: false);
} else if (kIsWeb && widget.title != null) {
// 设置预加载标题
// Chrome 和 Firefox 必须尽快设置标题以确保在历史记录菜单显示正确的标题
setCurrentTitle(widget.title!, Theme.of(context).primaryColor.value,
includePrefix: false);
}
return Scaffold( return Scaffold(
appBar: _data == null appBar: _data == null
? AppBar( ? AppBar(
@@ -95,4 +117,10 @@ class _GalleryPage extends State<GalleryPage> with ThemeModeWidget {
child: Text("Error: $_error"), child: Text("Error: $_error"),
)); ));
} }
@override
void dispose() {
_cancel?.cancel();
super.dispose();
}
} }

View File

@@ -25,6 +25,7 @@ import 'platform/clipboard.dart';
import 'platform/path.dart'; import 'platform/path.dart';
import 'tags.dart'; import 'tags.dart';
import 'utils.dart'; import 'utils.dart';
export 'gallery.dart' show GalleryPageExtra;
final dio = Dio() final dio = Dio()
..options.validateStatus = (int? _) { ..options.validateStatus = (int? _) {
@@ -307,7 +308,8 @@ void setCurrentTitle(String title, int primaryColor,
}); });
} else { } else {
SystemChrome.setApplicationSwitcherDescription( SystemChrome.setApplicationSwitcherDescription(
ApplicationSwitcherDescription(label: title, primaryColor: primaryColor)) ApplicationSwitcherDescription(
label: title, primaryColor: primaryColor))
.then((_) { .then((_) {
_currentTitle = title; _currentTitle = title;
if (isPrefix) _prefix = title; if (isPrefix) _prefix = title;

View File

@@ -59,9 +59,13 @@ final _router = GoRouter(
}), }),
GoRoute( GoRoute(
path: GalleryPage.routeName, path: GalleryPage.routeName,
builder: (context, state) => GalleryPage( builder: (context, state) {
int.parse(state.pathParameters["gid"]!), final extra = state.extra as GalleryPageExtra?;
), return GalleryPage(
int.parse(state.pathParameters["gid"]!),
title: extra?.title,
);
},
redirect: (context, state) { redirect: (context, state) {
try { try {
int.parse(state.pathParameters["gid"]!); int.parse(state.pathParameters["gid"]!);