This is part of an article I originally wrote in Medium, but it's also available in DEV.to, check it out!
Last active
June 15, 2024 00:55
-
-
Save Miqueas/c1c268e7f2da5d94fc3b81914ce2cb4b to your computer and use it in GitHub Desktop.
OverflowBox and videos in Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:video_player/video_player.dart'; | |
void main() => runApp(const VideoPlayerApp()); | |
class VideoPlayerApp extends StatelessWidget { | |
const VideoPlayerApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
title: 'Video Player Demo', | |
home: VideoPlayerScreen(), | |
); | |
} | |
} | |
class VideoPlayerScreen extends StatefulWidget { | |
const VideoPlayerScreen({super.key}); | |
@override | |
State<VideoPlayerScreen> createState() => _VideoPlayerScreenState(); | |
} | |
class _VideoPlayerScreenState extends State<VideoPlayerScreen> { | |
late VideoPlayerController _controller; | |
late Future<void> _initializeVideoPlayerFuture; | |
@override | |
void initState() { | |
super.initState(); | |
// Create and store the VideoPlayerController. The VideoPlayerController | |
// offers several different constructors to play videos from assets, files, | |
// or the internet. | |
_controller = VideoPlayerController.networkUrl( | |
Uri.parse( | |
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4', | |
), | |
); | |
// Initialize the controller and store the Future for later use. | |
_initializeVideoPlayerFuture = _controller.initialize(); | |
// Use the controller to loop the video. | |
_controller.setLooping(true); | |
} | |
@override | |
void dispose() { | |
// Ensure disposing of the VideoPlayerController to free up resources. | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Butterfly Video'), | |
), | |
// Use a FutureBuilder to display a loading spinner while waiting for the | |
// VideoPlayerController to finish initializing. | |
body: FutureBuilder( | |
future: _initializeVideoPlayerFuture, | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.done) { | |
// If the VideoPlayerController has finished initialization, use | |
// the data it provides to limit the aspect ratio of the video. | |
return Center( | |
child: Container( | |
width: 200, | |
height: 200, | |
clipBehavior: Clip.antiAlias, | |
decoration: BoxDecoration( | |
color: Colors.amber, | |
borderRadius: BorderRadius.circular(20),), | |
child: OverflowBox( | |
maxWidth: double.infinity, | |
maxHeight: 200, | |
child: AspectRatio( | |
aspectRatio: _controller.value.aspectRatio, | |
// Use the VideoPlayer widget to display the video. | |
child: VideoPlayer(_controller), | |
), | |
), | |
), | |
); | |
} else { | |
// If the VideoPlayerController is still initializing, show a | |
// loading spinner. | |
return const Center( | |
child: CircularProgressIndicator(), | |
); | |
} | |
}, | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
// Wrap the play or pause in a call to `setState`. This ensures the | |
// correct icon is shown. | |
setState(() { | |
// If the video is playing, pause it. | |
if (_controller.value.isPlaying) { | |
_controller.pause(); | |
} else { | |
// If the video is paused, play it. | |
_controller.play(); | |
} | |
}); | |
}, | |
// Display the correct icon depending on the state of the player. | |
child: Icon( | |
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment