import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

// class VideoPlayer1 extends StatefulWidget {
//   VideoPlayer1({
//     Key? key,
//     required this.url,
//     this.onLoaded,
//   }) : super(key: key);

//   final String url;

//   final VoidCallback? onLoaded;

//   @override
//   _PlayerState createState() => _PlayerState();
// }

// class _PlayerState extends State<VideoPlayer1> {
//   late FlickManager flickManager;

//   bool isLoaded = false;

//   @override
//   void initState() {
//     super.initState();
//     flickManager = FlickManager(
//       videoPlayerController: VideoPlayerController.networkUrl(
//         Uri.parse(widget.url),
//         videoPlayerOptions: VideoPlayerOptions(),
//       ),
//     );
//     flickManager.flickVideoManager!.addListener(() {
//       if (!isLoaded && flickManager.flickVideoManager!.isPlaying) {
//         widget.onLoaded?.call();
//         isLoaded = true;
//       }
//     });
//   }

//   @override
//   void dispose() {
//     flickManager.dispose();
//     super.dispose();
//   }

//   @override
//   Widget build(BuildContext context) {
//     return VisibilityDetector(
//       key: ObjectKey(flickManager),
//       onVisibilityChanged: (visibility) {
//         if (visibility.visibleFraction == 0 && mounted) {
//           flickManager.flickControlManager?.autoPause();
//         } else if (visibility.visibleFraction == 1) {
//           flickManager.flickControlManager?.autoResume();
//         }
//       },
//       child: FlickVideoPlayer(
//         flickManager: flickManager,
//         flickVideoWithControls: const FlickVideoWithControls(
//           videoFit: BoxFit.contain,
//           closedCaptionTextStyle: TextStyle(fontSize: 8),
//           controls: FlickPortraitControls(),
//         ),
//         flickVideoWithControlsFullscreen: const FlickVideoWithControls(
//           videoFit: BoxFit.contain,
//           controls: FlickLandscapeControls(),
//         ),
//       ),
//     );
//   }
// }

class VideoPlayer1 extends StatefulWidget {
  const VideoPlayer1({
    Key? key,
    required this.url,
    this.onLoaded,
  }) : super(key: key);

  final String url;

  final VoidCallback? onLoaded;

  @override
  _VideoPlayer1State createState() => _VideoPlayer1State();
}

class _VideoPlayer1State extends State<VideoPlayer1> {
  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.networkUrl(Uri.parse(widget.url))
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Center(
          child: _controller.value.isInitialized
              ? AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                )
              : Container(),
        ),
        Positioned(
          top: 0,
          bottom: 0,
          left: 0,
          right: 0,
          child: IconButton(
            onPressed: () {
              setState(() {
                _controller.value.isPlaying
                    ? _controller.pause()
                    : _controller.play();
              });
            },
            icon: Icon(
              _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
              color: Colors.white,
            ),
          ),
        )
      ],
    );
    // floatingActionButton: FloatingActionButton(
    //   onPressed: () {
    //     setState(() {
    //       _controller.value.isPlaying
    //           ? _controller.pause()
    //           : _controller.play();
    //     });
    //   },
    // child: Icon(
    //   _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
    // ),
    // ),
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
