Skip to content

Commit a8a19f2

Browse files
committed
Switch to using ChangeNotifier
We can switch bach to using ValueNotifier when we have a lot more values and it is more convenient to change and notify with one `value.copyWith(foo='bar')` instead of explicitely having to call notifyListeners(). Untill then the code is just much cleaner without so much boiler plate.
1 parent e9d84e0 commit a8a19f2

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

lib/src/chewie_controller.dart

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,7 @@ import 'package:chewie/src/chewie_progress_colors.dart';
22
import 'package:flutter/material.dart';
33
import 'package:video_player/video_player.dart';
44

5-
/// The state of the [ChewieController].
6-
@immutable
7-
class ChewieValue {
8-
ChewieValue({
9-
this.isFullScreen = false,
10-
});
11-
12-
/// True if the video is currently playing fullscreen
13-
final bool isFullScreen;
14-
15-
ChewieValue copyWith({
16-
VideoPlayerController videoPlayerController,
17-
bool isFullScreen,
18-
}) {
19-
return ChewieValue(
20-
isFullScreen: isFullScreen ?? this.isFullScreen,
21-
);
22-
}
23-
24-
@override
25-
String toString() {
26-
return '$runtimeType('
27-
'isFullscreen: $isFullScreen, ';
28-
}
29-
}
30-
31-
class ChewieController extends ValueNotifier<ChewieValue> {
5+
class ChewieController extends ChangeNotifier {
326
ChewieController({
337
this.videoPlayerController,
348
this.aspectRatio,
@@ -43,9 +17,8 @@ class ChewieController extends ValueNotifier<ChewieValue> {
4317
this.showControls = true,
4418
this.allowedScreenSleep = true,
4519
this.isLive = false,
46-
}) : assert(videoPlayerController != null,
47-
'You must provide a controller to play a video'),
48-
super(ChewieValue()) {
20+
}) : assert(videoPlayerController != null,
21+
'You must provide a controller to play a video') {
4922
_initialize();
5023
}
5124

@@ -94,7 +67,9 @@ class ChewieController extends ValueNotifier<ChewieValue> {
9467
/// Defines if the controls should be for live stream video
9568
final bool isLive;
9669

97-
bool get isFullScreen => value.isFullScreen;
70+
bool _isFullScreen = false;
71+
72+
bool get isFullScreen => _isFullScreen;
9873

9974
Future _initialize() async {
10075
await videoPlayerController.setLooping(looping);
@@ -117,24 +92,26 @@ class ChewieController extends ValueNotifier<ChewieValue> {
11792

11893
if (fullScreenByDefault) {
11994
videoPlayerController.addListener(() async {
120-
if (await videoPlayerController.value.isPlaying &&
121-
!value.isFullScreen) {
95+
if (await videoPlayerController.value.isPlaying && !_isFullScreen) {
12296
enterFullscreen();
12397
}
12498
});
12599
}
126100
}
127101

128102
void enterFullscreen() {
129-
value = value.copyWith(isFullScreen: true);
103+
_isFullScreen = true;
104+
notifyListeners();
130105
}
131106

132107
void exitFullscreen() {
133-
value = value.copyWith(isFullScreen: false);
108+
_isFullScreen = false;
109+
notifyListeners();
134110
}
135111

136112
void toggleFullscreen() {
137-
value = value.copyWith(isFullScreen: !value.isFullScreen);
113+
_isFullScreen = !_isFullScreen;
114+
notifyListeners();
138115
}
139116

140117
void play() {

0 commit comments

Comments
 (0)