VideoWidget.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'package:video_player/video_player.dart';
  2. import 'package:flutter/material.dart';
  3. class VideoWidget extends StatefulWidget {
  4. VideoWidget({Key key, this.videoSrc}) : super(key: key);
  5. final String videoSrc; // 用来储存传递过来的值
  6. @override
  7. VideoWidgetState createState() => VideoWidgetState();
  8. }
  9. class VideoWidgetState extends State<VideoWidget> {
  10. VideoPlayerController _controller;
  11. String _defaultVideo =
  12. 'http://oss.izouma.com/shouyoudianjing/video/2019-03-01-11-19-41-ivqsgiy9.mp4';
  13. bool isVideo = true;
  14. bool showVideo = false;
  15. bool isPlay = false;
  16. @override
  17. void initState() {
  18. super.initState();
  19. if (widget.videoSrc == null) {
  20. } else {
  21. if (widget.videoSrc.contains('.mp4')) {
  22. _defaultVideo = widget.videoSrc;
  23. isVideo = true;
  24. } else {
  25. isVideo = false;
  26. }
  27. }
  28. print('**********');
  29. print(isVideo);
  30. print(_defaultVideo);
  31. print(widget.videoSrc);
  32. if (isVideo) {
  33. _controller = VideoPlayerController.network(_defaultVideo)
  34. ..initialize().then((_) {
  35. // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
  36. setState(() {});
  37. });
  38. _controller.setLooping(true);
  39. _controller.addListener(() {
  40. setState(() {
  41. isPlay = _controller.value.isPlaying;
  42. });
  43. // if(_controller.value.isLooping)
  44. });
  45. }
  46. }
  47. @override
  48. void dispose() {
  49. super.dispose();
  50. _controller.dispose();
  51. }
  52. void playVideo() async {
  53. if (!showVideo) {
  54. setState(() {
  55. showVideo = true;
  56. });
  57. await Future.delayed(Duration(milliseconds: 500));
  58. }
  59. _controller.value.isPlaying ? _controller.pause() : _controller.play();
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return Container(
  64. child: Column(
  65. children: <Widget>[
  66. isVideo
  67. ? _playContainer()
  68. : Image.network(widget.videoSrc, width: double.infinity,height: 210,fit: BoxFit.cover,)
  69. ],
  70. ));
  71. }
  72. Widget _playContainer() {
  73. return Container(
  74. color: Colors.black,
  75. height: 210,
  76. width: double.infinity,
  77. child: Center(
  78. child: Stack(
  79. children: <Widget>[
  80. InkWell(
  81. child: Center(
  82. child: showVideo
  83. ? AspectRatio(
  84. aspectRatio: _controller.value.aspectRatio,
  85. child: VideoPlayer(_controller),
  86. )
  87. : Image.network(
  88. 'http://images.liqucn.com/img/h22/h70/img_localize_8e824debdd9ee29522690f36680e2d8e_600x337.png',
  89. width: double.infinity),
  90. ),
  91. onTap: () => playVideo()),
  92. !isPlay
  93. ? Positioned(
  94. left: 0,
  95. right: 0,
  96. top:83,
  97. child: Center(
  98. child: UnconstrainedBox(
  99. child: Container(
  100. width: 44,
  101. height: 44,
  102. child: RaisedButton(
  103. shape: RoundedRectangleBorder(
  104. borderRadius: BorderRadius.circular(44)),
  105. padding: EdgeInsets.all(0),
  106. color: Colors.black87,
  107. textColor: Colors.white,
  108. onPressed: () => playVideo(),
  109. child:
  110. Icon(isPlay ? Icons.pause : Icons.play_arrow),
  111. ),
  112. ),
  113. ),
  114. ))
  115. : Container(),
  116. ],
  117. ),
  118. ),
  119. );
  120. // return InkWell(
  121. // child: AspectRatio(
  122. // aspectRatio: _controller.value.aspectRatio,
  123. // child: VideoPlayer(_controller),
  124. // ),
  125. // onTap: () {
  126. // _controller.value.isPlaying ? _controller.pause() : _controller.play();
  127. // },
  128. // );
  129. }
  130. }