fix(android): 保存/导出写入系统公共 Documents(可见),不再落到应用私有 data 目录
- 原生通道 seqstudio/docs:Android 10+ 走 MediaStore 写入 Documents/SequenceDiagram(免权限、商店合规);Android 9- 直接写公共目录(WRITE_EXTERNAL_STORAGE 限 maxSdk=28) - 安卓保存/打开/导出改用「Documents 对话框」:保存输入文件名(自动补全后缀),打开列出 Documents 中的 .seq.json 点选 - 控制器新增 markSaved(外部通道保存后标记干净);重新构建签名 APK
This commit is contained in:
@@ -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/android_docs.dart';
|
||||
import '../persistence/storage.dart';
|
||||
import '../state/app_controller.dart';
|
||||
import '../theme/tokens.dart';
|
||||
@@ -419,6 +420,180 @@ Future<String?> pickFile(
|
||||
);
|
||||
}
|
||||
|
||||
// ===================== 安卓公共 Documents 文件对话框 =====================
|
||||
|
||||
/// 安卓版文件对话框:保存到 / 打开自公共 `Documents/SequenceDiagram`。
|
||||
///
|
||||
/// - 保存模式:输入文件名(自动补全后缀),确认后由调用方经 [AndroidDocs] 写入;
|
||||
/// - 打开模式:列出 Documents 目录中匹配后缀的文件,点选返回文件名。
|
||||
/// 返回值为文件名;取消返回 null。
|
||||
class AndroidFileDialog extends StatefulWidget {
|
||||
const AndroidFileDialog({
|
||||
super.key,
|
||||
required this.saveMode,
|
||||
required this.defaultName,
|
||||
required this.suffix,
|
||||
});
|
||||
|
||||
/// 保存(true)或打开(false)。
|
||||
final bool saveMode;
|
||||
/// 保存模式的默认文件名。
|
||||
final String defaultName;
|
||||
/// 文件后缀(过滤与自动补全)。
|
||||
final String suffix;
|
||||
|
||||
@override
|
||||
State<AndroidFileDialog> createState() => _AndroidFileDialogState();
|
||||
}
|
||||
|
||||
class _AndroidFileDialogState extends State<AndroidFileDialog> {
|
||||
late TextEditingController _name;
|
||||
List<Map<String, dynamic>> _files = <Map<String, dynamic>>[];
|
||||
bool _loading = false;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_name = TextEditingController(text: widget.defaultName);
|
||||
if (!widget.saveMode) _load();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_name.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// 列出 Documents 目录中的匹配文件(新在前)。
|
||||
Future<void> _load() async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
final List<Map<String, dynamic>> all = await AndroidDocs.listFiles();
|
||||
final String sfx = widget.suffix.toLowerCase();
|
||||
final List<Map<String, dynamic>> matched = all
|
||||
.where((Map<String, dynamic> f) => (f['name'] as String? ?? '').toLowerCase().endsWith(sfx))
|
||||
.toList();
|
||||
if (mounted) setState(() => _files = matched);
|
||||
} catch (e) {
|
||||
if (mounted) setState(() => _error = '无法读取 Documents 目录:$e');
|
||||
} finally {
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
/// 替换其它已知后缀并补全目标后缀。
|
||||
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}';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final Tokens t = context.tok;
|
||||
return AlertDialog(
|
||||
backgroundColor: t.dialog,
|
||||
title: Text(widget.saveMode ? '保存到 Documents' : '从 Documents 打开'),
|
||||
content: SizedBox(
|
||||
width: 420,
|
||||
child: widget.saveMode
|
||||
? Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Text('文件将保存到平板存储的 ${AndroidDocs.displayDir}/',
|
||||
style: TextStyle(color: t.dim, fontSize: 11)),
|
||||
const SizedBox(height: 10),
|
||||
TextField(
|
||||
controller: _name,
|
||||
autofocus: true,
|
||||
style: TextStyle(color: t.text, fontSize: 12.5),
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
labelText: '文件名(${widget.suffix})',
|
||||
filled: true,
|
||||
fillColor: t.panel2,
|
||||
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
if (_loading)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Center(child: CircularProgressIndicator()),
|
||||
)
|
||||
else if (_error != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: Text(_error!, style: TextStyle(color: t.danger, fontSize: 11.5)),
|
||||
)
|
||||
else if (_files.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(14),
|
||||
child: Text('Documents 目录中暂无 ${widget.suffix} 文件',
|
||||
style: TextStyle(color: t.dim, fontSize: 11.5)),
|
||||
)
|
||||
else
|
||||
Flexible(
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: _files.length,
|
||||
itemBuilder: (BuildContext ctx, int i) {
|
||||
final Map<String, dynamic> f = _files[i];
|
||||
final String name = f['name'] as String? ?? '';
|
||||
return ListTile(
|
||||
dense: true,
|
||||
leading: Icon(Icons.description, size: 20, color: t.sub),
|
||||
title: Text(name, style: TextStyle(color: t.text, fontSize: 12)),
|
||||
onTap: () => Navigator.pop(context, name),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: widget.saveMode
|
||||
? <Widget>[
|
||||
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
final String name = _ensureSuffix(_name.text);
|
||||
if (name.isEmpty) return;
|
||||
Navigator.pop(context, name);
|
||||
},
|
||||
child: const Text('保存'),
|
||||
),
|
||||
]
|
||||
: <Widget>[
|
||||
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
|
||||
if (!_loading && _error == null)
|
||||
TextButton.icon(
|
||||
onPressed: _load,
|
||||
icon: const Icon(Icons.refresh, size: 16),
|
||||
label: const Text('刷新'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ===================== 动作编辑对话框 =====================
|
||||
|
||||
/// 动作编辑对话框:修改选中动作的名称、驱动、工位、相位与颜色。
|
||||
|
||||
Reference in New Issue
Block a user