easy_gallery_saver 1.0.0
easy_gallery_saver: ^1.0.0 copied to clipboard
Simple Flutter package to save images to gallery with one function call. Supports assets, files, and network images. No external dependencies for saving images.
example/main.dart
import 'package:flutter/material.dart';
import 'package:easy_gallery_saver/easy_gallery_saver.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Easy Gallery Saver Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool _isLoading = false;
String _statusMessage = '';
int _savedCount = 0;
// Rasmni saqlash funksiyasi
Future<void> _saveImage(String imagePath, String source) async {
setState(() {
_isLoading = true;
_statusMessage = '⏳ Saqlanmoqda...';
});
try {
bool result = await EasyGallerySaver.saveImage(
imagePath,
albumName: 'MyAppImages',
);
setState(() {
_isLoading = false;
if (result) {
_savedCount++;
_statusMessage = '✅ Rasm muvaffaqiyatli saqlandi! ($source)';
} else {
_statusMessage = '❌ Xatolik yuz berdi';
}
});
// SnackBar ko'rsatish
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
result
? '✅ Rasm galereiyaga saqlandi!'
: '❌ Rasmni saqlab bo\'lmadi',
),
backgroundColor: result ? Colors.green : Colors.red,
duration: const Duration(seconds: 2),
),
);
}
} catch (e) {
setState(() {
_isLoading = false;
_statusMessage = '❌ Xatolik: $e';
});
}
}
// Ruxsat holatini tekshirish
Future<void> _checkPermission() async {
bool hasPermission = await EasyGallerySaver.checkPermission();
if (mounted) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Ruxsat holati'),
content: Text(
hasPermission
? '✅ Galereiyaga kirish ruxsati berilgan'
: '❌ Galereiyaga kirish ruxsati yo\'q',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Easy Gallery Saver Demo'),
centerTitle: true,
elevation: 2,
actions: [
IconButton(
icon: const Icon(Icons.settings),
onPressed: _checkPermission,
tooltip: 'Ruxsat holatini tekshirish',
),
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Status Card
if (_statusMessage.isNotEmpty)
Card(
color: _statusMessage.contains('✅')
? Colors.green.shade50
: Colors.red.shade50,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
children: [
Expanded(
child: Text(
_statusMessage,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: _statusMessage.contains('✅')
? Colors.green.shade900
: Colors.red.shade900,
),
),
),
if (!_isLoading)
IconButton(
icon: const Icon(Icons.close, size: 20),
onPressed: () => setState(() => _statusMessage = ''),
),
],
),
),
),
const SizedBox(height: 20),
// Statistics Card
Card(
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
const Icon(Icons.photo_library, size: 48, color: Colors.blue),
const SizedBox(height: 8),
const Text(
'Saqlangan rasmlar',
style: TextStyle(fontSize: 16, color: Colors.grey),
),
Text(
'$_savedCount',
style: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
],
),
),
),
const SizedBox(height: 30),
// Demo rasmlar
const Text(
'Demo rasmlar:',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
// Asset rasm
_buildImageCard(
title: '1. Asset dan rasm',
description: 'Loyihadagi asset rasmni saqlash',
imageUrl: 'https://picsum.photos/400/300?random=1',
onSave: () => _saveImage(
'https://picsum.photos/800/600?random=1',
'Asset',
),
),
const SizedBox(height: 16),
// Network rasm 1
_buildImageCard(
title: '2. Internetdan rasm',
description: 'URL orqali rasmni saqlash',
imageUrl: 'https://picsum.photos/400/300?random=2',
onSave: () => _saveImage(
'https://picsum.photos/800/600?random=2',
'Network',
),
),
const SizedBox(height: 16),
// Network rasm 2
_buildImageCard(
title: '3. Tasodifiy rasm',
description: 'Har safar yangi rasm',
imageUrl: 'https://picsum.photos/400/300',
onSave: () => _saveImage(
'https://picsum.photos/800/600?random=${DateTime.now().millisecondsSinceEpoch}',
'Random',
),
),
const SizedBox(height: 30),
// Info
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.blue.shade50,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.blue.shade200),
),
child: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.info_outline, color: Colors.blue),
SizedBox(width: 8),
Text(
'Ma\'lumot',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
],
),
SizedBox(height: 8),
Text(
'• Rasmlar "MyAppImages" albomiga saqlanadi\n'
'• Avtomatik ruxsat so\'raladi\n'
'• Android va iOS qo\'llab-quvvatlanadi',
style: TextStyle(fontSize: 14, height: 1.5),
),
],
),
),
],
),
),
);
}
Widget _buildImageCard({
required String title,
required String description,
required String imageUrl,
required VoidCallback onSave,
}) {
return Card(
elevation: 2,
clipBehavior: Clip.antiAlias,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Rasm
Image.network(
imageUrl,
height: 200,
width: double.infinity,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(
height: 200,
color: Colors.grey.shade300,
child: const Center(
child: Icon(Icons.error_outline, size: 48),
),
);
},
loadingBuilder: (context, child, loadingProgress) {
if (loadingProgress == null) return child;
return Container(
height: 200,
color: Colors.grey.shade200,
child: const Center(
child: CircularProgressIndicator(),
),
);
},
),
// Ma'lumot
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
description,
style: TextStyle(
fontSize: 14,
color: Colors.grey.shade600,
),
),
const SizedBox(height: 12),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: _isLoading ? null : onSave,
icon: _isLoading
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Icon(Icons.download),
label: Text(_isLoading ? 'Saqlanmoqda...' : 'Saqlash'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 12),
),
),
),
],
),
),
],
),
);
}
}