Creating a wallpaper app in Flutter: Part 2

Cooking a wallpaper app from zero to one.

Divyanshu Bhargava
Sep 8, 2019 · 3 min read

In the last blog, we saw how to make an HTTP request to fetch images and build the UI with some cool animation. If you did not read the previous one, you can start from below.

In this part, we will see how to use Method Channel to write platform-specific code for setting your wallpaper. Before moving forward let’s know a little bit about Method Channel.

Flutter uses a flexible system that allows you to call platform-specific APIs whether available in Java or Kotlin code on Android, or in Objective-C or Swift code on iOS. Messages are passed between the client (UI) and host (platform) using platform channels as illustrated in this diagram:

First, we will construct the method channel with the specified name.

static const platform = const MethodChannel('com.divyanshu.chitr/wallpaper');

Next, we will create a setWallpaper() function which will invoke our method on the channel with specified arguments.

The invoke method functions takes a method name and a list of argument. Here we are sending two arguments. One is the file path which we are getting from DefaultCacheManager. Other is the wallpaper type. Wallpaper type is an integer value which specifies whether to set the wallpaper for home screen or lock screen or both. The Value of wallpaper type is:
— 1 for the home screen.
— 2 for the lock screen.
— 3 for both.

Now it’s time to write some Android platform-specific implementation. We are going to use Kotlin here. First, go to your MainActivity.kt file inside your android directory.

Inside the onCreate() method, create a MethodChannel and call setMethodCallHandler(). Make sure to use the same channel name as was used on the Flutter client side.

Next, we will add the setWallpaper method. Inside setWallpaper method, we will define the WallpaperManager. Using WallpaperManager we will call setBitmap function and provide the bitmap and wallpaper type.

Here is the full code for MainActivity.

That’s it. With just a few lines of code, we can set our Android app wallpaper.

Flutter Community

Articles and Stories from the Flutter Community

Divyanshu Bhargava

Written by

Android & Flutter developer | Open source enthusiast | Blogger | Remote Work | Exploring new Tech. 🇮🇳

Flutter Community

Articles and Stories from the Flutter Community

More From Medium

More from Flutter Community

More from Flutter Community

More from Flutter Community

Font Features in Flutter


More from Flutter Community

More from Flutter Community

Flutter — Shadows & glows

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade