diff --git a/lib/community/community_view/class_details.dart b/lib/community/community_view/class_details.dart index 7bd9251a..4b7c7db3 100644 --- a/lib/community/community_view/class_details.dart +++ b/lib/community/community_view/class_details.dart @@ -1,25 +1,19 @@ -import 'dart:io'; - -import 'package:chewie/chewie.dart'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; +import 'package:huixiang/community/community_view/class_details_video.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/chapter.dart'; import 'package:huixiang/retrofit/data/course_details.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/utils/font_weight.dart'; -import 'package:huixiang/view_widget/custom_image.dart'; -import 'package:chewie/src/chewie_progress_colors.dart' as chewie; import 'package:huixiang/view_widget/tips_dialog.dart'; import 'package:huixiang/web/web_view/comment_list.dart'; import 'package:huixiang/web/web_view/input_comment.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:video_player/video_player.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class ClassDetails extends StatefulWidget { @@ -33,16 +27,10 @@ class ClassDetails extends StatefulWidget { } } -class myChewieController extends ChewieController{ - -} - class _ClassDetails extends State with WidgetsBindingObserver { - VideoPlayerController videoPlayerController; - Chewie chewies; - ChewieController chewieAudioController; ApiService apiService; final GlobalKey commentKey = GlobalKey(); + final GlobalKey videoKey = GlobalKey(); final ScrollController scrollController = ScrollController(); bool isKeyBoardShow = false; var commentFocus = FocusNode(); @@ -55,6 +43,7 @@ class _ClassDetails extends State with WidgetsBindingObserver { List chapterList = []; bool isShowImg = true; int chapterIndex = 0; + double height = 0; @override void initState() { @@ -81,7 +70,9 @@ class _ClassDetails extends State with WidgetsBindingObserver { chapterList.clear(); chapterList.addAll(baseData.data); chapterIndex = 0; - initVideo(chapterList[chapterIndex].content.fileUrl); + ClassDetailsVideoState state = videoKey.currentState; + state.initVideo(chapterList[chapterIndex].content.fileUrl); + // initVideo(chapterList[chapterIndex].content.fileUrl); }); } EasyLoading.dismiss(); @@ -106,24 +97,6 @@ class _ClassDetails extends State with WidgetsBindingObserver { } } - @override - void dispose() { - /** - * 页面销毁时,视频播放器也销毁 - */ - - if (chewieAudioController != null) { - chewieAudioController.pause(); - chewieAudioController.dispose(); - chewieAudioController = null; - } - if (videoPlayerController != null) { - videoPlayerController.pause(); - videoPlayerController.dispose(); - } - super.dispose(); - } - @override void didChangeMetrics() { WidgetsBinding.instance.addPostFrameCallback((_) { @@ -159,43 +132,49 @@ class _ClassDetails extends State with WidgetsBindingObserver { child: Column( children: [ Stack( - children: [ - videoWidget( - MediaQuery.of(context).size.width, - videoPlayerController != null - ? (MediaQuery.of(context).size.width) / - videoPlayerController.value.aspectRatio - : MediaQuery.of(context).size.width / 2, - chapterList.length > chapterIndex - ? chapterList[chapterIndex].content.coverImg - : "", + children: [ + ClassDetailsVideo(key:videoKey,exitFull: (){setState(() {});}, + coverImg: chapterList.length > chapterIndex + ? chapterList[chapterIndex].content.coverImg + : "",isShowImg: this.isShowImg, + changeShowImg: (isShowImg){setState(() { + this.isShowImg = isShowImg; + });},heightFun: (height) { + this.height = height + + MediaQuery + .of(context) + .padding + .top + + kToolbarHeight + + 24; + if (mounted) setState(() {}); + },), + Container( + margin: EdgeInsets.only( + top: 40.h, left: 16.w, right: 16.w), + decoration: BoxDecoration( + color: Colors.transparent, ), - Container( - margin: EdgeInsets.only( - top: 40.h, left: 16.w, right: 16.w), - decoration: BoxDecoration( - color: Colors.transparent, - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - GestureDetector( - child: Image.asset( - "assets/image/integral_return.png", - width: 24, - height: 24, - ), - onTap: () { - Navigator.of(context).pop( - course != null ? course.viewers + 1 : 0); - }, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + GestureDetector( + child: Image.asset( + "assets/image/integral_return.png", + width: 24, + height: 24, ), - ], - ), + onTap: () { + Navigator.of(context).pop( + course != null ? course.viewers + 1 : 0); + }, + ), + ], ), - ], - ), + ), + ], + ), Container( height: 123, margin: EdgeInsets.only(bottom: 16.h), @@ -374,115 +353,9 @@ class _ClassDetails extends State with WidgetsBindingObserver { ); } - initVideo(videoUrl) async { - 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(() {}); - }); - } - - bool tempDelayedFlag = false; - - Future _fullScreenListener() async { - if (!chewieAudioController.isFullScreen && !tempDelayedFlag) { - tempDelayedFlag = true; - SystemChrome.setPreferredOrientations( - [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); - setState(() {}); - SystemChrome.setPreferredOrientations( - [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]); - Future.delayed(Duration(seconds: 1), () { - SystemChrome.setPreferredOrientations( - [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); - setState(() { - tempDelayedFlag = false; - }); - }); - } - } - - 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: - height: width / 7 * 5, - child: chewies = Chewie( - controller: chewieAudioController, - ), - ) - : Container( - width: width, - height:width / 7 * 5, - )), - if (isShowImg) - GestureDetector( - onTap: () { - setState(() { - isShowImg = false; - if (chewieAudioController != null) - chewieAudioController.play(); - }); - }, - child: Container( - width: width, - height: width / 7 * 5, - color: Colors.black, - child: Stack( - children: [ - Center( - child: MImage( - src, - fit: BoxFit.cover, - errorSrc: "assets/image/default_2_1.png", - fadeSrc: "assets/image/default_2_1.png", - ), - ), - Center( - child: Icon( - Icons.play_circle_outline, - color: Colors.white, - size: 60, - ), - ), - ], - ), - ), - ), - ])); - } - Widget anthology() { return Container( color: Colors.white, - height: 148.h, margin: EdgeInsets.only(bottom: 16), padding: EdgeInsets.all(16), child: Column( @@ -522,9 +395,9 @@ class _ClassDetails extends State with WidgetsBindingObserver { setState(() { chapterIndex = position; isShowImg = true; - if (chewieAudioController != null) - chewieAudioController.pause(); - initVideo(chapterList[position].content.fileUrl); + ClassDetailsVideoState state = videoKey.currentState; + state.initVideo(chapterList[position].content.fileUrl); + // initVideo(chapterList[position].content.fileUrl); }); }, child: classSelectItem(chapterList[position], position), diff --git a/lib/community/community_view/class_details_video.dart b/lib/community/community_view/class_details_video.dart new file mode 100644 index 00000000..a0aab6b4 --- /dev/null +++ b/lib/community/community_view/class_details_video.dart @@ -0,0 +1,180 @@ +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; + + ClassDetailsVideo( + { + Key key, + this.changeShowImg, + this.isShowImg, + this.exitFull, + this.heightFun, + this.coverImg + }) : 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, + ]); + } + + @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 { + 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:videoWidget( + MediaQuery.of(context).size.width, + videoPlayerController != null + ? (MediaQuery.of(context).size.width) / + videoPlayerController.value.aspectRatio + : MediaQuery.of(context).size.width / 2, + widget.coverImg, + )); + } + + @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: + height: width / 7 * 5, + child: chewies = Chewie( + controller: chewieAudioController, + ), + ) + : Container( + width: width, + height:width / 7 * 5, + )), + if (widget.isShowImg) + GestureDetector( + onTap: () { + setState(() { + widget.changeShowImg(false); + if (chewieAudioController != null) + chewieAudioController.play(); + }); + }, + child: Container( + width: width, + height: width / 7 * 5, + color: Colors.black, + child: Stack( + children: [ + Center( + child: MImage( + src, + fit: BoxFit.cover, + errorSrc: "assets/image/default_2_1.png", + fadeSrc: "assets/image/default_2_1.png", + ), + ), + 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(); + }); + } + } +} \ No newline at end of file diff --git a/lib/view_widget/hot_item.dart b/lib/view_widget/hot_item.dart index 79be95e1..3ff50286 100644 --- a/lib/view_widget/hot_item.dart +++ b/lib/view_widget/hot_item.dart @@ -177,14 +177,12 @@ class _HotArticleItem extends State { alignment: Alignment.center, children: [ Image.network( - widget.article != null ? widget.article.coverImg : "", + widget.article?.coverImg ?? "", fit: BoxFit.fill, height: double.infinity, ), Visibility( - visible: (widget.article != null && - widget.article.coverImg != "" && - widget.article.coverImg.endsWith(".mp4")), + visible: ((widget.article?.coverImg??"").endsWith(".mp4")), child: Icon( Icons.play_circle_outline, size: 24,