chewie_player.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter/widgets.dart';
  5. import 'package:chewie_example/player_with_controls.dart';
  6. import 'dart:async';
  7. import 'package:video_player/video_player.dart';
  8. class ChewiePlayer extends StatefulWidget {
  9. final VideoPlayerController controller;
  10. final bool autoPlay;
  11. final bool looping;
  12. final double aspectRatio;
  13. ChewiePlayer({
  14. Key key,
  15. @required this.controller,
  16. this.aspectRatio,
  17. this.autoPlay = false,
  18. this.looping = false,
  19. })
  20. : super(key: key);
  21. @override
  22. State<StatefulWidget> createState() {
  23. return new _ChewiePlayerState();
  24. }
  25. }
  26. class _ChewiePlayerState extends State<ChewiePlayer> {
  27. @override
  28. Widget build(BuildContext context) {
  29. return new PlayerWithControls(
  30. controller: widget.controller,
  31. onExpandCollapse: () {
  32. return _pushFullScreenWidget(context);
  33. },
  34. aspectRatio: widget.aspectRatio ?? _calculateAspectRatio(context));
  35. }
  36. @override
  37. void initState() {
  38. _initialize();
  39. super.initState();
  40. }
  41. _buildFullScreenVideo(BuildContext context, Animation<double> animation) {
  42. return new Scaffold(
  43. resizeToAvoidBottomPadding: false,
  44. body: new Container(
  45. color: Colors.black,
  46. child: new PlayerWithControls(
  47. controller: widget.controller,
  48. onExpandCollapse: () => new Future.value(Navigator.of(context).pop()),
  49. aspectRatio: widget.aspectRatio,
  50. fullScreen: true,
  51. ),
  52. ),
  53. );
  54. }
  55. Widget _fullScreenRoutePageBuilder(
  56. BuildContext context,
  57. Animation<double> animation,
  58. Animation<double> secondaryAnimation,
  59. ) {
  60. return new AnimatedBuilder(
  61. animation: animation,
  62. builder: (BuildContext context, Widget child) {
  63. return _buildFullScreenVideo(context, animation);
  64. },
  65. );
  66. }
  67. Future _initialize() async {
  68. await widget.controller.setLooping(widget.looping);
  69. await widget.controller.initialize();
  70. if (widget.autoPlay) {
  71. await widget.controller.play();
  72. }
  73. }
  74. Future<dynamic> _pushFullScreenWidget(BuildContext context) {
  75. final TransitionRoute<Null> route = new PageRouteBuilder<Null>(
  76. settings: new RouteSettings(isInitialRoute: false),
  77. pageBuilder: _fullScreenRoutePageBuilder,
  78. );
  79. SystemChrome.setEnabledSystemUIOverlays([]);
  80. return Navigator.of(context).push(route).then((_) {
  81. SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
  82. });
  83. }
  84. double _calculateAspectRatio(BuildContext context) {
  85. final size = MediaQuery.of(context).size;
  86. final width = size.width;
  87. final height = size.height;
  88. return width > height ? width / height : height / width;
  89. }
  90. }