youtube_player_iframe 1.0.0 youtube_player_iframe: ^1.0.0 copied to clipboard
Flutter port of the official YouTube iFrame player API. Supports web & mobile platforms.
// Copyright 2020 Sarbagya Dhaubanjar. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:youtube_player_iframe/youtube_player_iframe.dart';
import 'widgets/meta_data_section.dart';
import 'widgets/play_pause_button_bar.dart';
import 'widgets/player_state_section.dart';
import 'widgets/source_input_section.dart';
import 'widgets/volume_slider.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(YoutubeApp());
}
class YoutubeApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Youtube Player IFrame Demo',
theme: ThemeData(
primarySwatch: Colors.deepPurple,
iconTheme: IconThemeData(color: Colors.deepPurpleAccent),
),
debugShowCheckedModeBanner: false,
home: YoutubeAppDemo(),
);
}
}
class YoutubeAppDemo extends StatefulWidget {
@override
_YoutubeAppDemoState createState() => _YoutubeAppDemoState();
}
class _YoutubeAppDemoState extends State<YoutubeAppDemo> {
YoutubePlayerController _controller;
@override
void initState() {
super.initState();
_controller = YoutubePlayerController(
initialVideoId: 'K18cpp_-gP8',
params: YoutubePlayerParams(
playlist: [
'nPt8bK2gbaU',
'gQDByCdjUXw',
'iLnmTe5Q2Qw',
'_WoCV4c6XOE',
'KmzdUe0RSJo',
'6jZDSSZZxjQ',
'p2lYr3vM_1w',
'7QUtEmBT_-w',
'34_PXCzGw1M',
],
startAt: Duration(seconds: 30),
showControls: true,
showFullscreenButton: true,
),
)..listen((event) {
log(event.toString());
});
_controller.onEnterFullscreen = () => log('Entered Fullscreen');
_controller.onExitFullscreen = () {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
Future.delayed(const Duration(seconds: 1), () {
SystemChrome.setPreferredOrientations(DeviceOrientation.values);
});
log('Exited Fullscreen');
};
}
@override
Widget build(BuildContext context) {
return YoutubePlayerControllerProvider(
// Passing controller to widgets below.
controller: _controller,
child: Scaffold(
appBar: AppBar(
title: Text('Youtube Player Demo'),
),
body: LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth > 800) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(child: YoutubePlayerIFrame()),
SizedBox(width: 500, child: Controls()),
],
);
}
return ListView(
children: [
YoutubePlayerIFrame(),
Controls(),
],
);
},
),
),
);
}
@override
void dispose() {
_controller.close();
super.dispose();
}
}
class Controls extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_space,
MetaDataSection(),
_space,
SourceInputSection(),
_space,
PlayPauseButtonBar(),
_space,
VolumeSlider(),
_space,
PlayerStateSection(),
],
),
);
}
Widget get _space => const SizedBox(height: 10);
}