flutter_easy_tools 1.0.0
flutter_easy_tools: ^1.0.0 copied to clipboard
A Flutter package that simplifies common tasks like permission handling and file operations with a clean, unified API.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_easy_tools/file_handler.dart';
import 'package:flutter_easy_tools/flutter_easy_tools.dart';
void main() {
runApp(const FileHandlerExampleApp());
}
class FileHandlerExampleApp extends StatelessWidget {
const FileHandlerExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Easy Tools Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const FileHandlerExamplePage(),
);
}
}
class FileHandlerExamplePage extends StatefulWidget {
const FileHandlerExamplePage({super.key});
@override
State<FileHandlerExamplePage> createState() => _FileHandlerExamplePageState();
}
class _FileHandlerExamplePageState extends State<FileHandlerExamplePage> {
String _status = 'Ready';
double _downloadProgress = 0.0;
String? _downloadedFilePath;
bool _isDownloading = false;
final TextEditingController _urlController = TextEditingController(
text: 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
);
@override
void dispose() {
_urlController.dispose();
super.dispose();
}
Future<void> _requestPermissions() async {
setState(() => _status = 'Requesting storage permissions...');
try {
final hasPermission = await FileHandler.requestStoragePermission();
setState(() {
_status = hasPermission
? '✅ Storage permission granted!'
: '❌ Storage permission denied';
});
} catch (e) {
setState(() => _status = '❌ Error: $e');
}
}
Future<void> _downloadFile() async {
if (_isDownloading) return;
setState(() {
_status = 'Starting download...';
_downloadProgress = 0.0;
_isDownloading = true;
});
try {
final directory = await FileHandler.getApplicationDocumentsDirectory();
final fileName = _urlController.text.split('/').last;
final filePath = await FileHandler.downloadFile(
url: _urlController.text,
fileName: fileName,
directory: directory,
onProgress: (progress) {
setState(() {
_downloadProgress = progress / 100.0;
_status = 'Downloading: ${progress.toStringAsFixed(1)}%';
});
},
);
setState(() {
_isDownloading = false;
if (filePath != null) {
_downloadedFilePath = filePath;
_status = '✅ Download complete!\nPath: $filePath';
} else {
_status = '❌ Download failed';
}
});
} catch (e) {
setState(() {
_isDownloading = false;
_status = '❌ Error: $e';
});
}
}
Future<void> _checkFileExistence() async {
if (_downloadedFilePath == null) {
setState(() => _status = 'No file path available. Download a file first.');
return;
}
try {
final exists = await FileHandler.fileExists(_downloadedFilePath!);
setState(() {
_status = exists
? '✅ File exists at:\n$_downloadedFilePath'
: '❌ File does not exist';
});
} catch (e) {
setState(() => _status = '❌ Error checking file: $e');
}
}
Future<void> _deleteFile() async {
if (_downloadedFilePath == null) {
setState(() => _status = 'No file path available. Download a file first.');
return;
}
try {
final deleted = await FileHandler.deleteFile(_downloadedFilePath!);
setState(() {
if (deleted) {
_status = '✅ File deleted successfully';
_downloadedFilePath = null;
_downloadProgress = 0.0;
} else {
_status = '❌ Failed to delete file';
}
});
} catch (e) {
setState(() => _status = '❌ Error deleting file: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Easy Tools Example'),
centerTitle: true,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// URL Input
TextField(
controller: _urlController,
decoration: const InputDecoration(
labelText: 'File URL',
border: OutlineInputBorder(),
hintText: 'Enter a file URL to download',
),
),
const SizedBox(height: 16),
// Permission Button
ElevatedButton(
onPressed: _requestPermissions,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
child: const Text('Request Storage Permission'),
),
const SizedBox(height: 16),
// Download Button
ElevatedButton(
onPressed: _isDownloading ? null : _downloadFile,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
),
child: _isDownloading
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
),
SizedBox(width: 8),
Text('Downloading...'),
],
)
: const Text('Download File'),
),
const SizedBox(height: 16),
// Progress Indicator
if (_downloadProgress > 0 && _downloadProgress < 1)
Column(
children: [
LinearProgressIndicator(value: _downloadProgress),
const SizedBox(height: 8),
Text(
'${(_downloadProgress * 100).toStringAsFixed(1)}%',
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
],
),
// File Actions
if (_downloadedFilePath != null) ...[
const SizedBox(height: 16),
const Text(
'File Actions:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: _checkFileExistence,
child: const Text('Check File'),
),
),
const SizedBox(width: 8),
Expanded(
child: OutlinedButton(
onPressed: _deleteFile,
style: OutlinedButton.styleFrom(
foregroundColor: Colors.red,
side: const BorderSide(color: Colors.red),
),
child: const Text('Delete File'),
),
),
],
),
],
// Status Display
const SizedBox(height: 24),
const Text(
'Status:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
const SizedBox(height: 8),
Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(8),
),
child: SelectableText(
_status,
style: const TextStyle(fontFamily: 'monospace'),
),
),
],
),
),
);
}
}