功能调优

This commit is contained in:
DelLevin-Home
2026-05-27 14:09:29 +08:00
parent 00d8a38375
commit 5022a13b74
5 changed files with 112 additions and 157 deletions

View File

@@ -44,7 +44,6 @@ Future<void> _initUsageStats() async {
class MyApp extends StatefulWidget { class MyApp extends StatefulWidget {
final AppProvider appProvider; final AppProvider appProvider;
const MyApp({super.key, required this.appProvider}); const MyApp({super.key, required this.appProvider});
@override @override
@@ -52,52 +51,50 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> with WidgetsBindingObserver { class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
ThemeMode? _lastAppliedTheme;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
widget.appProvider.loadThemeMode(); widget.appProvider.loadThemeMode();
widget.appProvider.addListener(_onThemeChanged); widget.appProvider.addListener(_onThemeChanged);
_updateSystemUI(widget.appProvider.themeMode); // 延迟到首帧后确保生效
} WidgetsBinding.instance.addPostFrameCallback((_) => _applySystemUI());
@override
void dispose() {
widget.appProvider.removeListener(_onThemeChanged);
WidgetsBinding.instance.removeObserver(this);
super.dispose();
} }
void _onThemeChanged() { void _onThemeChanged() {
_updateSystemUI(widget.appProvider.themeMode); final current = widget.appProvider.themeMode;
if (_lastAppliedTheme != current) {
_applySystemUI();
}
}
void _applySystemUI() {
final mode = widget.appProvider.themeMode;
final Brightness brightness;
switch (mode) {
case ThemeMode.light: brightness = Brightness.light;
case ThemeMode.dark: brightness = Brightness.dark;
case ThemeMode.system: brightness = PlatformDispatcher.instance.platformBrightness;
}
_lastAppliedTheme = mode;
final isDark = brightness == Brightness.dark;
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: isDark ? Brightness.light : Brightness.dark,
systemNavigationBarColor: isDark ? Colors.black : Colors.white,
systemNavigationBarIconBrightness: isDark ? Brightness.light : Brightness.dark,
));
} }
@override @override
void didChangeAppLifecycleState(AppLifecycleState state) { void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) { if (state == AppLifecycleState.resumed) {
_updateSystemUI(widget.appProvider.themeMode); _applySystemUI();
} }
} }
void _updateSystemUI(ThemeMode mode) {
final Brightness brightness;
switch (mode) {
case ThemeMode.light:
brightness = Brightness.light;
case ThemeMode.dark:
brightness = Brightness.dark;
case ThemeMode.system:
brightness = PlatformDispatcher.instance.platformBrightness;
}
final isDark = brightness == Brightness.dark;
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: isDark ? Brightness.light : Brightness.dark,
systemNavigationBarColor: isDark ? const Color(0xFF1A1A1A) : Colors.white,
systemNavigationBarIconBrightness: isDark ? Brightness.light : Brightness.dark,
));
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final iconName = UserPrefs().appIconName; final iconName = UserPrefs().appIconName;
@@ -138,7 +135,6 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
class _AppIconWrapper extends StatefulWidget { class _AppIconWrapper extends StatefulWidget {
final Widget child; final Widget child;
final String iconName; final String iconName;
const _AppIconWrapper({required this.child, required this.iconName}); const _AppIconWrapper({required this.child, required this.iconName});
@override @override
@@ -163,7 +159,6 @@ class _AppIconWrapperState extends State<_AppIconWrapper> {
@Preview(name: "MookNote App Preview") @Preview(name: "MookNote App Preview")
Widget previewMyApp() { Widget previewMyApp() {
final appProvider = AppProvider(); final appProvider = AppProvider();
return MultiProvider( return MultiProvider(
providers: [ providers: [
ChangeNotifierProvider.value(value: appProvider), ChangeNotifierProvider.value(value: appProvider),

View File

@@ -10,7 +10,7 @@ import 'note/note_tab_page.dart';
import 'search_page.dart'; import 'search_page.dart';
import 'sync/webdav_sync_page.dart'; import 'sync/webdav_sync_page.dart';
/// 主内容页 - 观影/阅读/笔记标签页 /// 主内容页 - 观影/阅读/笔记标签页PageView 滑动切换)
class MainContentPage extends StatefulWidget { class MainContentPage extends StatefulWidget {
const MainContentPage({super.key}); const MainContentPage({super.key});
@@ -25,10 +25,20 @@ class _MainContentPageState extends State<MainContentPage> {
bool _showBookTab = true; bool _showBookTab = true;
bool _showNoteTab = true; bool _showNoteTab = true;
late PageController _pageController;
bool _isTabTap = false; // 防止点击 Tab 和滑动互斥
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_loadTabSettings(); _loadTabSettings();
_pageController = PageController(initialPage: 0);
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
} }
void _loadTabSettings() { void _loadTabSettings() {
@@ -118,32 +128,11 @@ class _MainContentPageState extends State<MainContentPage> {
)), )),
Text('云备份', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: bc.onSurface)), Text('云备份', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: bc.onSurface)),
const SizedBox(height: 20), const SizedBox(height: 20),
_cloudCard( _cloudCard(icon: Icons.cloud_upload_outlined, title: '上传数据', desc: hasConfig ? '将本地数据同步到云端' : '请先配置 WebDAV 服务器', enabled: hasConfig, onTap: hasConfig ? () { Navigator.pop(ctx); _performSync(context, SyncDirection.upload); } : null, colors: bc),
icon: Icons.cloud_upload_outlined,
title: '上传数据',
desc: hasConfig ? '将本地数据同步到云端' : '请先配置 WebDAV 服务器',
onTap: hasConfig ? () { Navigator.pop(ctx); _performSync(context, SyncDirection.upload); } : null,
enabled: hasConfig,
colors: bc,
),
const SizedBox(height: 12), const SizedBox(height: 12),
_cloudCard( _cloudCard(icon: Icons.cloud_download_outlined, title: '下载数据', desc: hasConfig ? '从云端恢复数据到本地' : '请先配置 WebDAV 服务器', enabled: hasConfig, onTap: hasConfig ? () { Navigator.pop(ctx); _performSync(context, SyncDirection.download); } : null, colors: bc),
icon: Icons.cloud_download_outlined,
title: '下载数据',
desc: hasConfig ? '从云端恢复数据到本地' : '请先配置 WebDAV 服务器',
onTap: hasConfig ? () { Navigator.pop(ctx); _performSync(context, SyncDirection.download); } : null,
enabled: hasConfig,
colors: bc,
),
const SizedBox(height: 12), const SizedBox(height: 12),
_cloudCard( _cloudCard(icon: Icons.settings_outlined, title: 'WebDAV 设置', desc: '配置服务器地址与认证信息', enabled: true, onTap: () { Navigator.pop(ctx); Navigator.push(context, MaterialPageRoute(builder: (_) => const WebDAVSyncPage())); }, colors: bc),
icon: Icons.settings_outlined,
title: 'WebDAV 设置',
desc: '配置服务器地址与认证信息',
onTap: () { Navigator.pop(ctx); Navigator.push(context, MaterialPageRoute(builder: (_) => const WebDAVSyncPage())); },
enabled: true,
colors: bc,
),
]), ]),
), ),
); );
@@ -162,14 +151,7 @@ class _MainContentPageState extends State<MainContentPage> {
border: Border.all(color: enabled ? colors.primary.withValues(alpha: 0.1) : colors.outlineVariant, width: 0.5), border: Border.all(color: enabled ? colors.primary.withValues(alpha: 0.1) : colors.outlineVariant, width: 0.5),
), ),
child: Row(children: [ child: Row(children: [
Container( Container(width: 48, height: 48, decoration: BoxDecoration(color: enabled ? colors.primary.withValues(alpha: 0.08) : colors.surfaceContainerHighest, borderRadius: BorderRadius.circular(12)), child: Icon(icon, size: 24, color: enabled ? colors.primary : colors.onSurface.withValues(alpha: 0.18))),
width: 48, height: 48,
decoration: BoxDecoration(
color: enabled ? colors.primary.withValues(alpha: 0.08) : colors.surfaceContainerHighest,
borderRadius: BorderRadius.circular(12),
),
child: Icon(icon, size: 24, color: enabled ? colors.primary : colors.onSurface.withValues(alpha: 0.18)),
),
const SizedBox(width: 16), const SizedBox(width: 16),
Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(title, style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: enabled ? colors.onSurface : colors.onSurface.withValues(alpha: 0.25))), Text(title, style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600, color: enabled ? colors.onSurface : colors.onSurface.withValues(alpha: 0.25))),
@@ -201,12 +183,7 @@ class _MainContentPageState extends State<MainContentPage> {
await provider.loadNotes(); await provider.loadNotes();
} }
if (context.mounted) { if (context.mounted) {
_showResultDialog(context, _showResultDialog(context, title: result.success ? '同步成功' : '同步失败', message: result.message.isNotEmpty ? result.message : (result.success ? '同步成功' : '同步失败'), isSuccess: result.success, details: {'uploaded': result.uploadedFiles + result.uploadedImages, 'downloaded': result.downloadedFiles + result.downloadedImages});
title: result.success ? '同步成功' : '同步失败',
message: result.message.isNotEmpty ? result.message : (result.success ? '同步成功' : '同步失败'),
isSuccess: result.success,
details: {'uploaded': result.uploadedFiles + result.uploadedImages, 'downloaded': result.downloadedFiles + result.downloadedImages},
);
} }
} }
@@ -218,31 +195,18 @@ class _MainContentPageState extends State<MainContentPage> {
backgroundColor: colors.surface, elevation: 0, backgroundColor: colors.surface, elevation: 0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
title: Row(children: [ title: Row(children: [
Container( Container(width: 40, height: 40, decoration: BoxDecoration(color: isSuccess ? const Color(0xFFE8F5E9) : const Color(0xFFFFEBEE), borderRadius: BorderRadius.circular(10)), child: Icon(isSuccess ? Icons.check_circle : Icons.error, color: isSuccess ? const Color(0xFF4CAF50) : const Color(0xFFE57373), size: 24)),
width: 40, height: 40,
decoration: BoxDecoration(color: isSuccess ? const Color(0xFFE8F5E9) : const Color(0xFFFFEBEE), borderRadius: BorderRadius.circular(10)),
child: Icon(isSuccess ? Icons.check_circle : Icons.error, color: isSuccess ? const Color(0xFF4CAF50) : const Color(0xFFE57373), size: 24),
),
const SizedBox(width: 12), const SizedBox(width: 12),
Text(title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: colors.onSurface)), Text(title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: colors.onSurface)),
]), ]),
content: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ content: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(message, style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6), height: 1.5)), Text(message, style: TextStyle(fontSize: 14, color: colors.onSurface.withValues(alpha: 0.6), height: 1.5)),
if (details != null) ...[ if (details != null) ...[const SizedBox(height: 16), Container(padding: const EdgeInsets.all(12), decoration: BoxDecoration(color: colors.surfaceContainerHigh, borderRadius: BorderRadius.circular(8)), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
const SizedBox(height: 16), if (details['uploaded'] != null) _detailRow('上传文件', '${details['uploaded']}', colors),
Container(padding: const EdgeInsets.all(12), decoration: BoxDecoration(color: colors.surfaceContainerHigh, borderRadius: BorderRadius.circular(8)), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ if (details['downloaded'] != null) _detailRow('下载文件', '${details['downloaded']}', colors),
if (details['uploaded'] != null) _detailRow('上传文件', '${details['uploaded']}', colors), ]))],
if (details['downloaded'] != null) _detailRow('下载文件', '${details['downloaded']}', colors),
])),
],
]), ]),
actions: [ actions: [ElevatedButton(onPressed: () => Navigator.pop(context), style: ElevatedButton.styleFrom(backgroundColor: colors.primary, foregroundColor: colors.onPrimary, elevation: 0, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)), padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12)), child: const Text('确定'))],
ElevatedButton(
onPressed: () => Navigator.pop(context),
style: ElevatedButton.styleFrom(backgroundColor: colors.primary, foregroundColor: colors.onPrimary, elevation: 0, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)), padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12)),
child: const Text('确定'),
),
],
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
), ),
); );
@@ -265,6 +229,10 @@ class _MainContentPageState extends State<MainContentPage> {
} }
} }
// ═══════════════════════════════════════════════════════════════════
// Tab 栏 + 指示条(跟随 PageView 滑动)
// ═══════════════════════════════════════════════════════════════════
Widget _buildTabBar(BuildContext context) { Widget _buildTabBar(BuildContext context) {
return Consumer<AppProvider>( return Consumer<AppProvider>(
builder: (context, provider, child) { builder: (context, provider, child) {
@@ -286,47 +254,47 @@ class _MainContentPageState extends State<MainContentPage> {
return Expanded( return Expanded(
child: GestureDetector( child: GestureDetector(
behavior: HitTestBehavior.opaque, behavior: HitTestBehavior.opaque,
onTap: () => provider.setMainTabIndex(tab.originalIndex), onTap: () {
_isTabTap = true;
_pageController.animateToPage(idx, duration: const Duration(milliseconds: 350), curve: Curves.easeInOut);
provider.setMainTabIndex(tab.originalIndex);
Future.delayed(const Duration(milliseconds: 400), () => _isTabTap = false);
},
child: Padding( child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10), padding: const EdgeInsets.symmetric(vertical: 10),
child: Row( child: Row(mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: [
mainAxisAlignment: MainAxisAlignment.center, Icon(_tabIcon(tab.label), size: 18, color: selected ? colors.primary : colors.onSurface.withValues(alpha: 0.3)),
mainAxisSize: MainAxisSize.min, const SizedBox(width: 5),
children: [ Text(tab.label, textAlign: TextAlign.center, style: TextStyle(fontSize: 15, fontWeight: selected ? FontWeight.w700 : FontWeight.w500, color: selected ? colors.primary : colors.onSurface.withValues(alpha: 0.3))),
Icon( ]),
_tabIcon(tab.label),
size: 18,
color: selected ? colors.primary : colors.onSurface.withValues(alpha: 0.3),
),
const SizedBox(width: 5),
Text(tab.label, textAlign: TextAlign.center, style: TextStyle(
fontSize: 15,
fontWeight: selected ? FontWeight.w700 : FontWeight.w500,
color: selected ? colors.primary : colors.onSurface.withValues(alpha: 0.3),
)),
],
),
), ),
), ),
); );
}).toList(), }).toList(),
), ),
), ),
Padding( // 指示条 — 跟随 PageView 滑动
padding: const EdgeInsets.symmetric(horizontal: 24), AnimatedBuilder(
child: LayoutBuilder( animation: _pageController,
builder: (context, constraints) { builder: (context, _) {
final tabWidth = tabs.isNotEmpty ? constraints.maxWidth / tabs.length : 0.0; final page = _pageController.hasClients ? _pageController.page ?? 0.0 : safeIndex.toDouble();
return SizedBox(height: 2.5, child: Stack(children: [ return Padding(
AnimatedPositioned( padding: const EdgeInsets.symmetric(horizontal: 24),
duration: const Duration(milliseconds: 300), curve: Curves.easeInOut, child: LayoutBuilder(
left: safeIndex * tabWidth, top: 0, builder: (context, constraints) {
width: tabWidth, final tabWidth = tabs.isNotEmpty ? constraints.maxWidth / tabs.length : 0.0;
child: Container(height: 2.5, decoration: BoxDecoration(color: colors.primary, borderRadius: BorderRadius.circular(2))), return SizedBox(height: 2.5, child: Stack(children: [
), Positioned(
])); left: page * tabWidth,
}, top: 0,
), width: tabWidth,
child: Container(height: 2.5, decoration: BoxDecoration(color: colors.primary, borderRadius: BorderRadius.circular(2))),
),
]));
},
),
);
},
), ),
]), ]),
); );
@@ -334,23 +302,34 @@ class _MainContentPageState extends State<MainContentPage> {
); );
} }
// ═══════════════════════════════════════════════════════════════════
// PageView 内容区
// ═══════════════════════════════════════════════════════════════════
Widget _buildTabContent() { Widget _buildTabContent() {
return Consumer<AppProvider>( return Consumer<AppProvider>(
builder: (context, provider, child) { builder: (context, provider, child) {
final tabs = _enabledTabs; final tabs = _enabledTabs;
_TabItem? currentTab; final safeIndex = _mapToEnabledTabIndex(provider.mainTabIndex).clamp(0, tabs.length - 1);
for (final tab in tabs) { if (tab.originalIndex == provider.mainTabIndex) { currentTab = tab; break; } }
if (currentTab == null && tabs.isNotEmpty) { // 同步 provider → PageView点击 Tab 触发)
currentTab = tabs.first; if (_isTabTap && _pageController.hasClients) {
WidgetsBinding.instance.addPostFrameCallback((_) => provider.setMainTabIndex(currentTab!.originalIndex)); _pageController.animateToPage(safeIndex, duration: const Duration(milliseconds: 350), curve: Curves.easeInOut);
}
if (currentTab == null) return const Center(child: Text('请至少启用一个标签页'));
switch (currentTab.originalIndex) {
case 0: return const MovieTabPage();
case 1: return const BookTabPage();
case 2: return const NoteTabPage();
default: return const MovieTabPage();
} }
return PageView(
controller: _pageController,
onPageChanged: (index) {
if (!_isTabTap && index < tabs.length) {
provider.setMainTabIndex(tabs[index].originalIndex);
}
},
children: [
if (_showMovieTab) const MovieTabPage(),
if (_showBookTab) const BookTabPage(),
if (_showNoteTab) const NoteTabPage(),
],
);
}, },
); );
} }
@@ -363,23 +342,6 @@ class _MainContentPageState extends State<MainContentPage> {
default: return Icons.circle; default: return Icons.circle;
} }
} }
void _showAddDialog(BuildContext context, AppProvider provider) {
final colors = Theme.of(context).colorScheme;
showModalBottomSheet(
context: context, backgroundColor: colors.surface,
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
builder: (_) => SafeArea(
child: Wrap(children: [
ListTile(leading: Icon(Icons.movie, color: colors.onSurface), title: const Text('添加观影'), onTap: () { Navigator.pop(context); Navigator.pushNamed(context, '/movie-form', arguments: {'initialStatus': ['watched', 'watching', 'want_to_watch'][provider.movieStatusIndex]}); }),
Divider(height: 0.5, indent: 56, color: colors.outlineVariant),
ListTile(leading: Icon(Icons.menu_book, color: colors.onSurface), title: const Text('添加阅读'), onTap: () { Navigator.pop(context); Navigator.pushNamed(context, '/book-form', arguments: {'initialStatus': ['read', 'reading', 'want_to_read'][provider.bookStatusIndex]}); }),
Divider(height: 0.5, indent: 56, color: colors.outlineVariant),
ListTile(leading: Icon(Icons.note, color: colors.onSurface), title: const Text('添加笔记'), onTap: () { Navigator.pop(context); Navigator.pushNamed(context, '/note-form'); }),
]),
),
);
}
} }
class _TabItem { class _TabItem {

View File

@@ -64,7 +64,6 @@ class AppTheme {
elevation: 0, elevation: 0,
centerTitle: false, centerTitle: false,
titleSpacing: 24, titleSpacing: 24,
systemOverlayStyle: SystemUiOverlayStyle.dark,
titleTextStyle: TextStyle( titleTextStyle: TextStyle(
fontFamily: _fontFamily, fontFamily: _fontFamily,
fontSize: 18, fontSize: 18,
@@ -278,7 +277,6 @@ class AppTheme {
elevation: 0, elevation: 0,
centerTitle: false, centerTitle: false,
titleSpacing: 24, titleSpacing: 24,
systemOverlayStyle: SystemUiOverlayStyle.light,
titleTextStyle: TextStyle( titleTextStyle: TextStyle(
fontFamily: _fontFamily, fontFamily: _fontFamily,
fontSize: 18, fontSize: 18,

View File

@@ -17,8 +17,8 @@ class UsageStatsService with WidgetsBindingObserver {
final UserPrefs _prefs = UserPrefs(); final UserPrefs _prefs = UserPrefs();
/// 统计服务器地址,发布前替换为实际地址,置空则禁用 /// 统计服务器地址,发布前替换为实际地址,置空则禁用
// static String serverUrl = 'http://api.mooknote.iletter.top/'; static String serverUrl = 'http://api.mooknote.iletter.top/';
static String serverUrl = 'http://192.168.31.48:27050/'; // static String serverUrl = 'http://192.168.31.48:27050/';
Timer? _heartbeatTimer; Timer? _heartbeatTimer;
bool _started = false; bool _started = false;

View File

@@ -1,7 +1,7 @@
name: mooknote name: mooknote
description: "app for tracking movies, books, and notes" description: "app for tracking movies, books, and notes"
publish_to: 'none' publish_to: 'none'
version: 0.1.8 version: 0.1.9
environment: environment:
sdk: ^3.5.0 sdk: ^3.5.0