file_picker macos compatibility (#442)

This commit is contained in:
wanghongenpin
2025-04-13 05:53:41 +08:00
parent dd3b540dc4
commit 3ea6dce5cb
14 changed files with 123 additions and 45 deletions

View File

@@ -18,6 +18,7 @@ import 'dart:convert';
import 'dart:io';
import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:path_provider/path_provider.dart';
@@ -89,7 +90,7 @@ Widget multiWindow(int windowId, Map<dynamic, dynamic> argument) {
}
if (argument['name'] == 'JavaScript') {
return const JavaScript();
return JavaScript(windowId: windowId);
}
if (argument['name'] == 'RegExpPage') {
@@ -215,6 +216,20 @@ void registerMethodHandler() {
return 'done';
}
if (call.method == 'pickFiles') {
var extensions = call.arguments['allowedExtensions'];
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: extensions == null ? FileType.any : FileType.custom,
allowedExtensions: extensions == null ? null : List.from(extensions),
initialDirectory: "/Downloads");
if (result == null || result.files.isEmpty) return null;
return result.files.single.path;
}
if (call.method == 'saveFile') {
return await FilePicker.platform.saveFile(fileName: call.arguments['fileName']);
}
if (call.method == 'getApplicationSupportDirectory') {
return getApplicationSupportDirectory().then((it) => it.path);
}

View File

@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@@ -14,7 +15,9 @@ import 'package:proxypin/network/components/js/md5.dart';
import 'package:proxypin/network/components/js/xhr.dart';
class JavaScript extends StatefulWidget {
const JavaScript({super.key});
final int? windowId;
const JavaScript({super.key, this.windowId});
@override
State<StatefulWidget> createState() {
@@ -88,10 +91,20 @@ class _JavaScriptState extends State<JavaScript> {
//选择文件
ElevatedButton.icon(
onPressed: () async {
FilePickerResult? result =
await FilePicker.platform.pickFiles(type: FileType.custom, allowedExtensions: ['js']);
if (result != null) {
File file = File(result.files.single.path!);
String? path;
if (Platform.isMacOS) {
path = await DesktopMultiWindow.invokeMethod(0, "pickFiles", {
"allowedExtensions": ['js']
});
WindowController.fromWindowId(widget.windowId!).show();
} else {
FilePickerResult? result =
await FilePicker.platform.pickFiles(type: FileType.custom, allowedExtensions: ['js']);
path = result?.files.single.path;
}
if (path != null) {
File file = File(path);
String content = await file.readAsString();
code.text = content;
setState(() {});

View File

@@ -349,17 +349,22 @@ class _QrEncodeState extends State<_QrEncode> with AutomaticKeepAliveClientMixin
return;
}
if (Platforms.isDesktop()) {
String? path = (await FilePicker.platform.saveFile(fileName: "qrcode.png"));
if (path == null) return;
String? path;
if (Platform.isMacOS) {
path = await DesktopMultiWindow.invokeMethod(0, "saveFile", {"fileName": "qrcode.png"});
WindowController.fromWindowId(widget.windowId!).show();
} else {
path = (await FilePicker.platform.saveFile(fileName: "qrcode.png"));
}
var imageBytes = await toImageBytes();
if (imageBytes == null) return;
if (path == null) return;
await File(path).writeAsBytes(imageBytes);
if (mounted) {
FlutterToastr.show(localizations.saveSuccess, context, duration: 2);
}
var imageBytes = await toImageBytes();
if (imageBytes == null) return;
await File(path).writeAsBytes(imageBytes);
if (mounted) {
FlutterToastr.show(localizations.saveSuccess, context, duration: 2);
}
}