VideoWidget.dart 4.3 KB

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