set_wallpaper 0.0.3
set_wallpaper: ^0.0.3 copied to clipboard
A Flutter plugin for setting wallpapers on Android devices seamlessly, supporting home screen, lock screen, and both.
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:set_wallpaper/set_wallpaper.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepPurple,
brightness: Brightness.light,
),
),
darkTheme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepPurple,
brightness: Brightness.dark,
),
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen>
with SingleTickerProviderStateMixin {
String? _selectedImagePath;
String _platformVersion = 'Unknown';
bool _isLoading = false;
late AnimationController _animationController;
late Animation<double> _fadeAnimation;
@override
void initState() {
super.initState();
_getPlatformVersion();
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 300),
);
_fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeIn),
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
final _setWallpaperPlugin = SetWallpaper();
Future<void> _getPlatformVersion() async {
try {
final version = await _setWallpaperPlugin.getPlatformVersion() ??
'Unknown platform version';
setState(() {
_platformVersion = version;
});
} on PlatformException {
setState(() {
_platformVersion = 'Failed to get platform version.';
});
}
}
Future<void> _pickImage() async {
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickImage(
source: ImageSource.gallery,
imageQuality: 100,
);
if (image != null) {
setState(() {
_selectedImagePath = image.path;
});
_animationController.reset();
_animationController.forward();
}
}
Future<void> _setWallpaper(WallpaperType type) async {
if (_selectedImagePath == null) {
_showMessage('Please select an image first', isError: true);
return;
}
setState(() {
_isLoading = true;
});
try {
await SetWallpaper.setWallpaper(
imagePath: _selectedImagePath!,
wallpaperType: type,
);
if (mounted) {
_showMessage('Wallpaper set successfully! ✨');
}
} catch (e) {
if (mounted) {
_showMessage('Failed to set wallpaper: $e', isError: true);
}
} finally {
setState(() {
_isLoading = false;
});
}
}
void _showMessage(String message, {bool isError = false}) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: isError ? Colors.red : Colors.green,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
margin: const EdgeInsets.all(16),
),
);
}
@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(
title: const Text('Set Wallpaper'),
centerTitle: true,
elevation: 0,
backgroundColor: Colors.transparent,
),
body: Stack(
children: [
SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: colorScheme.primaryContainer.withOpacity(0.2),
borderRadius: BorderRadius.circular(12),
),
child: Text(
'Running on: $_platformVersion',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodyLarge,
),
),
const SizedBox(height: 24),
if (_selectedImagePath != null)
FadeTransition(
opacity: _fadeAnimation,
child: Container(
height: 400,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
blurRadius: 10,
offset: const Offset(0, 5),
),
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.file(
File(_selectedImagePath!),
fit: BoxFit.cover,
),
),
),
)
else
Container(
height: 200,
decoration: BoxDecoration(
color: colorScheme.primaryContainer.withOpacity(0.1),
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: colorScheme.primary.withOpacity(0.2),
width: 2,
style: BorderStyle.solid,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.add_photo_alternate_outlined,
size: 48,
color: colorScheme.primary,
),
const SizedBox(height: 12),
Text(
'Select an image to get started',
style: TextStyle(
color: colorScheme.primary,
fontSize: 16,
),
),
],
),
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: _pickImage,
icon: const Icon(Icons.add_photo_alternate),
label: Text(
_selectedImagePath == null
? 'Select Image'
: 'Change Image',
style: const TextStyle(fontSize: 16),
),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: colorScheme.primary,
foregroundColor: colorScheme.onPrimary,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
if (_selectedImagePath != null) ...[
const SizedBox(height: 24),
Text(
'Set Wallpaper As:',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
_WallpaperOptionButton(
icon: Icons.phone_android,
label: 'System (Home & Lock)',
onPressed: () => _setWallpaper(WallpaperType.system),
colorScheme: colorScheme,
),
const SizedBox(height: 12),
_WallpaperOptionButton(
icon: Icons.home_rounded,
label: 'Home Screen Only',
onPressed: () => _setWallpaper(WallpaperType.home),
colorScheme: colorScheme,
),
const SizedBox(height: 12),
_WallpaperOptionButton(
icon: Icons.lock_rounded,
label: 'Lock Screen Only',
onPressed: () => _setWallpaper(WallpaperType.lock),
colorScheme: colorScheme,
),
],
],
),
),
),
if (_isLoading)
Container(
color: Colors.black.withOpacity(0.5),
child: Center(
child: Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Theme.of(context).scaffoldBackgroundColor,
borderRadius: BorderRadius.circular(12),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(),
const SizedBox(height: 16),
const Text('Setting wallpaper...'),
],
),
),
),
),
],
),
);
}
}
class _WallpaperOptionButton extends StatelessWidget {
final IconData icon;
final String label;
final VoidCallback onPressed;
final ColorScheme colorScheme;
const _WallpaperOptionButton({
required this.icon,
required this.label,
required this.onPressed,
required this.colorScheme,
});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: colorScheme.primary.withOpacity(0.1),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: OutlinedButton.icon(
onPressed: onPressed,
icon: Icon(icon),
label: Text(label),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
side: BorderSide(color: colorScheme.primary),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
backgroundColor: colorScheme.surface,
),
),
);
}
}