YSBannerView.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_swiper/flutter_swiper.dart';
  3. import 'package:video_player/video_player.dart';
  4. import 'package:ysairplane2/tools/YSTools.dart';
  5. class YSBannerView extends StatefulWidget {
  6. final List dataArray;
  7. const YSBannerView({Key key, @required this.dataArray}) : super(key: key);
  8. @override
  9. _YSBannerViewState createState() => _YSBannerViewState();
  10. }
  11. class _YSBannerViewState extends State<YSBannerView> {
  12. @override
  13. Widget build(BuildContext context) {
  14. return Container(
  15. height: MediaQuery.of(context).padding.top+hsp(380),
  16. width: MediaQuery.of(context).size.width,
  17. color: Colors.white,
  18. child: Swiper(
  19. itemBuilder: (BuildContext context, int index) {
  20. Map item = widget.dataArray[index];
  21. return item['type']==2?Image.network(
  22. item['url'],
  23. fit: BoxFit.fill,
  24. ):YSVideoView(videoStr: item['url'],);
  25. },
  26. itemCount: widget.dataArray.length,
  27. scrollDirection: Axis.horizontal,
  28. loop: true,
  29. pagination: SwiperPagination(
  30. margin: EdgeInsets.only(bottom: hsp(100)),
  31. alignment: Alignment.bottomCenter,
  32. builder: DotSwiperPaginationBuilder(
  33. color: Colors.white,
  34. activeColor: Colors.white,
  35. size: 5.0,
  36. activeSize: 10.0
  37. ),
  38. ),
  39. ),
  40. );
  41. }
  42. }
  43. final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
  44. class YSVideoView extends StatefulWidget {
  45. final String videoStr;
  46. const YSVideoView({Key key, this.videoStr}) : super(key: key);
  47. @override
  48. _YSVideoViewState createState() => _YSVideoViewState();
  49. }
  50. class _YSVideoViewState extends State<YSVideoView> with RouteAware{
  51. VideoPlayerController _controller;
  52. bool _isPlaying = false;
  53. @override
  54. void didChangeDependencies() {
  55. routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute);
  56. super.didChangeDependencies();
  57. }
  58. @override
  59. void dispose() {
  60. if(_controller!=null)_controller.dispose();
  61. routeObserver.unsubscribe(this);
  62. super.dispose();
  63. }
  64. @override
  65. void didPushNext() {
  66. _controller.pause();
  67. super.didPushNext();
  68. }
  69. @override
  70. void didPopNext() {
  71. _controller.play();
  72. super.didPopNext();
  73. }
  74. @override
  75. void initState() {
  76. _controller = VideoPlayerController.network(widget.videoStr)..addListener(() {
  77. final bool isPlaying = _controller.value.isPlaying;
  78. if (isPlaying != _isPlaying) {
  79. setState(() { _isPlaying = isPlaying; });
  80. }
  81. })..initialize().then((_) {
  82. _controller.setLooping(true);
  83. _controller.play();
  84. setState(() {});
  85. });
  86. super.initState();
  87. }
  88. @override
  89. Widget build(BuildContext context) {
  90. return Stack(
  91. alignment: Alignment.bottomCenter,
  92. children: <Widget>[
  93. VideoPlayer(_controller),
  94. _PlayPauseOverlay(controller: _controller),
  95. Container(
  96. height:6.5,
  97. margin: EdgeInsets.only(bottom: hsp(80)),
  98. child: VideoProgressIndicator(
  99. _controller,
  100. allowScrubbing: true,
  101. colors: VideoProgressColors(playedColor: Colors.black,backgroundColor: Colors.white),
  102. )
  103. ),
  104. ],
  105. );
  106. }
  107. }
  108. class _PlayPauseOverlay extends StatelessWidget {
  109. const _PlayPauseOverlay({Key key, this.controller}) : super(key: key);
  110. final VideoPlayerController controller;
  111. @override
  112. Widget build(BuildContext context) {
  113. return Stack(
  114. children: <Widget>[
  115. AnimatedSwitcher(
  116. duration: Duration(milliseconds: 50),
  117. reverseDuration: Duration(milliseconds: 200),
  118. child: controller.value.isPlaying? SizedBox.shrink():Container(
  119. color: Colors.black26,
  120. child: Center(
  121. child: Icon(
  122. Icons.play_circle_filled,
  123. color: Colors.white,
  124. size: 50,
  125. ),
  126. ),
  127. ),
  128. ),
  129. GestureDetector(
  130. onTap: () {
  131. controller.value.isPlaying ? controller.pause() : controller.play();
  132. },
  133. ),
  134. ],
  135. );
  136. }
  137. }