mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-06-06 05:49:03 +08:00
Add support to change title
This commit is contained in:
@@ -92,6 +92,7 @@ class _GalleriesPage extends State<GalleriesPage> with ThemeModeWidget {
|
||||
],
|
||||
leadingIcon: const Icon(Icons.sort),
|
||||
);
|
||||
setCurrentTitle(AppLocalizations.of(context)!.galleries);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: IconButton(
|
||||
|
||||
@@ -63,11 +63,13 @@ class _GalleryPage extends State<GalleryPage> with ThemeModeWidget {
|
||||
tryInitApi(context);
|
||||
final isLoading = _data == null && _error == null;
|
||||
if (isLoading && !_isLoading) _fetchData();
|
||||
final i18n = AppLocalizations.of(context)!;
|
||||
final title = isLoading
|
||||
? AppLocalizations.of(context)!.loading
|
||||
? i18n.loading
|
||||
: _data != null
|
||||
? ""
|
||||
: AppLocalizations.of(context)!.gallery;
|
||||
? _data!.meta.preferredTitle
|
||||
: i18n.gallery;
|
||||
setCurrentTitle(title, includePrefix: false);
|
||||
return Scaffold(
|
||||
appBar: _data == null
|
||||
? AppBar(
|
||||
|
||||
@@ -6,12 +6,15 @@ import 'package:event_listener/event_listener.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/services.dart'
|
||||
show ApplicationSwitcherDescription, SystemChrome;
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
import 'api/client.dart';
|
||||
import 'auth.dart';
|
||||
import 'config/base.dart';
|
||||
@@ -278,3 +281,35 @@ void checkAuth(BuildContext context) {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
String? _currentTitle;
|
||||
String? _prefix;
|
||||
final _titleLog = Logger("Title");
|
||||
|
||||
void setCurrentTitle(String title,
|
||||
{bool isPrefix = false, bool includePrefix = true, bool usePrefix = false}) {
|
||||
if (!isPrefix && includePrefix && _prefix != null) {
|
||||
title = "$_prefix - $title";
|
||||
}
|
||||
if (usePrefix && _prefix != null) {
|
||||
title = _prefix!;
|
||||
}
|
||||
if (_currentTitle != null && title == _currentTitle) return;
|
||||
if (isDesktop) {
|
||||
windowManager.setTitle(title).then((_) {
|
||||
_currentTitle = title;
|
||||
if (isPrefix) _prefix = title;
|
||||
}).catchError((err) {
|
||||
_titleLog.warning("Failed to set title:", err);
|
||||
});
|
||||
} else {
|
||||
SystemChrome.setApplicationSwitcherDescription(
|
||||
ApplicationSwitcherDescription(label: title))
|
||||
.then((_) {
|
||||
_currentTitle = title;
|
||||
if (isPrefix) _prefix = title;
|
||||
}).catchError((err) {
|
||||
_titleLog.warning("Failed to set title:", err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ class HomePage extends HookWidget {
|
||||
Widget build(BuildContext context) {
|
||||
tryInitApi(context);
|
||||
var mode = useState(MainApp.of(context).themeMode);
|
||||
setCurrentTitle("", usePrefix: true);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)!.titleBar),
|
||||
|
||||
@@ -169,9 +169,7 @@ class _MainApp extends State<MainApp> {
|
||||
routerConfig: _router,
|
||||
onGenerateTitle: (context) {
|
||||
final title = AppLocalizations.of(context)!.title;
|
||||
if (isDesktop) {
|
||||
windowManager.setTitle(title);
|
||||
}
|
||||
setCurrentTitle(title, isPrefix: true);
|
||||
return title;
|
||||
},
|
||||
locale: _lang.toLocale(),
|
||||
|
||||
@@ -135,6 +135,7 @@ class _SettingsPage extends State<SettingsPage> with ThemeModeWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
setCurrentTitle(AppLocalizations.of(context)!.settings);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: IconButton(
|
||||
|
||||
Reference in New Issue
Block a user