path_manager 1.0.0
path_manager: ^1.0.0 copied to clipboard
Plugin for getting commonly used locations on host file system via FFI
import 'dart:io';
import 'package:material_ui/material_ui.dart';
import 'package:path_manager/path_manager.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Directory? _temporaryDirectory;
Directory? _applicationSupportDirectory;
Directory? _documentsDirectory;
Directory? _applicationCacheDirectory;
Directory? _noBackupDirectory;
Directory? _downloadsDirectory;
String _externalStorageStatus = 'Loading...';
String _desktopStatus = 'Loading...';
String _picturesStatus = 'Loading...';
String _musicStatus = 'Loading...';
String _videosStatus = 'Loading...';
String _diskSpaceStatus = 'Loading...';
String _cacheSizeStatus = 'Loading...';
String _cacheActionStatus = 'Idle';
String? _syncTemporaryPath;
String _exclusionTestResult = 'Not tested';
String? _error;
@override
void initState() {
super.initState();
_loadPaths();
}
Future<String> _resolveMediaPath(Future<Directory> Function() getter) async {
try {
return (await getter()).path;
} on UnsupportedError {
return 'Unsupported on this platform';
} catch (e) {
return 'Error: $e';
}
}
Future<String> _resolveDiskSpace() async {
try {
final space = await PathManager.getDiskSpace();
return '${PathManager.formatBytes(space.freeBytes)} free '
'of ${PathManager.formatBytes(space.totalBytes)}';
} on FileSystemException catch (e) {
return 'Unavailable: ${e.message}';
}
}
Future<void> _loadPaths() async {
try {
final temp = await PathManager.getTemporaryDirectory();
final appSupport = await PathManager.getApplicationSupportDirectory();
final docs = await PathManager.getApplicationDocumentsDirectory();
final cache = await PathManager.getApplicationCacheDirectory();
final syncTemp = PathManager.getTemporaryDirectorySync();
final cacheSize = await PathManager.getApplicationCacheSize();
Directory? noBackup;
Directory? downloads;
String extStatus = 'Unsupported on this platform';
try {
noBackup = await PathManager.getApplicationNoBackupDirectory();
} catch (e) {
debugPrint('Error loading no-backup dir: $e');
}
try {
downloads = await PathManager.getDownloadsDirectory();
} catch (e) {
debugPrint('Error loading downloads dir: $e');
}
try {
final extStorage = await PathManager.getExternalStorageDirectory(
StorageDirectory.music,
);
extStatus = extStorage.path;
} on UnsupportedError {
extStatus = 'Unsupported on this platform';
} catch (e) {
extStatus = 'Error: $e';
}
final desktop = await _resolveMediaPath(PathManager.getDesktopDirectory);
final pictures = await _resolveMediaPath(
PathManager.getPicturesDirectory,
);
final music = await _resolveMediaPath(PathManager.getMusicDirectory);
final videos = await _resolveMediaPath(PathManager.getVideosDirectory);
final diskSpace = await _resolveDiskSpace();
setState(() {
_temporaryDirectory = temp;
_applicationSupportDirectory = appSupport;
_documentsDirectory = docs;
_applicationCacheDirectory = cache;
_noBackupDirectory = noBackup;
_downloadsDirectory = downloads;
_externalStorageStatus = extStatus;
_desktopStatus = desktop;
_picturesStatus = pictures;
_musicStatus = music;
_videosStatus = videos;
_diskSpaceStatus = diskSpace;
_cacheSizeStatus = PathManager.formatBytes(cacheSize);
_syncTemporaryPath = syncTemp.path;
});
} catch (e) {
setState(() {
_error = e.toString();
});
}
}
Future<void> _clearCache() async {
setState(() {
_cacheActionStatus = 'Clearing...';
});
try {
await PathManager.clearApplicationCache();
final remaining = await PathManager.getApplicationCacheSize();
setState(() {
_cacheSizeStatus = PathManager.formatBytes(remaining);
_cacheActionStatus = 'Cache cleared.';
});
} catch (e) {
setState(() {
_cacheActionStatus = 'Failed: $e';
});
}
}
Future<void> _testBackupExclusion() async {
setState(() {
_exclusionTestResult = 'Testing...';
});
try {
final tempDir = await PathManager.getTemporaryDirectory();
final testFile = File('${tempDir.path}/example_test_file.txt');
await testFile.writeAsString('Hello World');
await PathManager.setApplicationPathIsExcludedFromBackup(
testFile.path,
true,
);
setState(() {
_exclusionTestResult = 'Success: Excluded file from backup.';
});
} on UnsupportedError catch (e) {
setState(() {
_exclusionTestResult = 'Unsupported on this platform: ${e.message}';
});
} catch (e) {
setState(() {
_exclusionTestResult = 'Failed: $e';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(useMaterial3: true),
home: Scaffold(
appBar: AppBar(
title: const Text('PathManager Example'),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: _error != null
? Center(
child: Text(
'Error: $_error',
style: const TextStyle(color: Colors.red),
),
)
: ListView(
children: [
_buildPathCard(
'Temporary Directory',
_temporaryDirectory?.path,
),
_buildPathCard(
'Application Support Directory',
_applicationSupportDirectory?.path,
),
_buildPathCard(
'Documents Directory',
_documentsDirectory?.path,
),
_buildPathCard(
'Application Cache Directory',
_applicationCacheDirectory?.path,
),
_buildPathCard(
'No Backup Directory',
_noBackupDirectory?.path,
),
_buildPathCard(
'Downloads Directory',
_downloadsDirectory?.path,
),
_buildPathCard(
'External Storage Directory',
_externalStorageStatus,
),
_buildPathCard('Desktop Directory', _desktopStatus),
_buildPathCard('Pictures Directory', _picturesStatus),
_buildPathCard('Music Directory', _musicStatus),
_buildPathCard('Videos Directory', _videosStatus),
_buildPathCard(
'Synchronous Temporary Directory',
_syncTemporaryPath,
),
_buildPathCard('Disk Space', _diskSpaceStatus),
const SizedBox(height: 16),
Card(
color: Colors.blueGrey.shade900,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Application Cache Utilities',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
const SizedBox(height: 8),
Text(
'Cache size: $_cacheSizeStatus',
style: const TextStyle(fontFamily: 'monospace'),
),
Text(
'Status: $_cacheActionStatus',
style: const TextStyle(fontFamily: 'monospace'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: _clearCache,
child: const Text('Clear Cache'),
),
],
),
),
),
const SizedBox(height: 16),
Card(
color: Colors.blueGrey.shade900,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Test Backup Exclusion API',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
const SizedBox(height: 8),
Text(
'Status: $_exclusionTestResult',
style: const TextStyle(fontFamily: 'monospace'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: _testBackupExclusion,
child: const Text('Run Exclusion Test'),
),
],
),
),
),
],
),
),
),
);
}
Widget _buildPathCard(String title, String? path) {
return Card(
margin: const EdgeInsets.symmetric(vertical: 8.0),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
const SizedBox(height: 8),
Text(
path ?? 'Loading...',
style: TextStyle(
fontFamily: 'monospace',
color: path != null ? Colors.greenAccent : Colors.grey,
),
),
],
),
),
);
}
}