You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.5 KiB
Dart

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:url_launcher/url_launcher.dart';
/// Opens a folder in the native file explorer.
/// Returns true if a launch was attempted.
Future<bool> openFolder(String path) async {
if (path.isEmpty) return false;
try {
if (kIsWeb) {
final uri = _parseLaunchableUri(path);
if (uri != null) {
return await launchUrl(uri, mode: LaunchMode.platformDefault);
}
return false;
}
if (Platform.isAndroid || Platform.isIOS) {
final uri = _parseLaunchableUri(path);
if (uri != null) {
return await launchUrl(uri, mode: LaunchMode.externalApplication);
}
return false; // Non-HTTP paths (e.g., Windows shares) are not openable on mobile
}
if (Platform.isWindows) {
await Process.run('explorer', [path]);
return true;
} else if (Platform.isMacOS) {
await Process.run('open', [path]);
return true;
} else if (Platform.isLinux) {
await Process.run('xdg-open', [path]);
return true;
}
} catch (_) {
// ignore errors; caller can show a message
}
return false;
}
Uri? _parseLaunchableUri(String input) {
final s = input.trim();
if (s.isEmpty) return null;
if (s.startsWith('http://') || s.startsWith('https://') || s.startsWith('content://') || s.startsWith('file://')) {
return Uri.tryParse(s);
}
// Allow smb:// or ftp:// if present
if (s.startsWith('smb://') || s.startsWith('ftp://')) {
return Uri.tryParse(s);
}
return null;
}