forked from crazecoder/open_file
-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathopen_filex.dart
More file actions
50 lines (44 loc) · 1.61 KB
/
open_filex.dart
File metadata and controls
50 lines (44 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart';
import '../../open_filex_plus.dart';
/// OpenFilex class
class OpenFilex {
static const MethodChannel _channel = MethodChannel('open_file');
OpenFilex._();
///linuxDesktopName like 'xdg'/'gnome'
static Future<OpenResult> open(String filePath,
{String? type, String? uti, String linuxDesktopName = "xdg"}) async {
if (!Platform.isIOS && !Platform.isAndroid) {
int result = -1;
if (Platform.isMacOS) {
final process = await Process.start('open', [filePath]);
result = await process.exitCode;
} else if (Platform.isWindows) {
final process = await Process.start('cmd', ['/c', 'start', '', filePath]);
result = await process.exitCode;
} else if (Platform.isLinux) {
final process = await Process.start("$linuxDesktopName-open", [filePath]);
result = await process.exitCode;
} else {
throw UnsupportedError("Unsupported platform");
}
return OpenResult(
type: result == 0 ? ResultType.done : ResultType.error,
message: result == 0
? "done"
: result == -1
? "This operating system is not currently supported"
: "there are some errors when open $filePath");
}
Map<String, String?> map = {
"file_path": filePath,
"type": type,
"uti": uti,
};
final result = await _channel.invokeMethod('open_file', map);
final resultMap = json.decode(result) as Map<String, dynamic>;
return OpenResult.fromJson(resultMap);
}
}