import 'package:chewie/chewie.dart'; import 'package:flutter/material.dart'; import 'package:sonnat/core/widgets/global_loading.dart'; import 'package:video_player/video_player.dart'; class VideoPlayer extends StatefulWidget { final String url; const VideoPlayer({super.key, required this.url}); @override State createState() => _VideoPlayerState(); } class _VideoPlayerState extends State { late ChewieController? chewieController; late VideoPlayerController videoPlayerController; @override void initState() { super.initState(); videoPlayerController = VideoPlayerController.network(widget.url); } Future initVideo() async { await videoPlayerController.initialize(); chewieController = ChewieController( videoPlayerController: videoPlayerController, autoPlay: false, looping: false, autoInitialize: true, allowFullScreen: true, ); return Future.value(true); } @override Widget build(BuildContext context) { if (videoPlayerController.value.isInitialized) { return AspectRatio( aspectRatio: chewieController!.aspectRatio ?? 16 / 9, child: Chewie(controller: chewieController!), ); } return FutureBuilder( future: initVideo(), builder: (context, snapshot) { if (snapshot.hasData) { return AspectRatio( aspectRatio: chewieController!.aspectRatio ?? 16 / 9, child: Chewie(controller: chewieController!), ); } if (snapshot.hasError) { return const Icon(Icons.error_outline); } return const GlobalLoading(isSmallSize: true); }); } @override void dispose() { videoPlayerController.dispose(); chewieController?.dispose(); super.dispose(); } }