Skip to content

Commit 38fd0a6

Browse files
committed
Refactor file export logic to handle Android platform and improve file saving process
1 parent d5e4559 commit 38fd0a6

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

packages/lw_sysapi/lib/src/api/save.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart';
77
import 'package:flutter/material.dart';
88

99
bool supportsShare() => kIsWeb || !Platform.isLinux;
10+
bool supportsSave() => kIsWeb || !Platform.isIOS;
1011

1112
Future<void> exportFile({
1213
required BuildContext context,
@@ -18,7 +19,7 @@ Future<void> exportFile({
1819
required String label,
1920
bool share = false,
2021
}) async {
21-
if (share && supportsShare()) {
22+
if (share && supportsShare() || (!share && !supportsSave())) {
2223
return exportUsingShare(
2324
bytes: bytes,
2425
fileName: fileName,
@@ -27,12 +28,14 @@ Future<void> exportFile({
2728
label: label,
2829
);
2930
}
30-
return save.exportFile(
31-
bytes,
32-
fileName,
33-
fileExtension,
34-
mimeType,
35-
uniformTypeIdentifier,
36-
label,
37-
);
31+
if (supportsSave()) {
32+
return save.exportFile(
33+
bytes,
34+
fileName,
35+
fileExtension,
36+
mimeType,
37+
uniformTypeIdentifier,
38+
label,
39+
);
40+
}
3841
}

packages/lw_sysapi/lib/src/api/src/save.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import 'dart:io';
12
import 'dart:typed_data';
23

34
import 'package:file_picker/file_picker.dart';
5+
import 'package:lw_sysapi/src/api/api.dart';
46

57
Future<void> exportFile(
68
Uint8List bytes,
@@ -10,11 +12,29 @@ Future<void> exportFile(
1012
String uniformTypeIdentifier,
1113
String label,
1214
) async {
13-
await FilePicker.platform.saveFile(
15+
if (Platform.isAndroid) {
16+
await platform.invokeMethod('saveFile', {
17+
'mime': mimeType,
18+
'data': bytes,
19+
'name': '$fileName.$fileExtension',
20+
});
21+
return;
22+
}
23+
var file = await FilePicker.platform.saveFile(
1424
dialogTitle: label,
1525
fileName: '$fileName.$fileExtension',
1626
bytes: bytes,
1727
type: FileType.custom,
1828
allowedExtensions: [fileExtension],
1929
);
30+
if (file == null) return;
31+
if (!file.endsWith('.$fileExtension')) {
32+
final dotIndex = file.lastIndexOf('.');
33+
if (dotIndex != -1) {
34+
file = file.substring(0, dotIndex);
35+
}
36+
file = '$file.$fileExtension';
37+
}
38+
final outputFile = File(file);
39+
await outputFile.writeAsBytes(bytes);
2040
}

0 commit comments

Comments
 (0)