import 'package:flutter/material.dart'; import 'package:chewie/chewie.dart'; import 'package:video_player/video_player.dart'; import 'dart:io'; class FikoYTVideoPlayer extends StatefulWidget { final String videoFilePath; final bool autoPlay; final bool loop; final bool showOptions; final bool showControls; final bool showControlsOnInitialize; const FikoYTVideoPlayer({ super.key, required this.videoFilePath, this.autoPlay = false, this.loop = false, this.showControls = true, this.showOptions = false, this.showControlsOnInitialize = true, }); @override _FikoYTVideoPlayerState createState() => _FikoYTVideoPlayerState(); } class _FikoYTVideoPlayerState extends State { late VideoPlayerController _videoPlayerController; ChewieController? _chewieController; // Use nullable type for safety bool _controllerInitialized = false; @override void initState() { super.initState(); _initializeControllers(); } void _initializeControllers() async { // Initialize video player controller based on the file path type _videoPlayerController = widget.videoFilePath.startsWith("assets/") ? VideoPlayerController.asset(widget.videoFilePath) : VideoPlayerController.file(File(widget.videoFilePath)); try { await _videoPlayerController.initialize(); setState(() { _controllerInitialized = true; _chewieController = ChewieController( videoPlayerController: _videoPlayerController, aspectRatio: _videoPlayerController.value.aspectRatio, autoInitialize: true, showOptions: widget.showOptions, showControls: widget.showControls, showControlsOnInitialize: widget.showControlsOnInitialize, autoPlay: widget.autoPlay, looping: widget.loop, isLive: false, zoomAndPan: false, allowMuting: true, allowedScreenSleep: false, allowPlaybackSpeedChanging: true, allowFullScreen: true, ); }); } catch (e) { // Handle errors gracefully setState(() { _controllerInitialized = false; }); // debugPrint('Error initializing video controller: $e'); } } @override void dispose() { _chewieController?.dispose(); _videoPlayerController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { if (!_controllerInitialized) { return Center(child: CircularProgressIndicator()); } return AspectRatio( aspectRatio: _videoPlayerController.value.aspectRatio, child: Chewie(controller: _chewieController!), ); } }