Browse Source

视频缩放适配更改

ff_new
w-R 3 years ago
parent
commit
30adefafe6
  1. 175
      lib/community/community_view/class_details.dart
  2. 180
      lib/community/community_view/class_details_video.dart
  3. 6
      lib/view_widget/hot_item.dart

175
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:dio/dio.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.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/generated/l10n.dart';
import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/chapter.dart'; import 'package:huixiang/retrofit/data/chapter.dart';
import 'package:huixiang/retrofit/data/course_details.dart'; import 'package:huixiang/retrofit/data/course_details.dart';
import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/font_weight.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/view_widget/tips_dialog.dart';
import 'package:huixiang/web/web_view/comment_list.dart'; import 'package:huixiang/web/web_view/comment_list.dart';
import 'package:huixiang/web/web_view/input_comment.dart'; import 'package:huixiang/web/web_view/input_comment.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
class ClassDetails extends StatefulWidget { class ClassDetails extends StatefulWidget {
@ -33,16 +27,10 @@ class ClassDetails extends StatefulWidget {
} }
} }
class myChewieController extends ChewieController{
}
class _ClassDetails extends State<ClassDetails> with WidgetsBindingObserver { class _ClassDetails extends State<ClassDetails> with WidgetsBindingObserver {
VideoPlayerController videoPlayerController;
Chewie chewies;
ChewieController chewieAudioController;
ApiService apiService; ApiService apiService;
final GlobalKey commentKey = GlobalKey(); final GlobalKey commentKey = GlobalKey();
final GlobalKey videoKey = GlobalKey();
final ScrollController scrollController = ScrollController(); final ScrollController scrollController = ScrollController();
bool isKeyBoardShow = false; bool isKeyBoardShow = false;
var commentFocus = FocusNode(); var commentFocus = FocusNode();
@ -55,6 +43,7 @@ class _ClassDetails extends State<ClassDetails> with WidgetsBindingObserver {
List<Chapter> chapterList = []; List<Chapter> chapterList = [];
bool isShowImg = true; bool isShowImg = true;
int chapterIndex = 0; int chapterIndex = 0;
double height = 0;
@override @override
void initState() { void initState() {
@ -81,7 +70,9 @@ class _ClassDetails extends State<ClassDetails> with WidgetsBindingObserver {
chapterList.clear(); chapterList.clear();
chapterList.addAll(baseData.data); chapterList.addAll(baseData.data);
chapterIndex = 0; chapterIndex = 0;
initVideo(chapterList[chapterIndex].content.fileUrl); ClassDetailsVideoState state = videoKey.currentState;
state.initVideo(chapterList[chapterIndex].content.fileUrl);
// initVideo(chapterList[chapterIndex].content.fileUrl);
}); });
} }
EasyLoading.dismiss(); EasyLoading.dismiss();
@ -106,24 +97,6 @@ class _ClassDetails extends State<ClassDetails> with WidgetsBindingObserver {
} }
} }
@override
void dispose() {
/**
*
*/
if (chewieAudioController != null) {
chewieAudioController.pause();
chewieAudioController.dispose();
chewieAudioController = null;
}
if (videoPlayerController != null) {
videoPlayerController.pause();
videoPlayerController.dispose();
}
super.dispose();
}
@override @override
void didChangeMetrics() { void didChangeMetrics() {
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
@ -160,16 +133,22 @@ class _ClassDetails extends State<ClassDetails> with WidgetsBindingObserver {
children: [ children: [
Stack( Stack(
children: [ children: [
videoWidget( ClassDetailsVideo(key:videoKey,exitFull: (){setState(() {});},
MediaQuery.of(context).size.width, coverImg: chapterList.length > chapterIndex
videoPlayerController != null
? (MediaQuery.of(context).size.width) /
videoPlayerController.value.aspectRatio
: MediaQuery.of(context).size.width / 2,
chapterList.length > chapterIndex
? chapterList[chapterIndex].content.coverImg ? 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( Container(
margin: EdgeInsets.only( margin: EdgeInsets.only(
top: 40.h, left: 16.w, right: 16.w), top: 40.h, left: 16.w, right: 16.w),
@ -374,115 +353,9 @@ class _ClassDetails extends State<ClassDetails> 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<void> _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() { Widget anthology() {
return Container( return Container(
color: Colors.white, color: Colors.white,
height: 148.h,
margin: EdgeInsets.only(bottom: 16), margin: EdgeInsets.only(bottom: 16),
padding: EdgeInsets.all(16), padding: EdgeInsets.all(16),
child: Column( child: Column(
@ -522,9 +395,9 @@ class _ClassDetails extends State<ClassDetails> with WidgetsBindingObserver {
setState(() { setState(() {
chapterIndex = position; chapterIndex = position;
isShowImg = true; isShowImg = true;
if (chewieAudioController != null) ClassDetailsVideoState state = videoKey.currentState;
chewieAudioController.pause(); state.initVideo(chapterList[position].content.fileUrl);
initVideo(chapterList[position].content.fileUrl); // initVideo(chapterList[position].content.fileUrl);
}); });
}, },
child: classSelectItem(chapterList[position], position), child: classSelectItem(chapterList[position], position),

180
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<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,
]);
}
@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<void> _fullScreenListener() async {
if (!chewieAudioController.isFullScreen) {
Future.delayed(Duration(seconds: 1), () {
widget.exitFull();
});
}
}
}

6
lib/view_widget/hot_item.dart

@ -177,14 +177,12 @@ class _HotArticleItem extends State<HotArticleItem> {
alignment: Alignment.center, alignment: Alignment.center,
children: [ children: [
Image.network( Image.network(
widget.article != null ? widget.article.coverImg : "", widget.article?.coverImg ?? "",
fit: BoxFit.fill, fit: BoxFit.fill,
height: double.infinity, height: double.infinity,
), ),
Visibility( Visibility(
visible: (widget.article != null && visible: ((widget.article?.coverImg??"").endsWith(".mp4")),
widget.article.coverImg != "" &&
widget.article.coverImg.endsWith(".mp4")),
child: Icon( child: Icon(
Icons.play_circle_outline, Icons.play_circle_outline,
size: 24, size: 24,

Loading…
Cancel
Save