player_with_controls.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:async';
  2. import 'dart:ui';
  3. import 'package:chewie/src/chewie_progress_colors.dart';
  4. import 'package:chewie/src/cupertino_controls.dart';
  5. import 'package:chewie/src/material_controls.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:video_player/video_player.dart';
  9. class PlayerWithControls extends StatefulWidget {
  10. final VideoPlayerController controller;
  11. final Future<dynamic> Function() onExpandCollapse;
  12. final bool fullScreen;
  13. final ChewieProgressColors cupertinoProgressColors;
  14. final ChewieProgressColors materialProgressColors;
  15. final Widget placeholder;
  16. final double aspectRatio;
  17. final bool autoPlay;
  18. final bool showControls;
  19. final bool isLive;
  20. PlayerWithControls({
  21. Key key,
  22. @required this.controller,
  23. @required this.onExpandCollapse,
  24. @required this.aspectRatio,
  25. this.fullScreen = false,
  26. this.showControls = true,
  27. this.cupertinoProgressColors,
  28. this.materialProgressColors,
  29. this.placeholder,
  30. this.autoPlay,
  31. this.isLive = false,
  32. }) : super(key: key);
  33. @override
  34. State createState() {
  35. return new _VideoPlayerWithControlsState();
  36. }
  37. }
  38. class _VideoPlayerWithControlsState extends State<PlayerWithControls> {
  39. @override
  40. Widget build(BuildContext context) {
  41. final controller = widget.controller;
  42. return new Center(
  43. child: new Container(
  44. width: MediaQuery.of(context).size.width,
  45. child: new AspectRatio(
  46. aspectRatio: widget.aspectRatio,
  47. child: _buildPlayerWithControls(controller, context),
  48. ),
  49. ),
  50. );
  51. }
  52. Container _buildPlayerWithControls(
  53. VideoPlayerController controller, BuildContext context) {
  54. return new Container(
  55. child: new Stack(
  56. children: <Widget>[
  57. widget.placeholder ?? new Container(),
  58. new Center(
  59. child: new Hero(
  60. tag: controller,
  61. child: new AspectRatio(
  62. aspectRatio: widget.aspectRatio,
  63. child: new VideoPlayer(controller),
  64. ),
  65. ),
  66. ),
  67. _buildControls(context, controller),
  68. ],
  69. ),
  70. );
  71. }
  72. Widget _buildControls(
  73. BuildContext context,
  74. VideoPlayerController controller,
  75. ) {
  76. return widget.showControls
  77. ? Theme.of(context).platform == TargetPlatform.android
  78. ? new MaterialControls(
  79. controller: controller,
  80. onExpandCollapse: widget.onExpandCollapse,
  81. fullScreen: widget.fullScreen,
  82. progressColors: widget.materialProgressColors,
  83. autoPlay: widget.autoPlay,
  84. isLive: widget.isLive,
  85. )
  86. : new CupertinoControls(
  87. backgroundColor: new Color.fromRGBO(41, 41, 41, 0.7),
  88. iconColor: new Color.fromARGB(255, 200, 200, 200),
  89. controller: controller,
  90. onExpandCollapse: widget.onExpandCollapse,
  91. fullScreen: widget.fullScreen,
  92. progressColors: widget.cupertinoProgressColors,
  93. autoPlay: widget.autoPlay,
  94. isLive: widget.isLive,
  95. )
  96. : new Container();
  97. }
  98. @override
  99. void initState() {
  100. // Hack to show the video when it starts playing. Should be fixed by the
  101. // Plugin IMO.
  102. widget.controller.addListener(_onPlay);
  103. super.initState();
  104. }
  105. @override
  106. void didUpdateWidget(PlayerWithControls oldWidget) {
  107. super.didUpdateWidget(oldWidget);
  108. if (widget.controller.dataSource != oldWidget.controller.dataSource) {
  109. widget.controller.addListener(_onPlay);
  110. }
  111. }
  112. @override
  113. dispose() {
  114. widget.controller.removeListener(_onPlay);
  115. super.dispose();
  116. }
  117. void _onPlay() {
  118. if (widget.controller.value.isPlaying) {
  119. setState(() {
  120. widget.controller.removeListener(_onPlay);
  121. });
  122. }
  123. }
  124. }