Flutter Quick Actions - Android & iOS

Based in India.
Search for a command to run...

Based in India.
I recently had to deploy a Ktor project to Railway for my hobby project. I'll share the how-to through this article. Introduction Ktor is a framework that allows us to build web applications in Kotlin, built and backed by Jetbrains. It's open-source,...

Understanding Coroutines and Flow Kotlin Coroutines and Flows are a powerful tool for managing asynchronous programming in your Android apps. This article is a beginner's guide to understanding Kotlin Flow. First, let's understand what coroutines are...

The problem When working with JVM (Java Virtual Machine), the Garbage Collector will take care of allocating and deallocating memory for us - Most of the time. In some cases, it fails to release the memory for some objects which are no longer in use,...

What are app shortcuts? App shortcuts (introduced in Android Nougat 7.1) allow users to perform certain app actions without having to open the app. Developers can allow users to pin certain app actions to home screen. Let's see how. Where have I seen...

Quick actions under the hood are App Shortcuts(in Android) and Home Screen Actions(in iOS) which allow users to perform certain app actions without having to open the app.
You might have already seen and used this. Most of the popular apps have implemented this to make a few of their actions more reachable to users.


Note: Not all android launchers support this feature.
** Add the latest quick_action dependency in pubspec.yaml -**
flutter:
sdk: flutter
quick_actions:
Create an object of QuickActions-
QuickActions quickActions = QuickActions();
If it's not automatically imported by your IDE, you can add the following import
import 'package:quick_actions/quick_actions.dart';
Initialize the object -
quickActions.initialize((shortcutType) {
//TODO: handle shortcutType
});
We need to initialize it early in the application's lifecycle to set callbacks on different actions. We can simply add it to
initStateof the main App widget.
@override
void initState() {
super.initState();
final QuickActions quickActions = QuickActions();
quickActions.initialize((String shortcutType) {
//TODO: handle shortcutType
});
}
Add shortcuts to quickActions -
@override
void initState() {
super.initState();
final QuickActions quickActions = QuickActions();
quickActions.initialize((String shortcutType) {
//TODO: handle shortcutType
});
quickActions.setShortcutItems(<ShortcutItem>[
ShortcutItem(
type: "shortcut1", // The value passed here will be used to determine the shortcutType which we will use to handle different shortcuts,
localizedTitle: "Shortcut 1", // The label we want to show in launcher for this shortcut
icon: "assetIcon" // Icon that will be displayed with the shortcut
),
]);
}
Handle shortcuts -
We can modify the initialize method to handle shortcuts. In this example we're just printing the shortcut Name
quickActions.initialize((String shortcutType) {
switch (shortcutType) {
case 'shortcut1': print("Shortcut 1");
default : print("Unknown shortcut");
}
});
That's all!
I hope you found this article useful!
Follow me for more articles like this. Have a great day! 😊