chewie_player.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. @required this.aspectRatio,
  17. this.autoPlay = false,
  18. this.looping = false,
  19. }) : super(key: key);
  20. @override
  21. State<StatefulWidget> createState() {
  22. return new _ChewiePlayerState();
  23. }
  24. }
  25. class _ChewiePlayerState extends State<ChewiePlayer> {
  26. @override
  27. Widget build(BuildContext context) {
  28. return new PlayerWithControls(
  29. controller: widget.controller,
  30. onExpandCollapse: () {
  31. return _pushFullScreenWidget(context);
  32. },
  33. aspectRatio: widget.aspectRatio,
  34. );
  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. }