w-R
3 years ago
3 changed files with 231 additions and 180 deletions
@ -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(); |
||||
}); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue