Untitled

 avatar
unknown
dart
3 years ago
4.4 kB
2
Indexable

class _VideoWidgetState extends State<VideoWidget> {
  double aRatio = 0.5;
  ChewieController chewieController;

  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;

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

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(widget.url);
    _initializeVideoPlayerFuture = _controller.initialize().then((_) {
      // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
      setState(
        () {
          aRatio = _controller.value.aspectRatio <
                  ((MediaQuery.of(context).size.width /
                          MediaQuery.of(context).size.height) +
                      0.4)
              ? _controller.value.aspectRatio / 0.7
              : _controller.value.aspectRatio;
          chewieController = ChewieController(
            cupertinoProgressColors: ChewieProgressColors(
              playedColor: Colors.transparent,
              bufferedColor: Colors.transparent,
              handleColor: Colors.transparent,
            ),
            materialProgressColors: ChewieProgressColors(
              playedColor: Colors.transparent,
              bufferedColor: Colors.transparent,
              handleColor: Colors.transparent,
            ),
            videoPlayerController: _controller,
            autoPlay: false,
            looping: false,
            autoInitialize: true,
            allowMuting: true,
            deviceOrientationsOnEnterFullScreen: [
              DeviceOrientation.portraitUp,
              DeviceOrientation.landscapeLeft,
              DeviceOrientation.landscapeRight,
            ],
            deviceOrientationsAfterFullScreen: [DeviceOrientation.portraitUp],
            aspectRatio: aRatio,
          );
          _controller.pause();
          _controller.setLooping(false);
        },
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return VisibilityDetector(
      key: ObjectKey(_controller),
      onVisibilityChanged: (visibility) {
        if (visibility.visibleFraction == 0 &&
            this.mounted &&
            !chewieController.isFullScreen) {
          _controller.pause();
        } else if (chewieController.isFullScreen) {
          _controller.play();
        } else {
          _controller.pause();
        }
      },
      child: FutureBuilder(
        future: _initializeVideoPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return _controller.value.isInitialized
                ? AspectRatio(
                    aspectRatio: aRatio,
                    child: Stack(
                      children: [
                        VolumeWatcher(
                          onVolumeChangeListener: (double volume) {
                            chewieController.setVolume(volume);
                          },
                          child: Chewie(
                            controller: chewieController,
                          ),
                        ),
                        Positioned.fill(
                          child: GestureDetector(
                            child: Container(
                              color: Colors.transparent,
                            ).marginSymmetric(vertical: 40),
                            onTap: () {
                              if (_controller.value.isPlaying) {
                                _controller.pause();
                              } else {
                                _controller.play();
                              }
                            },
                          ),
                        ),
                      ],
                    ),
                  )
                : Container();
          } else {
            return Container(
              width: double.infinity,
              height: 400,
              color: Colors.black,
              child: Center(
                child: CircularProgressIndicator(),
              ),
              // child: FancyShimmerImage(
              //     imageUrl: 'https://i.imgur.com/j57mwe4.jpg'),
            );
          }
        },
      ),
    );
  }
}