Skip to content

Commit d6dfff7

Browse files
HudsonJuniorHudson Proenca
andauthored
chore: improve performance using widgets instead of functions; allow to disable subtitle rendering (#49)
* chore: improve performance; make subtitles optional * chore: revert change log --------- Co-authored-by: Hudson Proenca <[email protected]>
1 parent 2bdc522 commit d6dfff7

File tree

3 files changed

+197
-183
lines changed

3 files changed

+197
-183
lines changed

lib/src/core/better_player.dart

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ class _BetterPlayerState extends State<BetterPlayer>
7070
void didChangeDependencies() {
7171
if (!_initialized) {
7272
final navigator = Navigator.of(context);
73-
setState(() {
74-
_navigatorState = navigator;
75-
});
73+
_navigatorState = navigator;
7674
_setup();
7775
_initialized = true;
7876
}
@@ -162,59 +160,20 @@ class _BetterPlayerState extends State<BetterPlayer>
162160
Widget build(BuildContext context) {
163161
return BetterPlayerControllerProvider(
164162
controller: widget.controller,
165-
child: _buildPlayer(),
163+
child: _InternalPlayer(controller: widget.controller),
166164
);
167165
}
168166

169-
Widget _buildFullScreenVideo(
170-
BuildContext context,
171-
Animation<double> animation,
172-
BetterPlayerControllerProvider controllerProvider) {
173-
return Scaffold(
174-
resizeToAvoidBottomInset: false,
175-
body: Container(
176-
alignment: Alignment.center,
177-
color: Colors.black,
178-
child: controllerProvider,
179-
),
180-
);
181-
}
182-
183-
AnimatedWidget _defaultRoutePageBuilder(
184-
BuildContext context,
185-
Animation<double> animation,
186-
Animation<double> secondaryAnimation,
187-
BetterPlayerControllerProvider controllerProvider) {
188-
return AnimatedBuilder(
189-
animation: animation,
190-
builder: (BuildContext context, Widget? child) {
191-
return _buildFullScreenVideo(context, animation, controllerProvider);
192-
},
193-
);
194-
}
195-
196-
Widget _fullScreenRoutePageBuilder(
197-
BuildContext context,
198-
Animation<double> animation,
199-
Animation<double> secondaryAnimation,
200-
) {
201-
final controllerProvider = BetterPlayerControllerProvider(
202-
controller: widget.controller, child: _buildPlayer());
203-
204-
final routePageBuilder = _betterPlayerConfiguration.routePageBuilder;
205-
if (routePageBuilder == null) {
206-
return _defaultRoutePageBuilder(
207-
context, animation, secondaryAnimation, controllerProvider);
208-
}
209-
210-
return routePageBuilder(
211-
context, animation, secondaryAnimation, controllerProvider);
212-
}
213-
214167
Future<dynamic> _pushFullScreenWidget(BuildContext context) async {
215168
final TransitionRoute<void> route = PageRouteBuilder<void>(
216169
settings: const RouteSettings(),
217-
pageBuilder: _fullScreenRoutePageBuilder,
170+
pageBuilder: (context, animation, secondaryAnimation) =>
171+
_FullScreenPageRouteBuilderWidget(
172+
controller: widget.controller,
173+
betterPlayerConfiguration: _betterPlayerConfiguration,
174+
animation: animation,
175+
secondaryAnimation: secondaryAnimation,
176+
),
218177
);
219178

220179
await SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
@@ -261,21 +220,74 @@ class _BetterPlayerState extends State<BetterPlayer>
261220
_betterPlayerConfiguration.deviceOrientationsAfterFullScreen);
262221
}
263222

264-
Widget _buildPlayer() {
223+
@override
224+
void didChangeAppLifecycleState(AppLifecycleState state) {
225+
super.didChangeAppLifecycleState(state);
226+
widget.controller.setAppLifecycleState(state);
227+
}
228+
}
229+
230+
class _InternalPlayer extends StatelessWidget {
231+
final BetterPlayerController controller;
232+
233+
const _InternalPlayer({required this.controller});
234+
235+
@override
236+
Widget build(BuildContext context) {
265237
return VisibilityDetector(
266-
key: Key("${widget.controller.hashCode}_key"),
238+
key: Key("${controller.hashCode}_key"),
267239
onVisibilityChanged: (VisibilityInfo info) =>
268-
widget.controller.onPlayerVisibilityChanged(info.visibleFraction),
240+
controller.onPlayerVisibilityChanged(info.visibleFraction),
269241
child: BetterPlayerWithControls(
270-
controller: widget.controller,
242+
controller: controller,
271243
),
272244
);
273245
}
246+
}
247+
248+
class _FullScreenPageRouteBuilderWidget extends StatelessWidget {
249+
final BetterPlayerController controller;
250+
final BetterPlayerConfiguration betterPlayerConfiguration;
251+
final Animation<double> animation;
252+
final Animation<double> secondaryAnimation;
253+
254+
const _FullScreenPageRouteBuilderWidget({
255+
required this.controller,
256+
required this.betterPlayerConfiguration,
257+
required this.animation,
258+
required this.secondaryAnimation,
259+
});
274260

275261
@override
276-
void didChangeAppLifecycleState(AppLifecycleState state) {
277-
super.didChangeAppLifecycleState(state);
278-
widget.controller.setAppLifecycleState(state);
262+
Widget build(BuildContext context) {
263+
final controllerProvider = BetterPlayerControllerProvider(
264+
controller: controller,
265+
child: _InternalPlayer(controller: controller),
266+
);
267+
268+
final routePageBuilder = betterPlayerConfiguration.routePageBuilder;
269+
if (routePageBuilder == null) {
270+
return AnimatedBuilder(
271+
animation: animation,
272+
builder: (BuildContext context, Widget? child) {
273+
return Scaffold(
274+
resizeToAvoidBottomInset: false,
275+
body: Container(
276+
alignment: Alignment.center,
277+
color: Colors.black,
278+
child: controllerProvider,
279+
),
280+
);
281+
},
282+
);
283+
}
284+
285+
return routePageBuilder(
286+
context,
287+
animation,
288+
secondaryAnimation,
289+
controllerProvider,
290+
);
279291
}
280292
}
281293

0 commit comments

Comments
 (0)