8
8
import 'package:flutter/cupertino.dart' ;
9
9
import 'package:flutter/material.dart' ;
10
10
import 'package:video_player/video_player.dart' ;
11
+ import 'package:flutter/scheduler.dart' ;
11
12
12
13
/// Controls play and pause of [controller] .
13
14
///
@@ -28,7 +29,7 @@ class VideoPlayPause extends StatefulWidget {
28
29
class _VideoPlayPauseState extends State <VideoPlayPause > {
29
30
_VideoPlayPauseState () {
30
31
listener = () {
31
- setState (() {});
32
+ SchedulerBinding .instance. addPostFrameCallback ((_) => setState (() {}) );
32
33
};
33
34
}
34
35
@@ -48,8 +49,11 @@ class _VideoPlayPauseState extends State<VideoPlayPause> {
48
49
49
50
@override
50
51
void deactivate () {
51
- controller.setVolume (0.0 );
52
- controller.removeListener (listener);
52
+ SchedulerBinding .instance.addPostFrameCallback ((_) {
53
+ controller.setVolume (0.0 );
54
+ controller.removeListener (listener);
55
+ });
56
+
53
57
super .deactivate ();
54
58
}
55
59
@@ -360,73 +364,152 @@ class AspectRatioVideoState extends State<AspectRatioVideo> {
360
364
}
361
365
}
362
366
363
- void main () {
364
- runApp (
365
- MaterialApp (
366
- home: DefaultTabController (
367
- length: 3 ,
368
- child: Scaffold (
369
- appBar: AppBar (
370
- title: const Text ('Video player example' ),
371
- bottom: const TabBar (
372
- isScrollable: true ,
373
- tabs: < Widget > [
374
- Tab (
375
- icon: Icon (Icons .cloud),
376
- text: "Remote" ,
377
- ),
378
- Tab (icon: Icon (Icons .insert_drive_file), text: "Asset" ),
379
- Tab (icon: Icon (Icons .list), text: "List example" ),
380
- ],
381
- ),
367
+ class App extends StatelessWidget {
368
+ @override
369
+ Widget build (BuildContext context) {
370
+ return DefaultTabController (
371
+ length: 3 ,
372
+ child: Scaffold (
373
+ key: const ValueKey <String >('home_page' ),
374
+ appBar: AppBar (
375
+ title: const Text ('Video player example' ),
376
+ actions: < Widget > [
377
+ IconButton (
378
+ key: const ValueKey <String >('push_tab' ),
379
+ icon: const Icon (Icons .navigation),
380
+ onPressed: () {
381
+ Navigator .push <PlayerVideoAndPopPage >(
382
+ context,
383
+ MaterialPageRoute <PlayerVideoAndPopPage >(
384
+ builder: (BuildContext context) =>
385
+ PlayerVideoAndPopPage ()),
386
+ );
387
+ },
388
+ )
389
+ ],
390
+ bottom: const TabBar (
391
+ isScrollable: true ,
392
+ tabs: < Widget > [
393
+ Tab (
394
+ icon: Icon (Icons .cloud),
395
+ text: "Remote" ,
396
+ ),
397
+ Tab (icon: Icon (Icons .insert_drive_file), text: "Asset" ),
398
+ Tab (icon: Icon (Icons .list), text: "List example" ),
399
+ ],
382
400
),
383
- body: TabBarView (
384
- children: < Widget > [
385
- SingleChildScrollView (
386
- child: Column (
387
- children: < Widget > [
388
- Container (
389
- padding: const EdgeInsets .only (top: 20.0 ),
401
+ ),
402
+ body: TabBarView (
403
+ children: < Widget > [
404
+ SingleChildScrollView (
405
+ child: Column (
406
+ children: < Widget > [
407
+ Container (
408
+ padding: const EdgeInsets .only (top: 20.0 ),
409
+ ),
410
+ const Text ('With remote mp4' ),
411
+ Container (
412
+ padding: const EdgeInsets .all (20 ),
413
+ child: NetworkPlayerLifeCycle (
414
+ 'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4' ,
415
+ (BuildContext context,
416
+ VideoPlayerController controller) =>
417
+ AspectRatioVideo (controller),
390
418
),
391
- const Text ('With remote mp4' ),
392
- Container (
393
- padding: const EdgeInsets .all (20 ),
394
- child: NetworkPlayerLifeCycle (
395
- 'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4' ,
419
+ ),
420
+ ],
421
+ ),
422
+ ),
423
+ SingleChildScrollView (
424
+ child: Column (
425
+ children: < Widget > [
426
+ Container (
427
+ padding: const EdgeInsets .only (top: 20.0 ),
428
+ ),
429
+ const Text ('With assets mp4' ),
430
+ Container (
431
+ padding: const EdgeInsets .all (20 ),
432
+ child: AssetPlayerLifeCycle (
433
+ 'assets/Butterfly-209.mp4' ,
396
434
(BuildContext context,
397
435
VideoPlayerController controller) =>
398
- AspectRatioVideo (controller),
399
- ),
400
- ),
401
- ],
402
- ),
436
+ AspectRatioVideo (controller)),
437
+ ),
438
+ ],
403
439
),
404
- SingleChildScrollView (
405
- child: Column (
406
- children: < Widget > [
407
- Container (
408
- padding: const EdgeInsets .only (top: 20.0 ),
409
- ),
410
- const Text ('With assets mp4' ),
411
- Container (
412
- padding: const EdgeInsets .all (20 ),
413
- child: AssetPlayerLifeCycle (
414
- 'assets/Butterfly-209.mp4' ,
415
- (BuildContext context,
416
- VideoPlayerController controller) =>
417
- AspectRatioVideo (controller)),
418
- ),
419
- ],
420
- ),
421
- ),
422
- AssetPlayerLifeCycle (
423
- 'assets/Butterfly-209.mp4' ,
424
- (BuildContext context, VideoPlayerController controller) =>
425
- VideoInListOfCards (controller)),
426
- ],
427
- ),
440
+ ),
441
+ AssetPlayerLifeCycle (
442
+ 'assets/Butterfly-209.mp4' ,
443
+ (BuildContext context, VideoPlayerController controller) =>
444
+ VideoInListOfCards (controller)),
445
+ ],
428
446
),
429
447
),
448
+ );
449
+ }
450
+ }
451
+
452
+ void main () {
453
+ runApp (
454
+ MaterialApp (
455
+ home: App (),
430
456
),
431
457
);
432
458
}
459
+
460
+ class PlayerVideoAndPopPage extends StatefulWidget {
461
+ @override
462
+ _PlayerVideoAndPopPageState createState () => _PlayerVideoAndPopPageState ();
463
+ }
464
+
465
+ class _PlayerVideoAndPopPageState extends State <PlayerVideoAndPopPage > {
466
+ VideoPlayerController _videoPlayerController;
467
+ bool startedPlaying = false ;
468
+
469
+ @override
470
+ void initState () {
471
+ super .initState ();
472
+
473
+ _videoPlayerController =
474
+ VideoPlayerController .asset ('assets/Butterfly-209.mp4' );
475
+ _videoPlayerController.addListener (() {
476
+ if (startedPlaying && ! _videoPlayerController.value.isPlaying) {
477
+ Navigator .pop (context);
478
+ }
479
+ });
480
+ }
481
+
482
+ @override
483
+ void dispose () {
484
+ _videoPlayerController.dispose ();
485
+ super .dispose ();
486
+ }
487
+
488
+ Future <bool > started () async {
489
+ await _videoPlayerController.initialize ();
490
+ await _videoPlayerController.play ();
491
+ startedPlaying = true ;
492
+ return true ;
493
+ }
494
+
495
+ @override
496
+ Widget build (BuildContext context) {
497
+ return Material (
498
+ elevation: 0 ,
499
+ child: Center (
500
+ child: FutureBuilder <bool >(
501
+ future: started (),
502
+ builder: (BuildContext context, AsyncSnapshot <bool > snapshot) {
503
+ if (snapshot.data == true ) {
504
+ return AspectRatio (
505
+ aspectRatio: _videoPlayerController.value.aspectRatio,
506
+ child: VideoPlayer (_videoPlayerController));
507
+ } else {
508
+ return const Text ('waiting for video to load' );
509
+ }
510
+ },
511
+ ),
512
+ ),
513
+ );
514
+ }
515
+ }
0 commit comments