Simple plugin to implement Picture in Picture support for Android only

Simple plugin to implement Picture in Picture support for Android only.

Android Setup:

You need to declare that your app supports Picture in Picture mode by adding to your AndroidManifest.xml the following line:

<activity
    ...
    android:supportsPictureInPicture="true"
    ...>
</activity>

Enter Picture in Picture mode:

int result = await FlutterPip.enterPictureInPictureMode();

If the result returns 0, the app has entered PiP mode successfully, if it returns 1 then most probably this indicates that the device does not support PiP mode.

This function supports an optional parameter of type PipRatio, for example:

int result = await FlutterPip.enterPictureInPictureMode(
    PipRatio(
        width: ...
        height: ...
    )
);

Default values for pipRatio are width: 16 and height: 9. You also need to take in
the consideration that PiP mode only supports a value range from 0.418410 to 2.390000 for Aspect Ratio,
going anything below the minimum or above the maximum will throw a PipRatioException exception.

To check whether the app is on Picture in Picture mode:

bool result = await FlutterPip.isInPictureInPictureMode();

Simply returns a boolean indicating if the app is on PiP mode or not.

PipWidget

This widget has two callbacks:

PipWidget(
    onResume: (bool pipMode) {
        ...
    },
    onSuspended: () {
        ...
    }
    child: ...
}

onResume is an Async callback that returns a boolean indicating if Picture in Picture
mode is enabled/disabled, this is useful in case your app was in PiP mode and you want
to restore your Widget Tree UI.

onSuspended is just a simple callback that you can use to execute anything you want to
do when the application is thrown in the background.

GitHub:

Source: https://github.com/rsathish5885/PIP?ref=flutterawesome.com

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 253

Newsletter Updates

Enter your email address below to subscribe to our newsletter