我会分享一些在网上找不到,或者遇到问题并解决的事情。
最近在调研Flutter视频播放,在横屏切换到竖屏的时候遇到了点问题。代码如下:
class PortraitPage extends StatefulWidget {
const PortraitPage({Key? key}) : super(key: key);
@override
_PortraitPageState createState() => _PortraitPageState();
}
class _PortraitPageState extends State {
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 {
@override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
]);
}
@override
void dispose() {
///页面退出时,切换为竖屏
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Stack(
children: [
Center(
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);
},
),
)
],
),
),
);
}
}
从竖屏切换到全屏没有问题,点击物理按键:Back
,会出现先退出了页面,然后再切换到竖屏。体验效果不是很好,所以修改方案:
class _VideoFullPageState extends State {
@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;
});
}
}
完整代码见GitHub