flutter_absolute_path_provider 0.0.1
flutter_absolute_path_provider: ^0.0.1 copied to clipboard
A package that folders directories (absolute path) from Android and iOS device.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_absolute_path_provider/flutter_absolute_path_provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Absolute Path Example',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const DirectoryExampleScreen(),
);
}
}
class DirectoryExampleScreen extends StatefulWidget {
const DirectoryExampleScreen({super.key});
@override
State<DirectoryExampleScreen> createState() => _DirectoryExampleScreenState();
}
class _DirectoryExampleScreenState extends State<DirectoryExampleScreen> {
final Map<DirectoryType, String?> _directoryPaths = {};
DirectoryType? _selectedType;
bool _isLoading = false;
String? _error;
@override
void initState() {
super.initState();
_checkPlatformSupport();
}
Future<void> _checkPlatformSupport() async {
if (!AbsolutePath.isPlatformSupported) {
setState(() {
_error = 'This platform is not supported';
});
}
}
Future<void> _getDirectoryPath(DirectoryType type) async {
setState(() {
_isLoading = true;
_error = null;
_selectedType = type;
});
try {
final directory = await AbsolutePath.absoluteDirectory(dirType: type);
if (directory != null) {
// Check if directory exists and is writable
await directory.ensureWritable();
setState(() {
_directoryPaths[type] = directory.path;
});
// Example: List directory contents
final files = await directory.list().toList();
debugPrint('Files in ${type.value}: ${files.length}');
} else {
setState(() {
_error = 'Directory not available';
});
}
} on DirectoryException catch (e) {
setState(() {
_error = e.toString();
});
} catch (e) {
setState(() {
_error = 'Unexpected error: $e';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Directory Paths Example'),
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Select a directory type to get its path:',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
Wrap(
spacing: 8,
runSpacing: 8,
children: DirectoryType.values.map((type) {
return ActionChip(
label: Text(type.value),
onPressed: () => _getDirectoryPath(type),
backgroundColor: _selectedType == type
? Theme.of(context).primaryColor.withOpacity(0.2)
: null,
);
}).toList(),
),
const SizedBox(height: 24),
if (_error != null) ...[
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.red.shade100,
borderRadius: BorderRadius.circular(8),
),
child: Text(
_error!,
style: const TextStyle(color: Colors.red),
),
),
const SizedBox(height: 16),
],
Expanded(
child: Card(
child: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: _directoryPaths.length,
itemBuilder: (context, index) {
final type = _directoryPaths.keys.elementAt(index);
final path = _directoryPaths[type];
return ListTile(
title: Text(type.value),
subtitle: Text(path ?? 'Not available'),
trailing: IconButton(
icon: const Icon(Icons.folder_open),
onPressed: () async {
if (path != null) {
final dir = Directory(path);
if (await dir.exists()) {
// Here you could implement directory opening logic
debugPrint('Opening directory: $path');
}
}
},
),
);
},
),
),
),
],
),
),
);
}
}