player_with_controls.dart 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:video_player/video_player.dart';
  4. import 'dart:async';
  5. import 'dart:ui';
  6. import 'package:chewie_example/material_controls.dart';
  7. import 'package:chewie_example/cupertino_controls.dart';
  8. class PlayerWithControls extends StatefulWidget {
  9. final VideoPlayerController controller;
  10. final Future<dynamic> Function() onExpandCollapse;
  11. final bool fullScreen;
  12. final double aspectRatio;
  13. PlayerWithControls(
  14. {@required this.controller,
  15. @required this.onExpandCollapse,
  16. @required this.aspectRatio,
  17. this.fullScreen = false});
  18. @override
  19. State createState() {
  20. return new _VideoPlayerWithControlsState();
  21. }
  22. }
  23. class _VideoPlayerWithControlsState extends State<PlayerWithControls> {
  24. @override
  25. Widget build(BuildContext context) {
  26. final controller = widget.controller;
  27. return new Center(
  28. child: new Container(
  29. width: MediaQuery.of(context).size.width,
  30. child: new AspectRatio(
  31. aspectRatio: widget.aspectRatio,
  32. child: new Container(
  33. child: new Stack(
  34. children: <Widget>[
  35. new Hero(
  36. tag: controller,
  37. child: new Center(
  38. child: new AspectRatio(
  39. aspectRatio: widget.aspectRatio,
  40. child: new VideoPlayer(controller)),
  41. ),
  42. ),
  43. Theme.of(context).platform == TargetPlatform.android
  44. ? new MaterialControls(
  45. controller: controller,
  46. onExpandCollapse: widget.onExpandCollapse,
  47. fullScreen: widget.fullScreen,
  48. )
  49. : new CupertinoControls(
  50. backgroundColor: new Color.fromRGBO(41, 41, 41, 0.7),
  51. iconColor: new Color.fromARGB(255, 200, 200, 200),
  52. controller: controller,
  53. onExpandCollapse: widget.onExpandCollapse,
  54. fullScreen: widget.fullScreen,
  55. ),
  56. ],
  57. ),
  58. ),
  59. ),
  60. ),
  61. );
  62. }
  63. @override
  64. void initState() {
  65. // Hack to show the video when it starts playing. Should be fixed by the
  66. // Plugin IMO.
  67. widget.controller.addListener(_onPlay);
  68. super.initState();
  69. }
  70. void _onPlay() {
  71. if (widget.controller.value.isPlaying) {
  72. setState(() {
  73. widget.controller.removeListener(_onPlay);
  74. });
  75. }
  76. }
  77. }