floating 2.0.0 floating: ^2.0.0 copied to clipboard
Picture in Picture mode management for Flutter. Available only for Android.
import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:floating/floating.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
final floating = Floating();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
floating.dispose();
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState lifecycleState) {
if (lifecycleState == AppLifecycleState.inactive) {
floating.enable(aspectRatio: Rational.square());
}
}
Future<void> enablePip(BuildContext context) async {
final rational = Rational.vertical();
final screenSize =
MediaQuery.of(context).size * MediaQuery.of(context).devicePixelRatio;
final status = await floating.enable(
aspectRatio: rational,
sourceRectHint: Rectangle<int>(
0,
0,
screenSize.width.toInt(),
screenSize.width ~/ rational.aspectRatio,
),
);
debugPrint('PiP enabled? $status');
}
@override
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Floating example app'),
),
body: Center(
child: PiPSwitcher(
childWhenDisabled: const Text('disabled'),
childWhenEnabled: const Text('enabled'),
),
),
floatingActionButton: FutureBuilder<bool>(
future: floating.isPipAvailable,
initialData: false,
builder: (context, snapshot) => snapshot.data ?? false
? PiPSwitcher(
childWhenDisabled: FloatingActionButton.extended(
onPressed: () => enablePip(context),
label: const Text('Enable PiP'),
icon: const Icon(Icons.picture_in_picture),
),
childWhenEnabled: const SizedBox(),
)
: const Card(
child: Text('PiP unavailable'),
),
),
),
);
}