generated from dellevin/template
优化windows笔记功能
This commit is contained in:
19
lib/widgets/app_shell.dart
Normal file
19
lib/widgets/app_shell.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'custom_title_bar.dart';
|
||||
|
||||
class AppShell extends StatelessWidget {
|
||||
final Widget child;
|
||||
const AppShell({super.key, required this.child});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!Platform.isWindows) return child;
|
||||
return Column(
|
||||
children: [
|
||||
const CustomTitleBar(),
|
||||
Expanded(child: child),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
148
lib/widgets/custom_title_bar.dart
Normal file
148
lib/widgets/custom_title_bar.dart
Normal file
@@ -0,0 +1,148 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
class CustomTitleBar extends StatefulWidget {
|
||||
const CustomTitleBar({super.key});
|
||||
|
||||
static const double height = 32.0;
|
||||
|
||||
@override
|
||||
State<CustomTitleBar> createState() => CustomTitleBarState();
|
||||
}
|
||||
|
||||
class CustomTitleBarState extends State<CustomTitleBar> with WindowListener {
|
||||
bool _isMaximized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
windowManager.addListener(this);
|
||||
_checkMaximized();
|
||||
}
|
||||
|
||||
Future<void> _checkMaximized() async {
|
||||
_isMaximized = await windowManager.isMaximized();
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
void onWindowMaximize() => setState(() => _isMaximized = true);
|
||||
|
||||
@override
|
||||
void onWindowUnmaximize() => setState(() => _isMaximized = false);
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
windowManager.removeListener(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return GestureDetector(
|
||||
onDoubleTap: () async {
|
||||
if (await windowManager.isMaximized()) {
|
||||
windowManager.unmaximize();
|
||||
} else {
|
||||
windowManager.maximize();
|
||||
}
|
||||
},
|
||||
onPanStart: (_) => windowManager.startDragging(),
|
||||
child: Container(
|
||||
height: CustomTitleBar.height,
|
||||
color: colors.surface,
|
||||
child: Row(
|
||||
children: [
|
||||
const SizedBox(width: 12),
|
||||
Image.asset('assets/icon/app_icon.webp', width: 16, height: 16),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'MookNote',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: colors.onSurface.withValues(alpha: 0.7),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
_WindowButton(
|
||||
icon: Icons.remove,
|
||||
size: 18,
|
||||
onTap: () => windowManager.minimize(),
|
||||
),
|
||||
_WindowButton(
|
||||
icon: _isMaximized ? Icons.filter_none : Icons.crop_square,
|
||||
size: 14,
|
||||
onTap: () async {
|
||||
if (await windowManager.isMaximized()) {
|
||||
windowManager.unmaximize();
|
||||
} else {
|
||||
windowManager.maximize();
|
||||
}
|
||||
},
|
||||
),
|
||||
_WindowButton(
|
||||
icon: Icons.close,
|
||||
size: 18,
|
||||
onTap: () => windowManager.close(),
|
||||
isClose: true,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _WindowButton extends StatefulWidget {
|
||||
final IconData icon;
|
||||
final double size;
|
||||
final VoidCallback onTap;
|
||||
final bool isClose;
|
||||
|
||||
const _WindowButton({
|
||||
required this.icon,
|
||||
this.size = 16,
|
||||
required this.onTap,
|
||||
this.isClose = false,
|
||||
});
|
||||
|
||||
@override
|
||||
State<_WindowButton> createState() => _WindowButtonState();
|
||||
}
|
||||
|
||||
class _WindowButtonState extends State<_WindowButton> {
|
||||
bool _hovering = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
Color bg;
|
||||
Color fg;
|
||||
if (widget.isClose && _hovering) {
|
||||
bg = const Color(0xFFE81123);
|
||||
fg = Colors.white;
|
||||
} else if (_hovering) {
|
||||
bg = colors.onSurface.withValues(alpha: 0.08);
|
||||
fg = colors.onSurface;
|
||||
} else {
|
||||
bg = Colors.transparent;
|
||||
fg = colors.onSurface.withValues(alpha: 0.7);
|
||||
}
|
||||
|
||||
return GestureDetector(
|
||||
onTap: widget.onTap,
|
||||
child: MouseRegion(
|
||||
onEnter: (_) => setState(() => _hovering = true),
|
||||
onExit: (_) => setState(() => _hovering = false),
|
||||
child: Container(
|
||||
width: 46,
|
||||
height: CustomTitleBar.height,
|
||||
color: bg,
|
||||
child: Icon(widget.icon, size: widget.size, color: fg),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ class VditorEditor extends StatefulWidget {
|
||||
final String? initialContent;
|
||||
final String noteId;
|
||||
final bool isDark;
|
||||
final Color surfaceColor;
|
||||
final ValueChanged<String>? onContentChanged;
|
||||
final String placeholder;
|
||||
|
||||
@@ -20,6 +21,7 @@ class VditorEditor extends StatefulWidget {
|
||||
this.initialContent,
|
||||
required this.noteId,
|
||||
this.isDark = false,
|
||||
this.surfaceColor = Colors.white,
|
||||
this.onContentChanged,
|
||||
this.placeholder = '使用 Markdown 格式书写...',
|
||||
});
|
||||
@@ -116,6 +118,14 @@ class VditorEditorState extends State<VditorEditor> {
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setBgColor(String hexColor) async {
|
||||
if (_controller == null || !_isReady) return;
|
||||
try {
|
||||
final escaped = jsonEncode(hexColor);
|
||||
await _controller!.evaluateJavascript(source: 'setBgColor($escaped)');
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> insertValue(String text) async {
|
||||
if (_controller == null || !_isReady) return;
|
||||
try {
|
||||
@@ -133,6 +143,20 @@ class VditorEditorState extends State<VditorEditor> {
|
||||
if (widget.initialContent != null && widget.initialContent!.isNotEmpty) {
|
||||
setValue(widget.initialContent!);
|
||||
}
|
||||
// 设置背景色
|
||||
setBgColor(_colorToHex(widget.surfaceColor));
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant VditorEditor oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.surfaceColor != oldWidget.surfaceColor) {
|
||||
setBgColor(_colorToHex(widget.surfaceColor));
|
||||
}
|
||||
}
|
||||
|
||||
static String _colorToHex(Color color) {
|
||||
return '#${(color.value & 0xFFFFFF).toRadixString(16).padLeft(6, '0')}';
|
||||
}
|
||||
|
||||
Future<void> _pickImage() async {
|
||||
|
||||
Reference in New Issue
Block a user