3

I was trying to create a videoplayer in a stateless widget in flutter. I could not do it.... this is the code I used ...Is it possible to create a video player in a stateless widget?

class VideoWidget extends StatelessWidget {
  VideoWidget({Key? key}) : super(key: key);
  final videoController =
      VideoPlayerController.network('https://youtu.be/_EoLNs5m-7Y?t=4')
        ..initialize();
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width,
      //height: 500,
      child: Column(
        children: [
          //VideoPlayer(videoController!),
          ValueListenableBuilder(
              valueListenable: videoPlayerNotifier,
              builder: (BuildContext cxt, play, widget_) {
                return videoController.value.isInitialized
                    ? AspectRatio(
                        aspectRatio: videoController.value.aspectRatio,
                        child: VideoPlayer(videoController))
                    : Container();
              }),

          const Text(
            'movieName',
             style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
          const Text('Description'),
          FloatingActionButton(onPressed: () {
            videoPlayerNotifier.value = true;
          })
        ],
      ),
    );
  }
}
Arun SS
  • 33
  • 4
  • Hi, You can definitely before I answer, are you using https://pub.dev/packages/video_player package? and you want to load Youtube Video? – Majid Mar 08 '22 at 16:31
  • Yes I have used video_player: ^2.2.18 , and I was trying to load Youtube Video..... – Arun SS Mar 18 '22 at 14:29

1 Answers1

0

VideoPlayer from the video_player pub package is quite stateful, due especially to VideoPlayerController. However, you can wrap it in a StatefulWidget to be used within a StatelessWidget. E.g. use this wrapper within a stateless widget:

class VideoPlayerWrapper extends StatefulWidget {
  final String videoUri;

  const VideoPlayerWrapper({Key? key, required this.videoUri}) : super(key: key);

  @override
  _VideoPlayerWrapperState createState() => _VideoPlayerWrapperState(videoUri);
}

class _VideoPlayerWrapperState extends State<VideoPlayerWrapper> {
  late VideoPlayerController _controller;
  final String videoUri;


  _VideoPlayerWrapperState(this.videoUri);

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

  @override
  Widget build(BuildContext context) {
    return Center(
          child: _controller.value.isInitialized
              ? AspectRatio(
            aspectRatio: _controller.value.aspectRatio,
            child: VideoPlayer(_controller),
          )
              : Container(),
    );
  }

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

voxoid
  • 1,164
  • 1
  • 14
  • 31