You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
5.4 KiB
194 lines
5.4 KiB
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'; |
|
|
|
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<StatefulWidget> createState() { |
|
return ClassDetailsVideoState(); |
|
} |
|
} |
|
|
|
class ClassDetailsVideoState extends State<ClassDetailsVideo> { |
|
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, |
|
//宽高比 |
|
autoPlay: false, |
|
//自动播放 |
|
looping: false, |
|
//循环播放 |
|
allowFullScreen: true, |
|
// 拖动条样式颜色 |
|
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( |
|
key: globalKey,child:(videoPlayerController?.value?.isInitialized ?? false) ?videoWidget( |
|
MediaQuery.of(context).size.height, |
|
(MediaQuery.of(context).size.height/videoPlayerController.value.aspectRatio)-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: width / 7 * 5, |
|
height: height, |
|
child: chewies = Chewie( |
|
controller: chewieAudioController, |
|
), |
|
) |
|
: Container( |
|
width: width, |
|
// height:width / 7 * 5, |
|
height: height, |
|
)), |
|
if (widget.isShowImg) |
|
GestureDetector( |
|
onTap: () { |
|
setState(() { |
|
widget.changeShowImg(false); |
|
if (chewieAudioController != null) |
|
chewieAudioController.play(); |
|
}); |
|
}, |
|
child: Container( |
|
width: width, |
|
// height: width / 7 * 5, |
|
height: height, |
|
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<void> _fullScreenListener() async { |
|
if (!chewieAudioController.isFullScreen) { |
|
Future.delayed(Duration(seconds: 1), () { |
|
widget.exitFull(); |
|
}); |
|
} |
|
} |
|
} |