fix: 安卓全屏(状态栏/导航栏区域融入界面)+ 保存/导出对话框按类型区分后缀并记忆目录
- 全屏:移除全局 SafeArea,系统栏透明;工具栏/底部状态栏底色延伸填满状态栏与导航栏区域,图标亮度随深浅主题切换(桌面端 padding 为 0 无影响) - 文件对话框:保存/导出按类型区分——工程 .seq.json、导出 .svg / .png(默认名=项目名,目录列表只显示对应后缀文件,输入自动补全/替换后缀) - 路径体验:默认目录改为桌面(移动端为应用目录),并跨会话记忆上次使用的目录;此修复同时作用于 Windows exe 与安卓版
This commit is contained in:
+113
-29
@@ -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<void> 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<FileBrowserDialog> {
|
||||
late Directory _dir;
|
||||
/// 文件名输入控制器。
|
||||
late TextEditingController _name;
|
||||
/// 当前目录下的条目(子目录 + .json 文件)。
|
||||
/// 当前目录下的条目(子目录 + 匹配后缀的文件)。
|
||||
List<FileSystemEntity> _entries = <FileSystemEntity>[];
|
||||
/// 读取目录失败时的错误信息。
|
||||
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<void> _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<String> _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<void> _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<String> known = <String>['.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<FileSystemEntity> 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<FileBrowserDialog> {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
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<FileBrowserDialog> {
|
||||
const Divider(),
|
||||
Row(
|
||||
children: <Widget>[
|
||||
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<FileBrowserDialog> {
|
||||
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<FileBrowserDialog> {
|
||||
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<FileBrowserDialog> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 打开文件选择对话框并返回所选路径;[save] 决定保存或打开模式。
|
||||
Future<String?> pickFile(BuildContext context, {required bool save, String? initialPath}) {
|
||||
/// 打开文件选择对话框并返回所选路径。
|
||||
///
|
||||
/// [save] 决定保存或打开模式;[suffix] 决定文件过滤与自动补全的后缀
|
||||
/// (`.seq.json` / `.svg` / `.png`);[defaultName] 为保存模式的默认文件名。
|
||||
Future<String?> pickFile(
|
||||
BuildContext context, {
|
||||
required bool save,
|
||||
String? initialPath,
|
||||
String? defaultName,
|
||||
String suffix = '.seq.json',
|
||||
}) {
|
||||
return showDialog<String>(
|
||||
context: context,
|
||||
builder: (BuildContext ctx) => FileBrowserDialog(saveMode: save, initialPath: initialPath),
|
||||
builder: (BuildContext ctx) => FileBrowserDialog(
|
||||
saveMode: save,
|
||||
initialPath: initialPath,
|
||||
defaultName: defaultName,
|
||||
suffix: suffix,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user