manage_wallpaper 0.0.2
manage_wallpaper: ^0.0.2 copied to clipboard
A Flutter plugin to set wallpapers and live wallpapers across multiple platforms.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:manage_wallpaper/wallpaper.dart';
import 'package:file_picker/file_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _wallpaperPlugin = Wallpaper();
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion =
await _wallpaperPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> _pickAndSetWallpaper(BuildContext context) async {
try {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.image,
);
if (result != null && result.files.single.path != null) {
String path = result.files.single.path!;
String? response = await _wallpaperPlugin.setWallpaper(path);
if (context.mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Wallpaper set: $response')));
}
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error: $e')));
}
}
}
Future<void> _pickAndSetLiveWallpaper(BuildContext context) async {
try {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.video, // or custom for gif
);
if (result != null && result.files.single.path != null) {
String path = result.files.single.path!;
await _wallpaperPlugin.setLiveWallpaper(path: path);
if (context.mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Live Wallpaper set!')));
}
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error: $e')));
}
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(
builder: (context) => Scaffold(
appBar: AppBar(title: const Text('Wallpaper Plugin Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Running on: $_platformVersion\n'),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => _pickAndSetWallpaper(context),
child: const Text('Pick Image and Set Wallpaper'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => _pickAndSetLiveWallpaper(context),
child: const Text('Pick Video and Set Live Wallpaper'),
),
],
),
),
),
),
);
}
}