Skip to content

Commit 55c130b

Browse files
authored
Merge pull request #14 from dshukertjr/feature/video_play
Drastically improved video play performance
2 parents f77eb9f + 0f858ce commit 55c130b

File tree

4 files changed

+64
-20
lines changed

4 files changed

+64
-20
lines changed
Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22
import '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
}

lib/cubits/video/video_cubit.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ class VideoCubit extends Cubit<VideoState> {
161161
if (_videoPlayerController == null) {
162162
_videoPlayerController = await _repository.getVideoPlayerController(_videoDetail!.url);
163163
await _videoPlayerController!.initialize();
164-
await _videoPlayerController!.setLooping(true);
165164
await _videoPlayerController!.play();
166165

167166
emit(VideoPlaying(

lib/repositories/repository.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,7 @@ class Repository {
439439
}
440440

441441
Future<VideoPlayerController> getVideoPlayerController(String url) async {
442-
final file = await DefaultCacheManager().getSingleFile(url);
443-
return VideoPlayerController.file(file);
442+
return VideoPlayerController.network(url);
444443
}
445444

446445
Future<bool> hasLocationPermission() async {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: spot
22
description: Find local video posts
3-
version: 1.0.11+21
3+
version: 1.0.12+22
44
publish_to: none
55

66
environment:

0 commit comments

Comments
 (0)