image_downloader_saver 1.0.2
image_downloader_saver: ^1.0.2 copied to clipboard
A lightweight, no-dependency Flutter plugin to download and save images to the Gallery or App Files with status codes, duplicate detection, and user preference support.
import 'package:flutter/material.dart';
import 'package:image_downloader_saver/image_downloader_saver.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image Downloader Saver Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const ImageDownloaderExample(),
);
}
}
class ImageDownloaderExample extends StatefulWidget {
const ImageDownloaderExample({super.key});
@override
State<ImageDownloaderExample> createState() => _ImageDownloaderExampleState();
}
class _ImageDownloaderExampleState extends State<ImageDownloaderExample> {
final TextEditingController _urlController = TextEditingController(
text: 'https://picsum.photos/600/400', // sample image URL
);
SaveResult? _result;
bool _isLoading = false;
SaveLocation _saveLocation = SaveLocation.gallery;
Future<void> _downloadImage() async {
final url = _urlController.text.trim();
if (url.isEmpty) {
setState(() => _result = SaveResult(400, 'Please enter a valid URL'));
return;
}
setState(() {
_isLoading = true;
_result = null;
});
try {
final result = await ImageSaver.saveImage(url, saveTo: _saveLocation);
setState(() => _result = result);
} catch (e) {
setState(() => _result = SaveResult(500, 'Error: $e'));
} finally {
setState(() => _isLoading = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Image Downloader Saver'),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Enter an image URL to download and save:',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 8),
TextField(
controller: _urlController,
decoration: InputDecoration(
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
hintText: 'https://example.com/image.jpg',
),
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Save to:', style: TextStyle(fontSize: 16)),
DropdownButton<SaveLocation>(
value: _saveLocation,
items: const [
DropdownMenuItem(
value: SaveLocation.gallery,
child: Text('Gallery'),
),
DropdownMenuItem(
value: SaveLocation.files,
child: Text('Files'),
),
],
onChanged: (value) => setState(() => _saveLocation = value!),
),
],
),
const SizedBox(height: 20),
ElevatedButton.icon(
onPressed: _isLoading ? null : _downloadImage,
icon: const Icon(Icons.download),
label: Text(_isLoading ? 'Downloading...' : 'Download & Save'),
style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 14)),
),
const SizedBox(height: 24),
if (_result != null) ...[
const Divider(),
Center(
child: Column(
children: [
Text(
'Status Code: ${_result!.code}',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: _result!.isSuccess ? Colors.green : Colors.red,
),
),
const SizedBox(height: 8),
Text(
_result!.message,
style: const TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
],
),
),
],
],
),
),
);
}
}