Set A Image As A Wallpaper In Flutter
Actually its very easy to change the Home Screen, Lock Screen (or both) Wallpaper(s) on Android devices using the Plugin “wallpaper_manager”.
Follow these Steps to Add This Feature in your flutter app:-
- Add The Below TwoPlugin To “dependencies” in your “pubspec.yaml” File.
dependencies:
wallpaper_manager: ^1.0.10
flutter_cache_manager: ^2.1.0
2. Then add the Two imports in your file wherever you want to use it.
import 'package:wallpaper_manager/wallpaper_manager.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
3. Then Add The Below Function To Your “State” of “StatefulWidget”.
// Platform messages are asynchronous, so we initialize in an async method.Future<void> setWallpaperFromFile() async {String result;var file = await DefaultCacheManager().getSingleFile('https://images.unsplash.com/photo-1542435503-956c469947f6');// Platform messages may fail, so we use a try/catch PlatformException.try {result = await WallpaperManager.setWallpaperFromFile(file.path, WallpaperManager.HOME_SCREEN);} on PlatformException {result = 'Failed to get wallpaper.';}// If the widget was removed from the tree while the asynchronous platform// message was in flight, we want to discard the reply rather than calling// setState to update our non-existent appearance.if (!mounted) return;}
4. Then call this function “setWallpaperFromFile” on “onPressed” of your Button or whatever the way you like to call it.
RaisedButton(child: Text("Set wallpaper from file"),onPressed: setWallpaperFromFile,),
5. and Done. when You Call the Function It may take Few Seconds to Set the wallpaper, Because we are using a picture From URL.
We can give the direct path from our mobile Storage as well and we can give the path from assets as well, in the case of assets we have to first add the asset to “pubspec.yaml”. then we can use it.
In above Function we are setting the wallpaper On Home Screen By using “WallpaperManager.HOME_SCREEN”. We have More Options to set the wallpaper on Lock Screen by using “WallpaperManager.LOCK_SCREEN” and to set on both home and lock screens we can use this “WallpaperManager.BOTH_SCREENS”.
Below are the separate Functions for Lock Screen and for both Screens.
Lock Screen:
Future<void> setWallpaperFromFile() async {String result;var file = await DefaultCacheManager().getSingleFile('https://images.unsplash.com/photo-1542435503-956c469947f6');// Platform messages may fail, so we use a try/catch PlatformException.try {result = await WallpaperManager.setWallpaperFromFile(file.path, WallpaperManager.LOCK_SCREEN);} on PlatformException {result = 'Failed to get wallpaper.';}// If the widget was removed from the tree while the asynchronous platform// message was in flight, we want to discard the reply rather than calling// setState to update our non-existent appearance.if (!mounted) return;}
Both Screens:
Future<void> setWallpaperFromFile() async {String result;var file = await DefaultCacheManager().getSingleFile('https://images.unsplash.com/photo-1542435503-956c469947f6');// Platform messages may fail, so we use a try/catch PlatformException.try {result = await WallpaperManager.setWallpaperFromFile(file.path, WallpaperManager.BOTH_SCREENS);} on PlatformException {result = 'Failed to get wallpaper.';}// If the widget was removed from the tree while the asynchronous platform// message was in flight, we want to discard the reply rather than calling// setState to update our non-existent appearance.if (!mounted) return;}
Its Done. Thanks For Reading.