Position Text over Video - Flutter-web - flutter

I'm trying to position some text over a video.
The video is currently taking as much space as possible while retaining it's original aspect ratio. Ideally I'd like it to keep doing this as I want the video to resize to fit the browser window.
I'm assuming I need to get get the height/width of the video dynamically..
If anyone knows how to make a video play automatically that would be great as well - I'm using the video_player package.
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
drawer: ResponsiveLayout.isSmallScreen(context) ? NavDrawer() : null,
body: Container(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
NavBar(),
Body(),
Footer(),
],
),
),
),
);
}
}
class Body extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ResponsiveLayout(
largeScreen: LargeScreen(),
mediumScreen: LargeScreen(),
smallScreen: LargeScreen(),
);
}
}
class LargeScreen extends StatefulWidget {
#override
_LargeScreenState createState() => _LargeScreenState();
}
class _LargeScreenState extends State<LargeScreen> {
VideoPlayerController _videoPlayerController;
Future<void> _initializeVideoPlayerFuture;
#override
void initState() {
_videoPlayerController = VideoPlayerController.asset(
'assets/videos/video.mp4',
);
_initializeVideoPlayerFuture = _videoPlayerController.initialize();
super.initState();
}
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 40),
child: Column(
children: <Widget>[
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 VideoPlayer.
return AspectRatio(
aspectRatio: _videoPlayerController.value.aspectRatio,
// Use the VideoPlayer widget to display the video.
child: VideoPlayer(_videoPlayerController),
);
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return Center(child: CircularProgressIndicator());
}
},
),
],
),
);
}
#override
void dispose() {
super.dispose();
_videoPlayerController.dispose();
}
}

you can wrap your Widgets in stack widget and use positioning to position your text
AspectRatio(
aspectRatio: _videoPlayerController.value.aspectRatio,
// Use the VideoPlayer widget to display the video.
child: Stack(children:<Widget>[
VideoPlayer(_videoPlayerController),
Positioned(bottom:10,left:10,
child:Text("my text here))
])
)
update
If anyone knows how to make a video play automatically that would be great as well - I'm using the video_player package.
you can set the state of your video using your controller
_videoPlayerController.value.isPlaying
I want the video to resize to fit the browser window.
you can wrap your video in a SizedBox.expand which Creates a box that will become as large as its parent allows.
sized box constructors

Related

Can't play rive animation flutter

class Animation extends State<ConnectToFriend> {
late RiveAnimationController riveAnimationController;
#override
void initState() {
super.initState();
riveAnimationController = SimpleAnimation('idle');
setState(() {});
}
#override
Widget build(BuildContext context) {
return LoaderOverlay(
useDefaultLoading: false,
overlayOpacity: 0.6,
overlayWidget: Center(child: CircularProgressIndicator()),
child: Scaffold(
body: Positioned(
child: Container(
child: RiveAnimation.asset(
'assets/animations/4054-8407-polito (2).riv',
controllers: [riveAnimationController],
animations: const ['idle'],
fit: BoxFit.cover,
onInit: (p0) => setState(() {}))))),
);
}
}
enter image description here
enter image description here
enter image description here
i can't play the polito animation in rive flutter, it's static
can anyone help me?I am new to Rive and because there is close to no good documentation for Rive 2 I wanted to ask here.
here is link https://rive.app/community/4054-8407-polito/

How to set Camera Preview to full screen in Flutter?

I am using the camera 0.9.4+5 package.
I think the Camera Preview's size and aspect ratio of the package has been changed.
Please let me know how to set the Camera Preview to full screen without stretch.
Thank you.
For now the only solution in my mind is to make the package local and removed it from yaml file and then make the aspect ratio according to your will :)
Place your CameraPreview inside of an AspectRatio inside of a SizedBox with you screen dimensions.
Code below:
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import '../../main.dart';
// I defined cameras in main
class FullScreenCamera extends StatefulWidget {
const FullScreenCamera({Key? key}) : super(key: key);
#override
_FullScreenCameraState createState() => _FullScreenCameraState();
}
class _FullScreenCameraState extends State<FullScreenCamera>
with WidgetsBindingObserver {
late CameraController controller;
late Future<void> _initializeControllerFuture;
double? _screenWidth;
double? _screenHeight;
#override
void initState() {
super.initState();
controller = CameraController(cameras[0], ResolutionPreset.ultraHigh);
controller.setFlashMode(FlashMode.auto);
_initializeControllerFuture = controller.initialize();
}
#override
void dispose() {
controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
_screenWidth = MediaQuery.of(context).size.width;
_screenHeight = MediaQuery.of(context).size.height;
return Scaffold(
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Stack(
children: [
SizedBox(
width: _screenWidth,
height: _screenHeight,
child: AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
),
),
],
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
);
}
}
Edit:
I didn't realize, but the code is creating stretching as seen below on an iphone 13 pro with a 19.5:9 aspect ratio... As Christopher Perry pointed out, the camera view aspect ratio appears stuck at 16:9 and just fills out the screen resulting in stretching.
Stretched in full screen:
Normal:

Flutter: video player crop/cover

I'm trying to show a video inside a container, videos could exceed container's aspect ratio, so the they need to be cropped.
With images it is simple as that:
Image.network(someUrl, fit: BoxFit.cover)
but how to do it with VideoPlayer?
my current code looks like this:
Stack(
children: [
Container(
width: double.infinity,
height: double.infinity,
child: ShowcaseVideoPlayer(someUrl)
),
Material(
color: Colors.transparent,
child: InkWell(
onTap: () {},
),
),
],);
class ShowcaseVideoPlayer extends StatefulWidget {
final String url;
ShowcaseVideoPlayer(this.url);
#override
_ShowcaseVideoPlayerState createState() => _ShowcaseVideoPlayerState();
}
class _ShowcaseVideoPlayerState extends State<ShowcaseVideoPlayer> {
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;
#override
void initState() {
_controller = VideoPlayerController.network(widget.url);
_initializeVideoPlayerFuture = _controller.initialize();
_controller.setVolume(0);
super.initState();
}
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
_controller.play();
// If the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the VideoPlayer.
return 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 Center(),
);
}
},
);
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
}
I need a stack as I will have to show other things over the video. I tried without the stack and the player maintains the aspect ratio of the video, basically letter-boxing it. With the stack, the video stratches, losing the original aspect ratio:
to be clear, what I want is this:
EDIT: I tried random things on it and managed to achieve something, I removed the Container and wrapped the VideoPlayer like this:
Stack(
children: [
OverflowBox(
child: Wrap(
children: [
ShowcaseVideoPlayer(someUrl)
],
),
)]);
now the the video is actually cropped, but not centered (I see the upper part of the video instead of the central one). Wrapping Wrap in a Center and setting alignment: Alignment.center on OverfowBox have no effect. Any idea?

How do i play a network video in flutter

I am trying to put a network video from a different site in Flutter.
I have used the video_player package. I have used a future builder in which, the CircularProgressIndicator will keep running until the video is loaded. When i run the app, in the start the CircularProgressIndicator keep on running and after a few seconds it stops as if the video is loaded but it show complete blankness on the emulator. In other words the video is not loading.
After i start the app
After Loading
This is the code
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;
#override
void initState() {
_controller = VideoPlayerController.network(
'https://ok.ru/videoembed/1616636152346');
_initializeVideoPlayerFuture = _controller.initialize();
_controller.setLooping(true);
_controller.setVolume(1.0);
super.initState();
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
),
body: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot){
if(snapshot.connectionState == ConnectionState.done){
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
);
} else {
return Center(
child: CircularProgressIndicator(backgroundColor: Colors.blue,
),
);
}
}
),
floatingActionButton: FloatingActionButton(
onPressed: (){
setState(() {
if(_controller.value.isPlaying){
_controller.pause();
}else{
_controller.play();
}
});
},
child: Icon(_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
);
}
}
Your link for video is not a valid video link , it is a embed Url.
To Play a video from network URL use direct video URL like https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4.
Put this link in your videoController , it will working fine.

How do you play a video outside of the video player class?

Sorry if the question isn't that straight forward I'm just starting out. Every play video example I see through flutter examples uses a floating action button in the same class as the video player. I want to add a video player instance to my home screen and experiment with different ways to play the video (tapping on different elements, etc. I can't seem to gain access to the instance to access the controller. I'm not sure how to actually create a video player instance and then access the video controller from another place.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class VideoPlayerScreen extends StatefulWidget {
VideoPlayerScreen({Key key}) : super(key: key);
final VideoPlayerScreenState videoState = new VideoPlayerScreenState();
#override
VideoPlayerScreenState createState() => VideoPlayerScreenState();
}
class VideoPlayerScreenState extends State<VideoPlayerScreen> {
VideoPlayerController controller;
Future<void> initializeVideoPlayerFuture;
#override
void initState() {
// Create and store the VideoPlayerController. The VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
controller = VideoPlayerController.network('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);
super.initState();
}
#override
void dispose() {
// Ensure you dispose the VideoPlayerController to free up resources
controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Container(
// Use a FutureBuilder to display a loading spinner while you wait for the
// VideoPlayerController to finish initializing.
child: 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 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 Center(child: CircularProgressIndicator());
}
},
),
);
}
}
//this is the button I'm calling from the app.dart file
Widget playPauseButton(VideoPlayerScreen videoPlayer){
return IconButton(
alignment: Alignment.center,
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 (videoPlayer.videoState.controller.value.isPlaying) {
videoPlayer.videoState.controller.pause();
} else {
// If the video is paused, play it
videoPlayer.videoState.controller.play();
}
});
},
icon: Icon(videoPlayer.videoState.controller.value.isPlaying ? Icons.pause: Icons.play_arrow),
);
}
you can create a class named VideoProvider and put a VideoPlayer widget inside there.
after that, all you need is create a parameter named controller and pass it to your VideoPlayer widget. controller should be a type of VideoPlayerController;
here is an example :
class MySpecificPage extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _MySpecificPageState();
}
}
class _MySpecificPageState extends State<MySpecificPage> {
VideoPlayerController controller;
VoidCallback listener;
#override
void initState() {
listener = () => setState(() {});
videoHandler();
super.initState();
}
void videoHandler() {
if (controller == null) {
controller = VideoPlayerController.network('https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4')
..addListener(listener)
..setVolume(0.5)
..initialize();
} else {
if (controller.value.isPlaying) {
controller.pause();
} else {
controller.play();
}
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Videop Provider Example'),
),
body:Container(
child: Column(
children: <Widget>[
VideoProvider(controller),
RaisedButton(
child: Text('click here to play & puase the video'),
onPressed: () {
videoHandler();
},
)
],
),
),
);
}
}
class VideoProvider extends StatelessWidget {
final VideoPlayerController controller;
VideoProvider(this.controller);
#override
Widget build(BuildContext context) {
return AspectRatio(
aspectRatio: 16 / 9,
child: VideoPlayer(
controller
),
);
}
}