Flutter Quick Actions - Android & iOS

Flutter Quick Actions - Android & iOS

Abhishek Kumar
·Jan 4, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • What are quick actions?

What are quick actions?

Quick actions under the hood is App Shortcuts(in Android) and Home Screen Actions(in iOS) which allow users to perform certain app actions without having to open the app.

Where have I seen this?

You might have already seen and used this. Most of the popular apps have implemented this to make few of their actions more reachable to users.

IMG_0205.jpg

Screenshot 2022-01-01 at 7.39.03 PM.png

Note: Not all android launchers support this feature.

How to implement this?

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 initState of 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! 😊

Did you find this article valuable?

Support Abhishek Kumar by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this