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 = 'http://oss.izouma.com/shouyoudianjing/video/2019-03-01-11-19-41-ivqsgiy9.mp4';
  12. bool isVideo = true;
  13. bool showVideo = false;
  14. bool isPlay = false;
  15. @override
  16. void initState() {
  17. super.initState();
  18. if (widget.videoSrc == null) {
  19. } else {
  20. if (widget.videoSrc.contains('.mp4')) {
  21. _defaultVideo = widget.videoSrc;
  22. isVideo = true;
  23. } else {
  24. isVideo = false;
  25. }
  26. }
  27. if (isVideo) {
  28. _controller = VideoPlayerController.network(_defaultVideo)
  29. ..initialize().then((_) {
  30. // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
  31. setState(() {});
  32. });
  33. _controller.setLooping(true);
  34. _controller.addListener(() {
  35. setState(() {
  36. isPlay = _controller.value.isPlaying;
  37. });
  38. // if(_controller.value.isLooping)
  39. });
  40. }
  41. }
  42. @override
  43. void dispose() {
  44. super.dispose();
  45. _controller.dispose();
  46. }
  47. Future<void> playVideo() async {
  48. if (!showVideo) {
  49. setState(() {
  50. showVideo = true;
  51. });
  52. await Future.delayed(Duration(milliseconds: 500));
  53. }
  54. _controller.value.isPlaying ? _controller.pause() : _controller.play();
  55. }
  56. @override
  57. Widget build(BuildContext context) {
  58. return Container(
  59. child: Column(
  60. children: <Widget>[
  61. isVideo
  62. ? _playContainer()
  63. : Image.network(
  64. widget.videoSrc,
  65. width: double.infinity,
  66. height: 210,
  67. fit: BoxFit.cover,
  68. )
  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. fit: BoxFit.cover,
  91. ),
  92. ),
  93. onTap: () => playVideo()),
  94. !isPlay
  95. ? Positioned(
  96. left: 0,
  97. right: 0,
  98. top: 83,
  99. child: Center(
  100. child: UnconstrainedBox(
  101. child: Container(
  102. width: 44,
  103. height: 44,
  104. child: RaisedButton(
  105. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(44)),
  106. padding: EdgeInsets.all(0),
  107. color: Colors.black87,
  108. textColor: Colors.white,
  109. onPressed: () => playVideo(),
  110. child: 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. }