11import 'package:flutter/material.dart' ;
22import 'package:video_player/video_player.dart' ;
33
4- class FullScreenVideoPlayer extends StatelessWidget {
4+ class FullScreenVideoPlayer extends StatefulWidget {
55 const FullScreenVideoPlayer ({
66 Key ? key,
77 required VideoPlayerController videoPlayerController,
@@ -10,32 +10,78 @@ class FullScreenVideoPlayer extends StatelessWidget {
1010
1111 final VideoPlayerController _videoPlayerController;
1212
13+ @override
14+ _FullScreenVideoPlayerState createState () => _FullScreenVideoPlayerState ();
15+ }
16+
17+ class _FullScreenVideoPlayerState extends State <FullScreenVideoPlayer > {
1318 @override
1419 Widget build (BuildContext context) {
1520 return GestureDetector (
16- onTap: () {
21+ onTap: () async {
1722 // Toggle video play
18- if (_videoPlayerController.value.isPlaying) {
19- _videoPlayerController.pause ();
23+ if (widget. _videoPlayerController.value.isPlaying) {
24+ await widget. _videoPlayerController.pause ();
2025 } else {
21- _videoPlayerController.play ();
26+ if (isVideoAtEnd) {
27+ await widget._videoPlayerController.seekTo (Duration .zero);
28+ }
29+ await widget._videoPlayerController.play ();
2230 }
2331 },
24- child: ClipRect (
25- child: OverflowBox (
26- alignment: Alignment .center,
27- child: FittedBox (
28- fit: BoxFit .cover,
29- child: SizedBox (
30- height: 1 ,
31- child: AspectRatio (
32- aspectRatio: _videoPlayerController.value.aspectRatio,
33- child: VideoPlayer (_videoPlayerController),
32+ child: Stack (
33+ fit: StackFit .expand,
34+ children: [
35+ ClipRect (
36+ child: OverflowBox (
37+ alignment: Alignment .center,
38+ child: FittedBox (
39+ fit: BoxFit .cover,
40+ child: SizedBox (
41+ height: 1 ,
42+ child: AspectRatio (
43+ aspectRatio: widget._videoPlayerController.value.aspectRatio,
44+ child: VideoPlayer (widget._videoPlayerController),
45+ ),
46+ ),
3447 ),
3548 ),
3649 ),
37- ),
50+ if (! widget._videoPlayerController.value.isPlaying)
51+ Center (
52+ child: Material (
53+ borderRadius: BorderRadius .circular (100 ),
54+ color: const Color (0xFF000000 ).withOpacity (0.25 ),
55+ child: const Padding (
56+ padding: EdgeInsets .all (16.0 ),
57+ child: Icon (
58+ Icons .play_arrow,
59+ size: 56 ,
60+ ),
61+ ),
62+ ),
63+ ),
64+ ],
3865 ),
3966 );
4067 }
68+
69+ @override
70+ void initState () {
71+ widget._videoPlayerController.addListener (updateUi);
72+ super .initState ();
73+ }
74+
75+ @override
76+ void dispose () {
77+ widget._videoPlayerController.removeListener (updateUi);
78+ super .dispose ();
79+ }
80+
81+ void updateUi () {
82+ setState (() {});
83+ }
84+
85+ bool get isVideoAtEnd =>
86+ widget._videoPlayerController.value.duration == widget._videoPlayerController.value.position;
4187}
0 commit comments