Skip to content

Commit d3e5d56

Browse files
committed
can play youtube video
1 parent a78fa19 commit d3e5d56

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

example/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ android {
3434
defaultConfig {
3535
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
3636
applicationId "com.example.app"
37-
minSdkVersion 16
37+
minSdkVersion 17
3838
targetSdkVersion 29
3939
versionCode flutterVersionCode.toInteger()
4040
versionName flutterVersionName

lib/src/widgets/editor.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:flutter/gestures.dart';
88
import 'package:flutter/material.dart';
99
import 'package:flutter/rendering.dart';
1010
import 'package:flutter/services.dart';
11+
import 'package:flutter_quill/src/widgets/youtube_video_app.dart';
1112
import 'package:string_validator/string_validator.dart';
1213
import 'package:url_launcher/url_launcher.dart';
1314

@@ -109,7 +110,14 @@ Widget _defaultEmbedBuilder(
109110
: Image.file(io.File(imageUrl));
110111
case 'video':
111112
final videoUrl = node.value.data;
112-
return VideoApp(videoUrl: videoUrl, context: context, readOnly: readOnly);
113+
if (videoUrl.toString().contains('www.youtube.com') ||
114+
videoUrl.toString().contains('youtu.be')) {
115+
return YoutubeVideoApp(
116+
videoUrl: videoUrl, context: context, readOnly: readOnly);
117+
} else {
118+
return VideoApp(
119+
videoUrl: videoUrl, context: context, readOnly: readOnly);
120+
}
113121
default:
114122
throw UnimplementedError(
115123
'Embeddable type "${node.value.type}" is not supported by default '
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import 'package:flutter/gestures.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:url_launcher/url_launcher.dart';
4+
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
5+
import '../../flutter_quill.dart';
6+
7+
class YoutubeVideoApp extends StatefulWidget {
8+
const YoutubeVideoApp(
9+
{required this.videoUrl, required this.context, required this.readOnly});
10+
11+
final String videoUrl;
12+
final BuildContext context;
13+
final bool readOnly;
14+
15+
@override
16+
_YoutubeVideoAppState createState() => _YoutubeVideoAppState();
17+
}
18+
19+
class _YoutubeVideoAppState extends State<YoutubeVideoApp> {
20+
var _youtubeController;
21+
22+
@override
23+
void initState() {
24+
super.initState();
25+
final videoId = YoutubePlayer.convertUrlToId(widget.videoUrl);
26+
if (videoId != null) {
27+
_youtubeController = YoutubePlayerController(
28+
initialVideoId: videoId,
29+
flags: const YoutubePlayerFlags(
30+
autoPlay: false,
31+
),
32+
);
33+
}
34+
}
35+
36+
@override
37+
Widget build(BuildContext context) {
38+
final defaultStyles = DefaultStyles.getInstance(context);
39+
if (_youtubeController == null) {
40+
print('problem with youtube video');
41+
if (widget.readOnly) {
42+
return RichText(
43+
text: TextSpan(
44+
text: widget.videoUrl,
45+
style: defaultStyles.link,
46+
recognizer: TapGestureRecognizer()
47+
..onTap = () => launch(widget.videoUrl)),
48+
);
49+
}
50+
51+
return RichText(
52+
text: TextSpan(text: widget.videoUrl, style: defaultStyles.link));
53+
}
54+
55+
return Container(
56+
height: 300,
57+
child: YoutubePlayerBuilder(
58+
player: YoutubePlayer(
59+
controller: _youtubeController,
60+
showVideoProgressIndicator: true,
61+
),
62+
builder: (context, player) {
63+
return Column(
64+
children: [
65+
// some widgets
66+
player,
67+
//some other widgets
68+
],
69+
);
70+
},
71+
),
72+
);
73+
}
74+
75+
@override
76+
void dispose() {
77+
super.dispose();
78+
_youtubeController.dispose();
79+
}
80+
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies:
2424
pedantic: ^1.11.0
2525
video_player: ^2.1.10
2626
characters: ^1.1.0
27+
youtube_player_flutter: ^8.0.0
2728

2829
dev_dependencies:
2930
flutter_test:

0 commit comments

Comments
 (0)