VideoWidget.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Future<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(
  69. widget.videoSrc,
  70. width: double.infinity,
  71. height: 210,
  72. fit: BoxFit.cover,
  73. )
  74. ],
  75. ));
  76. }
  77. Widget _playContainer() {
  78. return Container(
  79. color: Colors.black,
  80. height: 210,
  81. width: double.infinity,
  82. child: Center(
  83. child: Stack(
  84. children: <Widget>[
  85. InkWell(
  86. child: Center(
  87. child: showVideo
  88. ? AspectRatio(
  89. aspectRatio: _controller.value.aspectRatio,
  90. child: VideoPlayer(_controller),
  91. )
  92. : Image.network(
  93. 'http://images.liqucn.com/img/h22/h70/img_localize_8e824debdd9ee29522690f36680e2d8e_600x337.png',
  94. width: double.infinity),
  95. ),
  96. onTap: () => playVideo()),
  97. !isPlay
  98. ? Positioned(
  99. left: 0,
  100. right: 0,
  101. top: 83,
  102. child: Center(
  103. child: UnconstrainedBox(
  104. child: Container(
  105. width: 44,
  106. height: 44,
  107. child: RaisedButton(
  108. shape: RoundedRectangleBorder(
  109. borderRadius: BorderRadius.circular(44)),
  110. padding: EdgeInsets.all(0),
  111. color: Colors.black87,
  112. textColor: Colors.white,
  113. onPressed: () => playVideo(),
  114. child:
  115. Icon(isPlay ? Icons.pause : Icons.play_arrow),
  116. ),
  117. ),
  118. ),
  119. ))
  120. : Container(),
  121. ],
  122. ),
  123. ),
  124. );
  125. // return InkWell(
  126. // child: AspectRatio(
  127. // aspectRatio: _controller.value.aspectRatio,
  128. // child: VideoPlayer(_controller),
  129. // ),
  130. // onTap: () {
  131. // _controller.value.isPlaying ? _controller.pause() : _controller.play();
  132. // },
  133. // );
  134. }
  135. }