Created
September 21, 2021 10:32
-
-
Save nikhilmufc7/275e3e1bb887e59ec026388dcf4a2d06 to your computer and use it in GitHub Desktop.
Video Player Flutter Demo with Controls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:chewie/chewie.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:image_picker/image_picker.dart'; | |
import 'package:video_player/video_player.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Video Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
var pickedFile; | |
var _controller; | |
var chewieController; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
MaterialButton( | |
onPressed: () async { | |
pickedFile = await ImagePicker.platform | |
.pickVideo(source: ImageSource.camera); | |
if (pickedFile != null) { | |
setState(() { | |
_controller = VideoPlayerController.file( | |
File(pickedFile.path), | |
)..initialize().then((_) { | |
chewieController = ChewieController( | |
videoPlayerController: _controller, | |
autoPlay: true, | |
looping: true, | |
); | |
setState(() {}); | |
}); | |
}); | |
} | |
}, | |
child: Text("Pick Video"), | |
), | |
Container( | |
height: 300, | |
width: 300, | |
child: Center( | |
child: | |
chewieController != null && _controller.value.isInitialized | |
? Chewie( | |
controller: chewieController, | |
) | |
: Container(), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment