Skip to content

Instantly share code, notes, and snippets.

@allansrc
Last active January 17, 2025 23:07
Show Gist options
  • Save allansrc/2cc2275947b68210c12e13a98ea82e02 to your computer and use it in GitHub Desktop.
Save allansrc/2cc2275947b68210c12e13a98ea82e02 to your computer and use it in GitHub Desktop.
video_player: ^2.9.2
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Jarbas Video Player"),
),
body: MyWidget(),
),
),
);
}
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Default Layout"),
),
body: Column(
children: [
Center(
child: Text("Hello World!"),
),
Container(
color: Colors.blue,
height: 100,
width: 100,
child: Center(
child: Text("Hello World!"),
),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Scaffold(
body: Column(
children: [
Jarbasvideoplayer(
urlVideo: 'http://commondatastorage.googleapis.com/'
'gtv-videos-bucket/sample/BigBuckBunny.mp4',
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text("Voltar removendo da arvore"),
),
],
),
),
),
);
},
child: Text("Ir para o video"))
],
),
);
}
}
class Jarbasvideoplayer extends StatefulWidget {
const Jarbasvideoplayer({super.key, this.urlVideo});
final String? urlVideo;
@override
State<Jarbasvideoplayer> createState() => _JarbasvideoplayerState();
}
class _JarbasvideoplayerState extends State<Jarbasvideoplayer> {
late VideoPlayerController controllerVideo;
late Future<void> _initializeVideoPlayerFuture;
late _JarbasvideoplayerState instance;
@override
void initState() {
super.initState();
controllerVideo = VideoPlayerController.networkUrl(
Uri.parse(
widget.urlVideo!,
),
);
controllerVideo.setLooping(true);
_initializeVideoPlayerFuture =
controllerVideo.initialize().whenComplete(() {
controllerVideo.play();
});
instance = _JarbasvideoplayerState();
}
@override
void dispose() {
controllerVideo.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Builder(builder: (BuildContext context) {
return FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(
color: Colors.blue,
),
);
} else if (snapshot.connectionState == ConnectionState.done) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
AspectRatio(
aspectRatio: controllerVideo.value.aspectRatio,
child: Stack(
children: [
VideoPlayer(controllerVideo),
GestureDetector(
onTap: () {
setState(() {
if (controllerVideo.value.isPlaying) {
controllerVideo.pause();
} else {
controllerVideo.play();
}
});
},
child: Opacity(
opacity: 0,
child: Container(
color: Colors.black,
))),
Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: IconButton(
color: Colors.blue,
style: ButtonStyle(
backgroundColor:
WidgetStatePropertyAll(
Colors.blue)),
alignment: Alignment(0.2, 0.10),
onPressed: () {
setState(() {
if (controllerVideo.value.volume >
0) {
controllerVideo.setVolume(0);
} else {
controllerVideo.setVolume(100);
}
});
},
icon: Icon(
controllerVideo.value.volume == 0
? Icons.volume_off
: Icons.volume_up,
color: Colors.white,
)),
),
],
),
],
),
],
)),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyWidgetTheSecond(),
),
);
},
child: Text("Ir para o prox tela, mantendo na arvore"))
],
);
} else {
return Text("Video não carregado!");
}
});
});
}
}
class MyWidgetTheSecond extends StatelessWidget {
const MyWidgetTheSecond({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Placeholder(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment