Show menu

main.dart

  void main() => runApp(
    ChangeNotifierProvider<VolumeManager>(
      create: (_) => VolumeManager(),
      child: DemoApp(),
    )
  );

  class DemoApp extends StatelessWidget {
    const DemoApp();

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("Play a video"),
          ),
          body: const Center(
            child: VideoWidget(),
          )
        ),
      );
    }
  }
  

volume_manager.dart

  class VolumeManager with ChangeNotifier {
    double _volume = 50.0;
    double get volume => _volume;

    void setVolume({
      required double volumeValue, 
      required VideoPlayerController controller
    }) {
      _volume = volumeValue;
      controller.setVolume(_volume);

      notifyListeners();
    }
  }
  

video_widget.dart

  class VideoWidget extends StatefulWidget {
    const VideoWidget();

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

  class _VideoWidgetState extends State<VideoWidget> {
    late final VideoPlayerController controller;
    late final Future<void> initVideo;

    @override
    void initState() {
      super.initState();

      controller = VideoPlayerController.asset(
        "assets/butterfly.mp4"
      );

      controller.setLooping(true);
      initVideo = controller.initialize();
    }

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

    @override
    Widget build(BuildContext context) {
      return FutureBuilder(
        future: initVideo,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return PlayWidget(controller);
          }

          return const Center(
            child: CircularProgressIndicator(),
          );
        },
      );
    }
  }
  

video_play_widget.dart

  class PlayWidget extends StatelessWidget {
    final VideoPlayerController controller;
    const PlayWidget(this.controller);

    @override
    Widget build(BuildContext context) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          AspectRatio(
            aspectRatio: controller.value.aspectRatio,
            child: VideoPlayer(controller),
          ),

          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              RaisedButton(
                child: const Text("Play!"),
                onPressed: _play,
              ),

              RaisedButton(
                child: const Text("Pause"),
                onPressed: _pause,
              )
            ],
          ),

          Consumer<VolumeManager>(
            builder: (context, manager, _) {
              return Slider(
                value: manager.volume,
                onChanged: (value) {
                  manager.setVolume(
                    volumeValue: value,
                    controller: controller,
                  );
                },
                min: 0,
                max: 1,
                divisions: 10,
                label: "${manager.volume * 100}%",
              );
            },
          )
        ]
      );
    }

    void _play() {
      if (!controller.value.isPlaying)
        controller.play();
    }

    void _pause() {
      if (controller.value.isPlaying)
        controller.pause();
    }
  }
  

error_widget.dart

  class ErrorWidget extends StatelessWidget {
    const ErrorWidget();

    @override
    Widget build(BuildContext context) {
      return const Icon(
        Icons.error,
        size: 25,
        color: Colors.red,
      );
    }

  }
  

This website and the book are not official Google products. No affiliations are involved. Built with Java 14 and Vert.X

"Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC"