123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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<YSBannerView> {
- @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<PageRoute> routeObserver = RouteObserver<PageRoute>();
- class YSVideoView extends StatefulWidget {
- final String videoStr;
- const YSVideoView({Key key, this.videoStr}) : super(key: key);
- @override
- _YSVideoViewState createState() => _YSVideoViewState();
- }
- class _YSVideoViewState extends State<YSVideoView> 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: <Widget>[
- 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: <Widget>[
- 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();
- },
- ),
- ],
- );
- }
- }
|