refactor: modify data export function
This commit is contained in:
@@ -12,6 +12,8 @@ import 'package:file_picker/file_picker.dart';
|
|||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:package_info_plus/package_info_plus.dart';
|
import 'package:package_info_plus/package_info_plus.dart';
|
||||||
|
import 'package:share_plus/share_plus.dart';
|
||||||
|
import 'package:cross_file/cross_file.dart';
|
||||||
|
|
||||||
class SettingsScreen extends StatefulWidget {
|
class SettingsScreen extends StatefulWidget {
|
||||||
const SettingsScreen({super.key});
|
const SettingsScreen({super.key});
|
||||||
@@ -388,15 +390,15 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
children: [
|
children: [
|
||||||
Icon(Icons.share, color: Theme.of(context).colorScheme.primary),
|
Icon(Icons.share, color: Theme.of(context).colorScheme.primary),
|
||||||
const SizedBox(width: 12),
|
const SizedBox(width: 12),
|
||||||
Text('数据导出', style: AppTheme.titleMedium),
|
Text('数据分享', style: AppTheme.titleMedium),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
_buildActionButton(
|
_buildActionButton(
|
||||||
icon: Icons.download,
|
icon: Icons.share,
|
||||||
title: '导出数据',
|
title: '分享数据',
|
||||||
subtitle: '将记录导出为JSON文件',
|
subtitle: '将记录分享为JSON文件',
|
||||||
onTap: _exportData,
|
onTap: _shareData,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
_buildActionButton(
|
_buildActionButton(
|
||||||
@@ -501,42 +503,12 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String?> _selectDirectory() async {
|
|
||||||
try {
|
|
||||||
// 使用文件选择器选择目录
|
|
||||||
final directory = await FilePicker.platform.getDirectoryPath(
|
|
||||||
dialogTitle: '选择导出位置',
|
|
||||||
lockParentWindow: true,
|
|
||||||
);
|
|
||||||
return directory;
|
|
||||||
} catch (e) {
|
|
||||||
// 如果文件选择器失败,使用默认的文档目录
|
|
||||||
try {
|
|
||||||
final documentsDir = await getApplicationDocumentsDirectory();
|
|
||||||
final exportDir = Directory(path.join(documentsDir.path, 'LBJ_Exports'));
|
|
||||||
if (!await exportDir.exists()) {
|
|
||||||
await exportDir.create(recursive: true);
|
|
||||||
}
|
|
||||||
return exportDir.path;
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _exportData() async {
|
|
||||||
|
Future<void> _shareData() async {
|
||||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 让用户选择保存位置
|
|
||||||
final fileName =
|
|
||||||
'LBJ_Console_${DateTime.now().year}${DateTime.now().month.toString().padLeft(2, '0')}${DateTime.now().day.toString().padLeft(2, '0')}.json';
|
|
||||||
|
|
||||||
String? selectedDirectory = await _selectDirectory();
|
|
||||||
if (selectedDirectory == null) return;
|
|
||||||
|
|
||||||
final filePath = path.join(selectedDirectory, fileName);
|
|
||||||
|
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
barrierDismissible: false,
|
barrierDismissible: false,
|
||||||
@@ -545,46 +517,29 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
children: [
|
children: [
|
||||||
CircularProgressIndicator(),
|
CircularProgressIndicator(),
|
||||||
SizedBox(width: 16),
|
SizedBox(width: 16),
|
||||||
Text('正在导出数据...'),
|
Text('正在准备分享数据...'),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final exportedPath = await _databaseService.exportDataAsJson(customPath: filePath);
|
final exportedPath = await _databaseService.exportDataAsJson();
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
|
|
||||||
if (exportedPath != null) {
|
if (exportedPath != null) {
|
||||||
final file = File(exportedPath);
|
final file = File(exportedPath);
|
||||||
final fileName = file.path.split(Platform.pathSeparator).last;
|
final fileName = file.path.split(Platform.pathSeparator).last;
|
||||||
|
|
||||||
scaffoldMessenger.showSnackBar(
|
await Share.shareXFiles(
|
||||||
SnackBar(
|
[XFile(file.path)],
|
||||||
content: Text('数据已导出到:$fileName'),
|
subject: 'LBJ Console Data',
|
||||||
action: SnackBarAction(
|
text: '',
|
||||||
label: '查看',
|
|
||||||
onPressed: () async {
|
|
||||||
// 打开文件所在目录
|
|
||||||
try {
|
|
||||||
final directory = file.parent;
|
|
||||||
await Process.run('explorer', [directory.path]);
|
|
||||||
} catch (e) {
|
|
||||||
// 如果无法打开目录,显示路径
|
|
||||||
scaffoldMessenger.showSnackBar(
|
|
||||||
SnackBar(
|
|
||||||
content: Text('文件路径:${file.path}'),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
scaffoldMessenger.showSnackBar(
|
scaffoldMessenger.showSnackBar(
|
||||||
const SnackBar(
|
const SnackBar(
|
||||||
content: Text('导出失败'),
|
content: Text('分享失败:无法生成数据文件'),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -592,14 +547,14 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
scaffoldMessenger.showSnackBar(
|
scaffoldMessenger.showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text('导出错误:$e'),
|
content: Text('分享错误:$e'),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
scaffoldMessenger.showSnackBar(
|
scaffoldMessenger.showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text('选择目录错误:$e'),
|
content: Text('分享错误:$e'),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
|||||||
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
|
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
|
||||||
# In Windows, build-name is used as the major, minor, and patch parts
|
# In Windows, build-name is used as the major, minor, and patch parts
|
||||||
# of the product and file versions while build-number is used as the build suffix.
|
# of the product and file versions while build-number is used as the build suffix.
|
||||||
version: 0.1.3-flutter
|
version: 0.1.4-flutter
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.5.4
|
sdk: ^3.5.4
|
||||||
|
|||||||
Reference in New Issue
Block a user