|
1 | 1 | import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
2 | 3 | import 'package:youtube_player_flutter/youtube_player_flutter.dart'; |
3 | 4 | import 'package:url_launcher/url_launcher.dart'; |
4 | 5 |
|
5 | | -class VideoPage extends StatelessWidget { |
| 6 | +class VideoPage extends StatefulWidget { |
6 | 7 | final String videoId; |
7 | 8 | final String description; |
8 | 9 |
|
9 | | - const VideoPage( |
10 | | - {super.key, required this.videoId, required this.description}); |
| 10 | + const VideoPage({Key? key, required this.videoId, required this.description}) |
| 11 | + : super(key: key); |
11 | 12 |
|
12 | 13 | @override |
13 | | - Widget build(BuildContext context) { |
14 | | - final youtubePlayerController = YoutubePlayerController( |
15 | | - initialVideoId: videoId, |
| 14 | + _VideoPageState createState() => _VideoPageState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _VideoPageState extends State<VideoPage> { |
| 18 | + late final YoutubePlayerController youtubePlayerController; |
| 19 | + |
| 20 | + @override |
| 21 | + void initState() { |
| 22 | + super.initState(); |
| 23 | + SystemChrome.setPreferredOrientations([ |
| 24 | + DeviceOrientation.portraitUp, |
| 25 | + DeviceOrientation.portraitDown, |
| 26 | + ]); |
| 27 | + |
| 28 | + youtubePlayerController = YoutubePlayerController( |
| 29 | + initialVideoId: widget.videoId, |
16 | 30 | flags: const YoutubePlayerFlags( |
17 | 31 | autoPlay: false, |
18 | 32 | ), |
19 | 33 | ); |
| 34 | + } |
20 | 35 |
|
| 36 | + @override |
| 37 | + void dispose() { |
| 38 | + SystemChrome.setPreferredOrientations([ |
| 39 | + DeviceOrientation.portraitUp, |
| 40 | + DeviceOrientation.portraitDown, |
| 41 | + ]); |
| 42 | + super.dispose(); |
| 43 | + } |
| 44 | + |
| 45 | + @override |
| 46 | + Widget build(BuildContext context) { |
21 | 47 | return Scaffold( |
22 | 48 | appBar: AppBar(title: const Text('Mentor/Video')), |
23 | | - body: Padding( |
24 | | - padding: const EdgeInsets.all(16.0), |
25 | | - child: Column( |
26 | | - crossAxisAlignment: CrossAxisAlignment.stretch, |
27 | | - mainAxisAlignment: MainAxisAlignment.center, |
| 49 | + body: YoutubePlayerBuilder( |
| 50 | + player: YoutubePlayer( |
| 51 | + controller: youtubePlayerController, |
| 52 | + showVideoProgressIndicator: true, |
| 53 | + progressIndicatorColor: const Color.fromARGB(255, 50, 204, 102), |
| 54 | + ), |
| 55 | + builder: (context, player) { |
| 56 | + return Column( |
28 | 57 | children: [ |
29 | 58 | IconButton( |
30 | 59 | icon: const Icon(Icons.open_in_browser, |
31 | 60 | color: Color.fromARGB(255, 50, 204, 102)), |
32 | 61 | onPressed: () { |
33 | | - _launchURL(videoId); |
| 62 | + _launchURL(widget.videoId); |
34 | 63 | }, |
35 | 64 | ), |
36 | 65 | const SizedBox( |
37 | 66 | height: 2, |
38 | 67 | ), |
39 | | - YoutubePlayer( |
40 | | - controller: youtubePlayerController, |
41 | | - showVideoProgressIndicator: true, |
42 | | - progressIndicatorColor: |
43 | | - const Color.fromARGB(255, 50, 204, 102), |
44 | | - ), |
| 68 | + // some widgets |
| 69 | + player, |
45 | 70 | const SizedBox(height: 16.0), |
46 | | - Text(description), |
47 | | - ])), |
| 71 | + Text(widget.description), |
| 72 | + //some other widgets |
| 73 | + ], |
| 74 | + ); |
| 75 | + }), |
48 | 76 | ); |
49 | 77 | } |
50 | | -} |
51 | 78 |
|
52 | | -_launchURL(String videoId) async { |
53 | | - final url = 'https://www.youtube.com/watch?v=$videoId'; |
54 | | - Uri uri = Uri.parse(url); |
55 | | - if (!await canLaunchUrl(uri)) { |
56 | | - throw Exception('Could not launch $url'); |
| 79 | + _launchURL(String videoId) async { |
| 80 | + final url = 'https://www.youtube.com/watch?v=$videoId'; |
| 81 | + Uri uri = Uri.parse(url); |
| 82 | + if (!await canLaunchUrl(uri)) { |
| 83 | + throw Exception('Could not launch $url'); |
| 84 | + } |
| 85 | + await launchUrl(uri); |
57 | 86 | } |
58 | | - await launchUrl(uri); |
59 | 87 | } |
0 commit comments