Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Over Other Apps Using Flutter on Android

Is it possible to make enable the "Display Over Other Apps" on an Android Flutter app ?

Will bridging to the native Android API possible to enable this feature on Flutter apps? Can anyone elaborate on this please?

like image 238
JayVDiyk Avatar asked Oct 05 '20 14:10

JayVDiyk


People also ask

How do you overlay the screen in Flutter?

The Overlay in Flutter makes it easy to create visual elements on top of other widgets by adding them to the Overlay's stack. OverlayEntry is used to insert a widget into the Overlay, then Positioned or AnimatedPositioned is used to determine where it will enter within the Overlay.

How do I open an app from another app Flutter?

For opening an external app from your app in android, you need provide packageName of the app. If the plugin finds the app in the device, it will be be launched. But if the the app is not installed in the device then it leads the user to playstore link of the app.


2 Answers

The feature you describe is handled by the application permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Documentation states that this permission

Allows an app to create windows using the type WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, shown on top of all other apps.

A bit vague explanation isn't it? Lets find out what does TYPE_APPLICATION_OVERLAY means.

Application overlay windows are displayed above all activity windows (types between FIRST_APPLICATION_WINDOW and LAST_APPLICATION_WINDOW) but below critical system windows like the status bar or IME.

Why have I mentioned this two lines?

Well to answer your question we need to understand how to achieve the thing in plain android. And we do know ho it works. The thing is this method only works for android components with UI - activities, services, dialogs(latter two will require some extra work).

What does this mean in connection with Flutter? This means that there are no settings in clean Flutter to enable such a behaviour, only the tools underlying platform offers.

In context of android using this permission will allow you to use this feature for android UI containers(it needs to be explicitly requested through a permission management screen like this)

if(!Settings.canDrawOverlays(this)){
    // ask for setting 
     Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
     Uri.parse("package:" + getPackageName()));
     startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_OVERLAY_PERMISSION) {
        if (Settings.canDrawOverlays(this)) {
            // permission granted...
        }else{
            // permission not granted...
        }
    }
}

And I am not sure there is a Flutter analog yet.

Exploring further it turns out that the Flutter renders itself in special android containers FlutterActivity and FlutterFragment.

For these two you are able to force intended behaviour with relevant permission. The whole flutter UI will have possibility to be drawn as overlay. But if you want only some parts of your flutter UI to be draws as overlays, for example some flutter dialogs or parts of navigation route - you will not be able to achieve it that easily(only be creating a separate Android native UI containers and channeling your flutter UI there - AFAIK it is not recommended because having multiple flutter UI containers may cause problems)

like image 134
Pavlo Ostasha Avatar answered Sep 20 '22 17:09

Pavlo Ostasha


There already is a plugin available that claims to "show Truecaller like overlay window over all other apps along with callback events",

Please checkout the plugin system_alert_window URL: https://pub.flutter-io.cn/packages/system_alert_window

system_alert_window

A flutter plugin to show Truecaller like overlay window, over all other apps along with callback events. Android Go or Android 11 & above, this plugin shows notification bubble, in other android versions, it shows an overlay window.

like image 45
bluenile Avatar answered Sep 20 '22 17:09

bluenile