From fda7771918775ce9d5b462b363e653a8095cbfd9 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 6 Oct 2026 22:13:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=89=E5=8D=93=E5=85=A8=E5=B1=8F?= =?UTF-8?q?=EF=BC=88=E7=8A=B6=E6=80=81=E6=A0=8F/=E5=AF=BC=E8=88=AA?= =?UTF-8?q?=E6=A0=8F=E5=8C=BA=E5=9F=9F=E8=9E=8D=E5=85=A5=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=EF=BC=89+=20=E4=BF=9D=E5=AD=98/=E5=AF=BC=E5=87=BA=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=A1=86=E6=8C=89=E7=B1=BB=E5=9E=8B=E5=8C=BA=E5=88=86?= =?UTF-8?q?=E5=90=8E=E7=BC=80=E5=B9=B6=E8=AE=B0=E5=BF=86=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 全屏:移除全局 SafeArea,系统栏透明;工具栏/底部状态栏底色延伸填满状态栏与导航栏区域,图标亮度随深浅主题切换(桌面端 padding 为 0 无影响) - 文件对话框:保存/导出按类型区分——工程 .seq.json、导出 .svg / .png(默认名=项目名,目录列表只显示对应后缀文件,输入自动补全/替换后缀) - 路径体验:默认目录改为桌面(移动端为应用目录),并跨会话记忆上次使用的目录;此修复同时作用于 Windows exe 与安卓版 --- lib/main.dart | 31 +++++---- lib/ui/app_shell.dart | 49 +++++++++++---- lib/ui/dialogs.dart | 142 +++++++++++++++++++++++++++++++++--------- 3 files changed, 167 insertions(+), 55 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 93f49ef..9a76c71 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -25,15 +25,9 @@ import 'ui/app_shell.dart'; /// 然后启动根组件 [SequenceDiagramApp]。 Future main() async { WidgetsFlutterBinding.ensureInitialized(); - // 移动端:沉浸式(edge-to-edge)显示,配合 SafeArea 自动避让系统栏。 + // 移动端:沉浸式(edge-to-edge)显示,系统栏区域由应用工具栏/状态栏底色填充。 if (Platform.isAndroid || Platform.isIOS) { await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); - SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle( - statusBarColor: Colors.transparent, - systemNavigationBarColor: Colors.transparent, - statusBarIconBrightness: Brightness.dark, - statusBarBrightness: Brightness.light, - )); } await Storage.instance.init(); runApp(const SequenceDiagramApp()); @@ -170,14 +164,25 @@ class _SequenceDiagramAppState extends State { title: '时序图设计器 · Sequence Diagram Studio', debugShowCheckedModeBanner: false, theme: buildTheme(tokens), - // 固定文本缩放并包裹 SafeArea:保证平板等分辨率下布局像素级精准, - // 不受系统字体缩放影响,并自动避让状态栏 / 导航栏。 + // 固定文本缩放;系统栏图标随主题亮度自动切换(透明系统栏, + // 状态栏 / 导航栏区域由 AppShell 的工具栏 / 状态栏底色填满)。 builder: (BuildContext context, Widget? child) { - return MediaQuery( - data: MediaQuery.of(context).copyWith( - textScaler: const TextScaler.linear(1.0), + final bool dark = Theme.of(context).brightness == Brightness.dark; + return AnnotatedRegion( + value: SystemUiOverlayStyle( + statusBarColor: Colors.transparent, + statusBarIconBrightness: dark ? Brightness.light : Brightness.dark, + statusBarBrightness: dark ? Brightness.dark : Brightness.light, + systemNavigationBarColor: Colors.transparent, + systemNavigationBarIconBrightness: dark ? Brightness.light : Brightness.dark, + systemNavigationBarDividerColor: Colors.transparent, + ), + child: MediaQuery( + data: MediaQuery.of(context).copyWith( + textScaler: const TextScaler.linear(1.0), + ), + child: child ?? const SizedBox.shrink(), ), - child: SafeArea(child: child ?? const SizedBox.shrink()), ); }, home: AppShell(controller: _controller), diff --git a/lib/ui/app_shell.dart b/lib/ui/app_shell.dart index b8ebcba..d89c914 100644 --- a/lib/ui/app_shell.dart +++ b/lib/ui/app_shell.dart @@ -171,7 +171,13 @@ class _AppShellState extends State { /// 另存为:选择目标路径后写入文件(同时生成 .bak 备份)。 Future _saveAs() async { - final String? path = await pickFile(context, save: true, initialPath: c.filePath); + final String? path = await pickFile( + context, + save: true, + initialPath: c.filePath, + defaultName: c.filePath == null ? '${c.project.name}.seq.json' : null, + suffix: '.seq.json', + ); if (path == null) return; try { await c.saveAs(path); @@ -187,17 +193,21 @@ class _AppShellState extends State { if (mounted) await showMessage(context, '未授权', '未检测到有效授权(license/*.lic),无法导出 SVG。'); return; } - final String? path = await pickFile(context, save: true, initialPath: c.filePath); + final String? path = await pickFile( + context, + save: true, + defaultName: '${c.project.name}.svg', + suffix: '.svg', + ); if (path == null) return; - final String svgPath = path.replaceAll(RegExp(r'\.json$'), '.svg'); try { final String svg = SvgWriter( project: c.project, tokens: Tokens.of(c.brightness), mode: c.project.viewState.mode, ).build(); - await File(svgPath).writeAsString(svg, flush: true); - if (mounted) c.setStatus('已导出 $svgPath'); + await File(path).writeAsString(svg, flush: true); + if (mounted) c.setStatus('已导出 $path'); } catch (e) { if (mounted) await showMessage(context, '导出失败', '$e'); } @@ -210,17 +220,21 @@ class _AppShellState extends State { if (mounted) await showMessage(context, '未授权', '未检测到有效授权(license/*.lic),无法导出 PNG。'); return; } - final String? path = await pickFile(context, save: true, initialPath: c.filePath); + final String? path = await pickFile( + context, + save: true, + defaultName: '${c.project.name}.png', + suffix: '.png', + ); if (path == null) return; - final String pngPath = path.replaceAll(RegExp(r'\.(json|svg)$'), '.png'); try { // 抓取画布舞台的渲染对象并转换为图像。 final RenderRepaintBoundary boundary = _stageKey.currentContext!.findRenderObject()! as RenderRepaintBoundary; final ui.Image image = await boundary.toImage(pixelRatio: 2); final ByteData? data = await image.toByteData(format: ui.ImageByteFormat.png); - await File(pngPath).writeAsBytes(data!.buffer.asUint8List(), flush: true); - if (mounted) c.setStatus('已导出 $pngPath'); + await File(path).writeAsBytes(data!.buffer.asUint8List(), flush: true); + if (mounted) c.setStatus('已导出 $path'); } catch (e) { if (mounted) await showMessage(context, '导出失败', '$e'); } @@ -256,8 +270,13 @@ class _AppShellState extends State { backgroundColor: t.bg, body: Column( children: [ - // 顶部:自适应工具栏。 - _toolbar(t), + // 顶部:系统状态栏区域与工具栏同底色,内容在系统栏下方, + // 移动端实现真正的全屏(无黑边),桌面端 padding 为 0 无影响。 + Container( + color: t.chrome, + padding: EdgeInsets.only(top: MediaQuery.paddingOf(context).top), + child: _toolbar(t), + ), Expanded( child: Row( // 拉伸交叉轴:保证左右侧栏始终从工具栏一直贯通到底部状态栏。 @@ -319,8 +338,12 @@ class _AppShellState extends State { ], ), ), - // 底部:状态栏。 - _statusBar(t), + // 底部:系统导航栏区域与状态栏同底色,内容在系统栏上方。 + Container( + color: t.chrome, + padding: EdgeInsets.only(bottom: MediaQuery.paddingOf(context).bottom), + child: _statusBar(t), + ), ], ), ), diff --git a/lib/ui/dialogs.dart b/lib/ui/dialogs.dart index 7f61df3..1cf3c6f 100644 --- a/lib/ui/dialogs.dart +++ b/lib/ui/dialogs.dart @@ -14,6 +14,7 @@ import 'package:flutter/material.dart' hide Action; import '../license/license_service.dart'; import '../model/models.dart'; import '../model/units.dart'; +import '../persistence/storage.dart'; import '../state/app_controller.dart'; import '../theme/tokens.dart'; @@ -66,15 +67,27 @@ Future showMessage(BuildContext context, String title, String message) { // ===================== 文件浏览 / 保存对话框 ===================== -/// 文件浏览对话框:用于打开或另存为 `.seq.json` 文件。 -/// [saveMode] 为 true 时进入保存流程,否则为打开流程。 +/// 文件浏览对话框:用于打开或另存为文件(工程 `.seq.json`、导出 `.svg` / `.png`)。 +/// +/// [saveMode] 为 true 时进入保存流程,否则为打开流程;[suffix] 决定文件过滤 +/// 与保存时自动补全的后缀;[defaultName] 为保存模式的默认文件名。 class FileBrowserDialog extends StatefulWidget { - const FileBrowserDialog({super.key, required this.saveMode, this.initialPath}); + const FileBrowserDialog({ + super.key, + required this.saveMode, + this.initialPath, + this.defaultName, + this.suffix = '.seq.json', + }); /// 是否为保存模式(true = 另存为,false = 打开)。 final bool saveMode; /// 初始目录或初始文件路径。 final String? initialPath; + /// 保存模式的默认文件名(如 `装配线节拍.svg`)。 + final String? defaultName; + /// 文件后缀:过滤目录列表并自动补全用户输入(如 `.svg` / `.png` / `.seq.json`)。 + final String suffix; /// 创建文件浏览对话框对应的状态对象。 @override @@ -87,43 +100,97 @@ class _FileBrowserDialogState extends State { late Directory _dir; /// 文件名输入控制器。 late TextEditingController _name; - /// 当前目录下的条目(子目录 + .json 文件)。 + /// 当前目录下的条目(子目录 + 匹配后缀的文件)。 List _entries = []; /// 读取目录失败时的错误信息。 String? _error; - /// 解析初始路径、初始化文件名,并首次刷新目录列表。 + /// 解析初始路径 / 上次目录 / 桌面,初始化文件名并刷新目录列表。 @override void initState() { super.initState(); - String start = widget.initialPath ?? ''; - if (start.isEmpty) { - // 桌面版从用户目录开始;Android/iOS 从应用可写目录(系统临时目录)开始。 - start = Platform.environment['USERPROFILE'] ?? - (Platform.isAndroid || Platform.isIOS - ? Directory.systemTemp.path - : Directory.current.path); + final String base = widget.defaultName ?? + (widget.initialPath != null && widget.initialPath!.isNotEmpty + ? widget.initialPath!.split(RegExp(r'[\\/]')).last + : '节拍方案${widget.suffix}'); + _name = TextEditingController(text: base); + _bootstrap(); + } + + /// 异步确定起始目录:优先初始路径 → 上次使用的目录 → 桌面 → 用户目录。 + Future _bootstrap() async { + String start = ''; + if (widget.initialPath != null && widget.initialPath!.isNotEmpty) { + final File f = File(widget.initialPath!); + start = await f.exists() ? f.parent.path : widget.initialPath!; } - if (File(start).existsSync()) { - start = File(start).parent.path; + if (start.isEmpty || !Directory(start).existsSync()) { + start = await _readLastDir(); + } + if (start.isEmpty || !Directory(start).existsSync()) { + final Directory? desktop = _desktop(); + if (desktop != null) { + start = desktop.path; + } else { + start = Platform.environment['USERPROFILE'] ?? + (Platform.isAndroid || Platform.isIOS + ? Directory.systemTemp.path + : Directory.current.path); + } } _dir = Directory(start); if (!_dir.existsSync()) _dir = Directory.current; - final String base = widget.initialPath != null && widget.initialPath!.isNotEmpty - ? widget.initialPath!.split(RegExp(r'[\\/]')).last - : '节拍方案.seq.json'; - _name = TextEditingController(text: base); - _refresh(); + if (mounted) _refresh(); } - /// 重新读取当前目录:仅保留子目录与 .json 文件, + /// 上次使用目录的持久化文件路径(位于应用数据目录,跨会话记忆)。 + File get _lastDirFile => File( + '${Storage.instance.appDir.path}${Platform.pathSeparator}last_dir.txt'); + + /// 读取上次使用的目录;不存在或不可用时返回空串。 + Future _readLastDir() async { + try { + final File f = _lastDirFile; + if (await f.exists()) { + final String s = (await f.readAsString()).trim(); + if (s.isNotEmpty && Directory(s).existsSync()) return s; + } + } catch (_) {} + return ''; + } + + /// 记住本次使用的目录,便于下次直接定位。 + Future _writeLastDir(String path) async { + try { + final File f = _lastDirFile; + await f.parent.create(recursive: true); + await f.writeAsString(path, flush: true); + } catch (_) {} + } + + /// 规范化文件名:替换其它已知后缀并补全目标后缀。 + String _ensureSuffix(String raw) { + String n = raw.trim(); + if (n.isEmpty) return n; + const List known = ['.seq.json', '.json', '.svg', '.png']; + for (final String k in known) { + if (n.toLowerCase().endsWith(k)) { + if (k == widget.suffix) return n; + n = n.substring(0, n.length - k.length); + break; + } + } + return '$n${widget.suffix}'; + } + + /// 重新读取当前目录:仅保留子目录与匹配后缀的文件, /// 目录排在文件之前并按名称不区分大小写排序。 void _refresh() { try { final List all = _dir.listSync(); _entries = all.where((FileSystemEntity e) { if (e is Directory) return true; - return e.path.toLowerCase().endsWith('.json'); + return e.path.toLowerCase().endsWith(widget.suffix.toLowerCase()); }).toList() ..sort((FileSystemEntity a, FileSystemEntity b) { final bool ad = a is Directory; @@ -188,7 +255,7 @@ class _FileBrowserDialogState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text(widget.saveMode ? '另存为 .seq.json' : '打开 .seq.json', + Text(widget.saveMode ? '保存 ${widget.suffix} 文件' : '打开 ${widget.suffix} 文件', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: t.text)), const SizedBox(height: 10), Row( @@ -283,7 +350,7 @@ class _FileBrowserDialogState extends State { const Divider(), Row( children: [ - Text('文件名', style: TextStyle(color: t.sub, fontSize: 11)), + Text('文件名(${widget.suffix})', style: TextStyle(color: t.sub, fontSize: 11)), const SizedBox(width: 10), Expanded( child: TextField( @@ -293,6 +360,8 @@ class _FileBrowserDialogState extends State { isDense: true, filled: true, fillColor: t.panel2, + hintText: '名称${widget.suffix}', + hintStyle: TextStyle(color: t.dim, fontSize: 11), border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)), ), ), @@ -307,12 +376,13 @@ class _FileBrowserDialogState extends State { const SizedBox(width: 8), FilledButton( onPressed: () { - // 规范化文件名:空名忽略,缺少 .json 后缀时补全为 .seq.json。 - String name = _name.text.trim(); + // 规范化文件名:空名忽略;替换其它已知后缀并补全目标后缀。 + final String name = _ensureSuffix(_name.text); if (name.isEmpty) return; - if (!name.toLowerCase().endsWith('.json')) name = '$name.seq.json'; final String sep = Platform.pathSeparator; final String path = '${_dir.path}$sep$name'; + // 记住本次使用的目录,下次直接定位。 + _writeLastDir(_dir.path); Navigator.pop(context, path); }, child: Text(widget.saveMode ? '保存' : '打开'), @@ -327,11 +397,25 @@ class _FileBrowserDialogState extends State { } } -/// 打开文件选择对话框并返回所选路径;[save] 决定保存或打开模式。 -Future pickFile(BuildContext context, {required bool save, String? initialPath}) { +/// 打开文件选择对话框并返回所选路径。 +/// +/// [save] 决定保存或打开模式;[suffix] 决定文件过滤与自动补全的后缀 +/// (`.seq.json` / `.svg` / `.png`);[defaultName] 为保存模式的默认文件名。 +Future pickFile( + BuildContext context, { + required bool save, + String? initialPath, + String? defaultName, + String suffix = '.seq.json', +}) { return showDialog( context: context, - builder: (BuildContext ctx) => FileBrowserDialog(saveMode: save, initialPath: initialPath), + builder: (BuildContext ctx) => FileBrowserDialog( + saveMode: save, + initialPath: initialPath, + defaultName: defaultName, + suffix: suffix, + ), ); }