VideoWidget.dart 4.2 KB

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