-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.dart
More file actions
91 lines (81 loc) · 2.81 KB
/
main.dart
File metadata and controls
91 lines (81 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:floating/floating.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
final floating = Floating();
Future<void> enablePip(
BuildContext context, {
bool autoEnable = false,
}) async {
final rational = Rational.landscape();
final screenSize =
MediaQuery.of(context).size * MediaQuery.of(context).devicePixelRatio;
final height = screenSize.width ~/ rational.aspectRatio;
final arguments = autoEnable
? OnLeavePiP(
aspectRatio: rational,
sourceRectHint: Rectangle<int>(
0,
(screenSize.height ~/ 2) - (height ~/ 2),
screenSize.width.toInt(),
height,
),
)
: ImmediatePiP(
aspectRatio: rational,
sourceRectHint: Rectangle<int>(
0,
(screenSize.height ~/ 2) - (height ~/ 2),
screenSize.width.toInt(),
height,
),
);
final status = await floating.enable(arguments);
debugPrint('PiP enabled? $status');
}
@override
Widget build(BuildContext context) => MaterialApp(
theme: ThemeData.dark(),
home: PiPSwitcher(
childWhenDisabled: Scaffold(
body: Center(child: Image.asset('assets/image.jpg')),
floatingActionButtonLocation:
FloatingActionButtonLocation.centerFloat,
floatingActionButton: FutureBuilder<bool>(
future: floating.isPipAvailable,
initialData: false,
builder: (context, snapshot) => snapshot.data ?? false
? Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton.extended(
onPressed: () => enablePip(context),
label: const Text('Enable PiP'),
icon: const Icon(Icons.picture_in_picture),
),
const SizedBox(height: 12),
FloatingActionButton.extended(
onPressed: () => enablePip(context, autoEnable: true),
label: const Text('Enable PiP on app minimize'),
icon: const Icon(Icons.auto_awesome),
),
],
)
: const Card(
child: Text('PiP unavailable'),
),
),
),
childWhenEnabled: Image.asset('assets/image.jpg'),
),
);
}