Skip to content

Instantly share code, notes, and snippets.

@alpbak
Last active November 28, 2023 05:28
Show Gist options
  • Save alpbak/5a4b0f311d41e1e5c0f0e4f16b12d292 to your computer and use it in GitHub Desktop.
Save alpbak/5a4b0f311d41e1e5c0f0e4f16b12d292 to your computer and use it in GitHub Desktop.
*/
I took this code from a production app and abbreviated it.
You can't copy and paste this code.
The code is not complete.
This is just to give you and idea how to play videos in scroll views in flutter
*/
Widget _videoScrollView() {
const Key centerKey = ValueKey<String>('video-sliver-list');
return CustomScrollView(
controller: videosScrollController,
scrollDirection: Axis.vertical,
center: centerKey,
physics: PageScrollPhysics(),
slivers: <Widget>[
SliverList(
key: centerKey,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
scrollVideoIndex = index;
return MainScreenBackgroundWidget(
context: context, index: selectedIndex);
},
childCount: videoArray.length,
),
),
],
);
}
//MainScreenBackgroundWidget
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
Key _videoDisplayerkey = Key("VideoKey" + index.toString());
final GlobalKey<VideoDisplayerState> _videoDisplayerkey = GlobalKey();
return Stack(
children: [
Container(
height: MediaQuery.of(context).size.height,
child: VideoPlayerWidget(key: _videoDisplayerkey,)
)
],
);
}
//VideoPlayerWidget
class VideoPlayerWidget extends StatefulWidget {
VideoPlayerWidget({Key? key, required this.videoName, required this.videoUrl})
: super(key: key);
final String videoName;
final String videoUrl;
@override
VideoPlayerWidgetState createState() => VideoPlayerWidgetState();
}
class VideoPlayerWidgetState extends State<VideoPlayerWidget> {
late VideoPlayerController _videoController;
bool isVideoReady = false;
@override
void initState() {
if (widget.videoName.length > 0) {
setupVideoPlayerForAsset();
} else {
setupVideoPlayerForUrl();
}
super.initState();
}
@override
void dispose() {
_videoController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return isVideoReady
? SizedBox.expand(
child: FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: _videoController.value.size.width,
height: _videoController.value.size.height,
child: VideoPlayer(_videoController)),
),
)
: waitWidget();
}
Widget waitWidget() {
return Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
//color: Colors.blueAccent,
child: Container(
color: Colors.black,
child: Center(
child: CircularProgressIndicator(
color: Colors.blue,
),
),
),
);
}
setupVideoPlayerForAsset() {
_videoController = VideoPlayerController.asset(widget.videoName)
..initialize().then((_) {
print("VIDEO PLAYER INITILIZED FOR ASSET");
_videoController.play();
_videoController.setLooping(true);
_videoController.setVolume(0);
setState(() {
isVideoReady = true;
});
});
}
setupVideoPlayerForUrl() {
_videoController = VideoPlayerController.network(widget.videoUrl)
..initialize().then((_) {
print("VIDEO PLAYER INITILIZED-URL FOR URL");
_videoController.play();
_videoController.setLooping(true);
_videoController.setVolume(0);
setState(() {
loadingVideoListenable.value = false;
isVideoReady = true;
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment