VideoWidget.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. import '../net/HttpManager.dart';
  5. class VideoWidget extends StatefulWidget {
  6. VideoWidget({Key key, this.videoSrc}) : super(key: key);
  7. final String videoSrc; // 用来储存传递过来的值
  8. @override
  9. VideoWidgetState createState() => VideoWidgetState();
  10. }
  11. class VideoWidgetState extends State<VideoWidget> {
  12. VideoPlayerController _controller;
  13. String _defaultVideo = HttpManager.baseUrl + 'houseinfo.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. Future.delayed(Duration(seconds: 1), () {
  48. playVideo();
  49. });
  50. }
  51. @override
  52. void dispose() {
  53. super.dispose();
  54. _controller.dispose();
  55. }
  56. Future<void> playVideo() async {
  57. if (!showVideo) {
  58. setState(() {
  59. showVideo = true;
  60. });
  61. await Future.delayed(Duration(milliseconds: 500));
  62. }
  63. _controller.value.isPlaying ? _controller.pause() : _controller.play();
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return Container(
  68. child: Column(
  69. children: <Widget>[
  70. isVideo
  71. ? _playContainer()
  72. : Image.network(
  73. widget.videoSrc,
  74. width: double.infinity,
  75. height: 200,
  76. fit: BoxFit.cover,
  77. )
  78. ],
  79. ));
  80. }
  81. Widget _playContainer() {
  82. return Container(
  83. // color: Colors.black,
  84. // height: 200,
  85. width: double.infinity,
  86. child: Center(
  87. child: Stack(
  88. children: <Widget>[
  89. InkWell(
  90. child: Center(
  91. child: showVideo
  92. ? AspectRatio(
  93. aspectRatio: _controller.value.aspectRatio,
  94. child: VideoPlayer(_controller),
  95. )
  96. : Image.asset('images/fangjian_img_bg.png'),
  97. // Image.network(
  98. // 'http://images.liqucn.com/img/h22/h70/img_localize_8e824debdd9ee29522690f36680e2d8e_600x337.png',
  99. // width: double.infinity),
  100. ),
  101. onTap: () => playVideo()),
  102. !isPlay
  103. ? Positioned(
  104. left: 0,
  105. right: 0,
  106. top: 83,
  107. child: Center(
  108. child: UnconstrainedBox(
  109. child: Container(
  110. width: 44,
  111. height: 44,
  112. child: RaisedButton(
  113. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(44)),
  114. padding: EdgeInsets.all(0),
  115. color: Colors.black87,
  116. textColor: Colors.white,
  117. onPressed: () => playVideo(),
  118. child: Icon(isPlay ? Icons.pause : Icons.play_arrow),
  119. ),
  120. ),
  121. ),
  122. ))
  123. : Container(),
  124. ],
  125. ),
  126. ),
  127. );
  128. // return InkWell(
  129. // child: AspectRatio(
  130. // aspectRatio: _controller.value.aspectRatio,
  131. // child: VideoPlayer(_controller),
  132. // ),
  133. // onTap: () {
  134. // _controller.value.isPlaying ? _controller.pause() : _controller.play();
  135. // },
  136. // );
  137. }
  138. }