Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active December 20, 2023 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slightfoot/f6baee7f7c01779611317ffb4963aeeb to your computer and use it in GitHub Desktop.
Save slightfoot/f6baee7f7c01779611317ffb4963aeeb to your computer and use it in GitHub Desktop.
Flutter Video Player with Blurred background of the same video - by Simon Lightfoot :: 16th December 2023 #Flutter #Dart - https://www.youtube.com/live/kJWtSAFnJxk?si=CX8S1dF8GAZ-oh2d&t=5099
// MIT License
//
// Copyright (c) 2023 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
));
}
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
late final VideoPlayerController _videoController;
@override
void initState() {
super.initState();
_videoController = VideoPlayerController.asset('assets/bunny.mp4');
_videoController.initialize().then((_) {
_videoController.setLooping(true);
_videoController.play();
});
}
@override
void dispose() {
_videoController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Material(
color: Colors.black,
child: ListenableBuilder(
listenable: _videoController,
builder: (BuildContext context, Widget? child) {
if (_videoController.value.isInitialized == false) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Stack(
fit: StackFit.expand,
children: [
DecoratedBox(
position: DecorationPosition.foreground,
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.4),
),
child: FittedBox(
fit: BoxFit.cover,
child: SizedBox.fromSize(
size: _videoController.value.size,
child: VideoPlayer(_videoController),
),
),
),
BackdropFilter(
filter: ui.ImageFilter.blur(
sigmaX: 16.0,
sigmaY: 16.0,
),
child: Center(
child: DecoratedBox(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.6),
blurStyle: BlurStyle.outer,
blurRadius: 25.0,
spreadRadius: -5.0,
)
],
),
child: AspectRatio(
aspectRatio: _videoController.value.aspectRatio,
child: VideoPlayer(_videoController),
),
),
),
),
],
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment