I’m trying to create a kind of feed such as Insta. I have a widget video that I need to have a height set depending of the ratio of the video, but couldn’t find a solution.
The actual issue is that I’m working on FlutterFlow, which has a video widget that does not allow to mute the video. So I’m trying to create a video widget that will loop, with auto-play, sound off, and that will have 100% width and the height should be calculate form the ratio to fit each video into the feed.
class CustomVidPlayerCopy extends StatefulWidget {
CustomVidPlayerCopy({
this.width,
this.height,
required this.videoPath,
});
final double? width;
final double? height;
final String videoPath;
@override
_CustomVidPlayerState createState() => _CustomVidPlayerState();
}
// Define the state for the CustomVidPlayer widget
class _CustomVidPlayerState extends State<CustomVidPlayerCopy> {
late VideoPlayerController _videoPlayerController;
late ChewieController _chewieController;
bool _isVideoPlaying = false;
@override
void initState() {
super.initState();
// Initialize the VideoPlayerController with the provided videoPath
_videoPlayerController =
VideoPlayerController.networkUrl(Uri.parse(widget.videoPath));
_videoPlayerController.setVolume(0.0);
_videoPlayerController.initialize().then((_) {
setState(() {});
// Initialize the ChewieController
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
aspectRatio: _videoPlayerController.value.aspectRatio,
looping: true,
showControls: false,
// Set the loading indicator's color
materialProgressColors: ChewieProgressColors(
playedColor: Colors.pink, // You can set a default color here
handleColor: Colors.pink, // You can set a default color here
),
);
});
}
@override
void dispose() {
// Dispose of the video and Chewie controllers
_videoPlayerController.dispose();
_chewieController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return VisibilityDetector(
key: Key(widget.videoPath),
onVisibilityChanged: (visibilityInfo) {
// Play or pause the video based on visibility
if (visibilityInfo.visibleFraction >= 1) {
_videoPlayerController.play();
} else {
_videoPlayerController.pause();
}
},
child: Container(
child: _videoPlayerController.value.isInitialized
? Stack(
children: [
// Display the video using Chewie
Chewie(
controller: _chewieController,
),
GestureDetector(
behavior: HitTestBehavior.opaque,
),
],
)
: Center(
child: CircularProgressIndicator(),
),
),
);
}
}
I tried adding this to my code but the scroll Up get jerky after that:
// Set default width, height, aspect ratio
double get width => widget.width == null || widget.width! >= double.infinity
? MediaQuery.sizeOf(context).width
: widget.width!;
double get height =>
widget.height == null || widget.height! >= double.infinity
? width / aspectRatio
: widget.height!;
double get aspectRatio =>
_chewieController.videoPlayerController.value.aspectRatio;
2
Answers
I actually fixed the height issue, but I still get an issue when scrolling up.
Error
Dynamically calculate the height based on the aspect ratio of the video.
Try using below code: