youtube_player_flutter 4.0.0+2 youtube_player_flutter: ^4.0.0+2 copied to clipboard
Flutter plugin for playing or streaming inline YouTube videos using the official iFrame player API. This plugin supports both Android and iOS.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
void main() {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Color(0xFFFF0000),
));
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Youtube Player Demo',
theme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.red,
appBarTheme: AppBarTheme(color: Color(0xFFFF0000)),
scaffoldBackgroundColor: Colors.black,
),
home: MyHomePage(title: 'Youtube Player Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
YoutubePlayerController _controller = YoutubePlayerController();
var _idController = TextEditingController();
var _seekToController = TextEditingController();
double _volume = 100;
bool _muted = false;
String _playerStatus = "";
String _errorCode = '0';
String _videoId = "iLnmTe5Q2Qw";
void listener() {
if (_controller.value.playerState == PlayerState.ENDED) {
_showThankYouDialog();
}
if (mounted) {
setState(() {
_playerStatus = _controller.value.playerState.toString();
_errorCode = _controller.value.errorCode.toString();
});
}
}
@override
void deactivate() {
// This pauses video while navigating to next page.
_controller.pause();
super.deactivate();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
widget.title,
style: TextStyle(color: Colors.white),
),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
YoutubePlayer(
context: context,
videoId: _videoId,
flags: YoutubePlayerFlags(
mute: false,
autoPlay: true,
forceHideAnnotation: true,
showVideoProgressIndicator: true,
disableDragSeek: false,
),
videoProgressIndicatorColor: Color(0xFFFF0000),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.white,
size: 20.0,
),
onPressed: () {},
),
Text(
'Hello! This is a test title.',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
Spacer(),
IconButton(
icon: Icon(
Icons.settings,
color: Colors.white,
size: 25.0,
),
onPressed: () {},
),
],
progressColors: ProgressColors(
playedColor: Color(0xFFFF0000),
handleColor: Color(0xFFFF4433),
),
onPlayerInitialized: (controller) {
_controller = controller;
_controller.addListener(listener);
},
),
SizedBox(
height: 10.0,
),
Padding(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextFormField(
controller: _idController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter youtube \<video id\> or \<link\>"),
),
SizedBox(
height: 10.0,
),
InkWell(
onTap: () {
setState(() {
_videoId = _idController.text;
// If text is link then converting to corresponding id.
if (_videoId.contains("http"))
_videoId = YoutubePlayer.convertUrlToId(_videoId);
});
},
child: Container(
padding: EdgeInsets.symmetric(
vertical: 16.0,
),
color: Color(0xFFFF0000),
child: Text(
"PLAY",
style: TextStyle(fontSize: 18.0, color: Colors.white),
textAlign: TextAlign.center,
),
),
),
SizedBox(
height: 10.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
icon: Icon(
_controller.value.isPlaying
? Icons.play_arrow
: Icons.pause,
),
onPressed: () {
_controller.value.isPlaying
? _controller.pause()
: _controller.play();
setState(() {});
},
),
IconButton(
icon: Icon(_muted ? Icons.volume_off : Icons.volume_up),
onPressed: () {
_muted ? _controller.unMute() : _controller.mute();
setState(() {
_muted = !_muted;
});
},
),
IconButton(
icon: Icon(Icons.fullscreen),
onPressed: () => _controller.enterFullScreen(),
),
],
),
SizedBox(
height: 10.0,
),
TextField(
controller: _seekToController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: "Seek to seconds",
suffixIcon: Padding(
padding: EdgeInsets.all(5.0),
child: OutlineButton(
child: Text("Seek"),
onPressed: () => _controller.seekTo(
Duration(
seconds: int.parse(_seekToController.text),
),
),
),
),
),
),
SizedBox(
height: 10.0,
),
Row(
children: <Widget>[
Text(
"Volume",
style: TextStyle(fontWeight: FontWeight.w300),
),
Expanded(
child: Slider(
inactiveColor: Colors.transparent,
value: _volume,
min: 0.0,
max: 100.0,
divisions: 10,
label: '${(_volume).round()}',
onChanged: (value) {
setState(() {
_volume = value;
});
_controller.setVolume(_volume.round());
},
),
),
],
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text(
"Status: $_playerStatus",
style: TextStyle(
fontWeight: FontWeight.w300,
),
textAlign: TextAlign.center,
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text(
"Error Code: $_errorCode",
style: TextStyle(
fontWeight: FontWeight.w300,
),
textAlign: TextAlign.center,
),
),
],
),
),
],
),
),
);
}
void _showThankYouDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Video Ended"),
content: Text("Thank you for trying the plugin!"),
);
},
);
}
}