import 'package:flutter/material.dart'; import 'package:flutter_swiper/flutter_swiper.dart'; import 'package:video_player/video_player.dart'; import 'package:ysairplane2/tools/YSTools.dart'; class YSBannerView extends StatefulWidget { final List dataArray; const YSBannerView({Key key, @required this.dataArray}) : super(key: key); @override _YSBannerViewState createState() => _YSBannerViewState(); } class _YSBannerViewState extends State { @override Widget build(BuildContext context) { return Container( height: MediaQuery.of(context).padding.top+hsp(380), width: MediaQuery.of(context).size.width, color: Colors.white, child: Swiper( itemBuilder: (BuildContext context, int index) { Map item = widget.dataArray[index]; return item['type']==2?Image.network( item['url'], fit: BoxFit.fill, ):YSVideoView(videoStr: item['url'],); }, itemCount: widget.dataArray.length, scrollDirection: Axis.horizontal, loop: true, pagination: SwiperPagination( margin: EdgeInsets.only(bottom: hsp(100)), alignment: Alignment.bottomCenter, builder: DotSwiperPaginationBuilder( color: Colors.white, activeColor: Colors.white, size: 5.0, activeSize: 10.0 ), ), ), ); } } final RouteObserver routeObserver = RouteObserver(); class YSVideoView extends StatefulWidget { final String videoStr; const YSVideoView({Key key, this.videoStr}) : super(key: key); @override _YSVideoViewState createState() => _YSVideoViewState(); } class _YSVideoViewState extends State with RouteAware{ VideoPlayerController _controller; bool _isPlaying = false; @override void didChangeDependencies() { routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute); super.didChangeDependencies(); } @override void dispose() { if(_controller!=null)_controller.dispose(); routeObserver.unsubscribe(this); super.dispose(); } @override void didPushNext() { _controller.pause(); super.didPushNext(); } @override void didPopNext() { _controller.play(); super.didPopNext(); } @override void initState() { _controller = VideoPlayerController.network(widget.videoStr)..addListener(() { final bool isPlaying = _controller.value.isPlaying; if (isPlaying != _isPlaying) { setState(() { _isPlaying = isPlaying; }); } })..initialize().then((_) { _controller.setLooping(true); _controller.play(); setState(() {}); }); super.initState(); } @override Widget build(BuildContext context) { return Stack( alignment: Alignment.bottomCenter, children: [ VideoPlayer(_controller), _PlayPauseOverlay(controller: _controller), Container( height:6.5, margin: EdgeInsets.only(bottom: hsp(80)), child: VideoProgressIndicator( _controller, allowScrubbing: true, colors: VideoProgressColors(playedColor: Colors.black,backgroundColor: Colors.white), ) ), ], ); } } class _PlayPauseOverlay extends StatelessWidget { const _PlayPauseOverlay({Key key, this.controller}) : super(key: key); final VideoPlayerController controller; @override Widget build(BuildContext context) { return Stack( children: [ AnimatedSwitcher( duration: Duration(milliseconds: 50), reverseDuration: Duration(milliseconds: 200), child: controller.value.isPlaying? SizedBox.shrink():Container( color: Colors.black26, child: Center( child: Icon( Icons.play_circle_filled, color: Colors.white, size: 50, ), ), ), ), GestureDetector( onTap: () { controller.value.isPlaying ? controller.pause() : controller.play(); }, ), ], ); } }