player_with_controls.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'dart:ui';
  2. import 'package:chewie/src/chewie_player.dart';
  3. import 'package:chewie/src/cupertino_controls.dart';
  4. import 'package:chewie/src/material_controls.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:video_player/video_player.dart';
  8. class PlayerWithControls extends StatelessWidget {
  9. PlayerWithControls({Key key}) : super(key: key);
  10. @override
  11. Widget build(BuildContext context) {
  12. final ChewieController chewieController = ChewieController.of(context);
  13. return Center(
  14. child: Container(
  15. width: MediaQuery.of(context).size.width,
  16. child: AspectRatio(
  17. aspectRatio:
  18. chewieController.aspectRatio ?? _calculateAspectRatio(context),
  19. child: _buildPlayerWithControls(chewieController, context),
  20. ),
  21. ),
  22. );
  23. }
  24. Container _buildPlayerWithControls(
  25. ChewieController chewieController, BuildContext context) {
  26. return Container(
  27. child: Stack(
  28. children: <Widget>[
  29. chewieController.placeholder ?? Container(),
  30. Center(
  31. child: AspectRatio(
  32. aspectRatio: chewieController.aspectRatio ??
  33. _calculateAspectRatio(context),
  34. child: VideoPlayer(chewieController.videoPlayerController),
  35. ),
  36. ),
  37. chewieController.overlay ?? Container(),
  38. _buildControls(context, chewieController),
  39. ],
  40. ),
  41. );
  42. }
  43. Widget _buildControls(
  44. BuildContext context,
  45. ChewieController chewieController,
  46. ) {
  47. return chewieController.showControls
  48. ? chewieController.customControls != null
  49. ? chewieController.customControls
  50. : Theme.of(context).platform == TargetPlatform.android
  51. ? MaterialControls()
  52. : CupertinoControls(
  53. backgroundColor: Color.fromRGBO(41, 41, 41, 0.7),
  54. iconColor: Color.fromARGB(255, 200, 200, 200),
  55. )
  56. : Container();
  57. }
  58. double _calculateAspectRatio(BuildContext context) {
  59. final size = MediaQuery.of(context).size;
  60. final width = size.width;
  61. final height = size.height;
  62. return width > height ? width / height : height / width;
  63. }
  64. }