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.
198 lines
5.9 KiB
198 lines
5.9 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'; |
|
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<StatefulWidget> createState() { |
|
return ClassDetailsVideoState(); |
|
} |
|
} |
|
|
|
class ClassDetailsVideoState extends State<ClassDetailsVideo> { |
|
late VideoPlayerController videoPlayerController; |
|
late ChewieController chewieAudioController; |
|
late Chewie chewies; |
|
GlobalKey globalKey = GlobalKey(); |
|
double height = 0; |
|
|
|
@override |
|
void initState() { |
|
super.initState(); |
|
SystemChrome.setPreferredOrientations([ |
|
DeviceOrientation.portraitUp, |
|
]); |
|
initVideo(widget.videoUrl); |
|
} |
|
|
|
@override |
|
void didChangeDependencies() { |
|
WidgetsBinding.instance.addPostFrameCallback(_getContainerHeight); |
|
super.didChangeDependencies(); |
|
} |
|
|
|
_getContainerHeight(_) { |
|
if (globalKey.currentContext != null) |
|
height = globalKey.currentContext!.size?.height ?? 0; |
|
widget.heightFun?.call(height); |
|
print("height: $height"); |
|
} |
|
|
|
initVideo(videoUrl) async { |
|
videoPlayerController.pause(); |
|
videoPlayerController.dispose(); |
|
videoPlayerController = VideoPlayerController.networkUrl( |
|
Uri.parse(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(); |
|
|
|
chewieAudioController.pause(); |
|
chewieAudioController.dispose(); |
|
|
|
videoPlayerController.pause(); |
|
videoPlayerController.dispose(); |
|
} |
|
|
|
Widget videoWidget(double width, double height, src) { |
|
print("src : $src"); |
|
return MediaQuery( |
|
data: MediaQuery.of(context).copyWith( |
|
textScaler: TextScaler.linear(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 ?? false) |
|
GestureDetector( |
|
onTap: () { |
|
setState(() { |
|
widget.changeShowImg?.call(false); |
|
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<void> _fullScreenListener() async { |
|
if (!chewieAudioController.isFullScreen) { |
|
Future.delayed(Duration(seconds: 1), () { |
|
widget.exitFull?.call(); |
|
}); |
|
} |
|
} |
|
}
|
|
|