/
play_video.dart
131 lines (116 loc) · 3.39 KB
/
play_video.dart
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(home: Scaffold(body: PortraitPage()));
}
}
class PortraitPage extends StatefulWidget {
const PortraitPage({Key? key}) : super(key: key);
@override
_PortraitPageState createState() => _PortraitPageState();
}
class _PortraitPageState extends State<PortraitPage> {
VideoPlayerController? controller;
@override
void initState() {
super.initState();
controller = VideoPlayerController.network(
"https://cdn-resource.ekwing.com/acpf/data/upload/expand/2017/08/29/59a53da773e00.mp4")
..setLooping(true)
..initialize().then((_) {
controller!.play();
});
}
@override
void dispose() {
controller!.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
children: [
AspectRatio(
aspectRatio: controller!.value.aspectRatio,
child: VideoPlayer(controller!),
),
MaterialButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>
VideoFullPage(controller: controller!)));
},
child: Text("切换全屏"))
],
),
),
);
}
}
class VideoFullPage extends StatefulWidget {
final VideoPlayerController controller;
const VideoFullPage({Key? key, required this.controller}) : super(key: key);
@override
_VideoFullPageState createState() => _VideoFullPageState();
}
class _VideoFullPageState extends State<VideoFullPage> {
@override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
]);
}
@override
Widget build(BuildContext context) {
return WillPopScope(
child: MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Stack(
children: [
Center(
child: Hero(
tag: "player",
child: AspectRatio(
aspectRatio: widget.controller.value.aspectRatio,
child: VideoPlayer(widget.controller),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 25, right: 20),
child: IconButton(
icon: const Icon(Icons.arrow_back_ios),
color: Colors.white,
onPressed: () {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
Navigator.pop(context);
},
),
)
],
),
),
),
onWillPop: () async {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
Navigator.pop(context);
return false;
});
}
}