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

class VideoScreenThumbnail extends StatefulWidget {
  final dynamic video;
  const VideoScreenThumbnail({Key? key, required this.video}) : super(key: key);

  @override
  State<VideoScreenThumbnail> createState() => _VideoScreenThumbnailState();
}

class _VideoScreenThumbnailState extends State<VideoScreenThumbnail> {
  late VideoPlayerController _controllerVideo;
  late bool startedPlaying;

  @override
  void initState() {
    _controllerVideo = VideoPlayerController.network(widget.video);
    _controllerVideo.initialize();
    super.initState();
    started();
    _controllerVideo.setVolume(0);
    _controllerVideo.pause();
  }

  Future<bool> started() async {
    var renderized = false;
    double height = 0;

    await Future.delayed(const Duration(seconds: 2), () {
      height = _controllerVideo.value.size.height;
      if (height > 0) renderized = true;
    });

    return renderized;
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: started(),
      builder: ((context, AsyncSnapshot<bool> snapshot) {
        if (snapshot.data ?? false) {
          return Stack(
            alignment: AlignmentDirectional.center,
            children: [
              VideoPlayer(_controllerVideo),
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: const [
                  SizedBox(height: 30),
                ],
              ),
            ],
          );
        } else {
          return const Center(child: CircularProgressIndicator());
        }
      }),
    );
  }

  @override
  void dispose() {
    super.dispose();
    var video = _controllerVideo;
    video.dispose();
  }
}
