import 'package:chewie/chewie.dart'; import 'package:flutter/material.dart'; import 'package:chewie/src/chewie_progress_colors.dart' as chewie; import 'package:flutter/services.dart'; import 'package:huixiang/view_widget/custom_image.dart'; import 'package:video_player/video_player.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class ClassDetailsVideo extends StatefulWidget { final Function(bool isShowImg) changeShowImg; final Function(double height) heightFun; final bool isShowImg; final Function exitFull; final String coverImg; final String videoUrl; ClassDetailsVideo( { Key key, this.changeShowImg, this.isShowImg, this.exitFull, this.heightFun, this.coverImg, this.videoUrl }) : super(key: key); @override State createState() { return ClassDetailsVideoState(); } } class ClassDetailsVideoState extends State { VideoPlayerController videoPlayerController; ChewieController chewieAudioController; Chewie chewies; GlobalKey globalKey = GlobalKey(); double height = 0; @override void initState() { super.initState(); SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, ]); if(widget.videoUrl != null){ initVideo(widget.videoUrl); } } @override void didChangeDependencies() { if (widget.heightFun != null) WidgetsBinding.instance.addPostFrameCallback(_getContainerHeight); super.didChangeDependencies(); } _getContainerHeight(_) { if (globalKey.currentContext != null) height = globalKey.currentContext.size.height; if (widget.heightFun != null) widget.heightFun(height); print("height: $height"); } initVideo(videoUrl) async { if (videoPlayerController != null) { videoPlayerController.pause(); videoPlayerController.dispose(); } videoPlayerController = VideoPlayerController.network( videoUrl, )..initialize().then((value) { chewieAudioController = ChewieController( videoPlayerController: videoPlayerController, aspectRatio: videoPlayerController.value.aspectRatio, // aspectRatio: 1.44, //宽高比 autoPlay: false, //自动播放 looping: false, //循环播放 allowFullScreen: true, // 允许全屏 allowedScreenSleep: false, //允许屏幕休眠 materialProgressColors: chewie.ChewieProgressColors( playedColor: Colors.white, handleColor: Colors.white, backgroundColor: Colors.grey, bufferedColor: Colors.transparent, ), // 拖动条样式颜色 autoInitialize: true, ); chewieAudioController.addListener(_fullScreenListener); if (mounted) setState(() {}); }); } @override Widget build(BuildContext context) { return Container( padding: EdgeInsets.only(top: (Theme.of(context).platform == TargetPlatform.android) ? 0 : 70.h), color: Colors.black, key: globalKey,child:(videoPlayerController?.value?.isInitialized ?? false) ? videoWidget( MediaQuery.of(context).size.height, (MediaQuery.of(context).size.height/1.44)-43, widget.coverImg, ):Container()); } @override void dispose() { super.dispose(); if (chewieAudioController != null) { chewieAudioController.pause(); chewieAudioController.dispose(); chewieAudioController = null; } if (videoPlayerController != null) { videoPlayerController.pause(); videoPlayerController.dispose(); } } Widget videoWidget(double width, double height, src) { print("src : $src"); return MediaQuery( data: MediaQuery.of(context).copyWith( textScaleFactor: 0.9, ), child: Stack(children: [ (chewieAudioController != null ? Container( color: Colors.black, width: width, height:MediaQuery.of(context).size.width / 1.5, margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top), child: chewies = Chewie( controller: chewieAudioController, ), ) : Container( width: width, height:MediaQuery.of(context).size.width / 1.5, margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top), )), if (widget.isShowImg) GestureDetector( onTap: () { setState(() { widget.changeShowImg(false); if (chewieAudioController != null) chewieAudioController.play(); }); }, child: Container( width: width, height: MediaQuery.of(context).size.width / 1.5, margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top), color: Colors.black, child: Stack( children: [ Positioned( left: 0.0, top: 0.0, right: 0.0, bottom: 0.0, child: MImage( src, fit: BoxFit.cover, errorSrc: "assets/image/default_2_1.webp", fadeSrc: "assets/image/default_2_1.webp", ), ), Center( child: Icon( Icons.play_circle_outline, color: Colors.white, size: 60, ), ), ], ), ), ), ])); } Future _fullScreenListener() async { if (!chewieAudioController.isFullScreen) { Future.delayed(Duration(seconds: 1), () { widget.exitFull(); }); } } }