progress_bar.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import 'package:flutter/material.dart';
  2. class ProgressBar extends StatelessWidget {
  3. final double max;
  4. final double current;
  5. final double buffered;
  6. final Color backgroundColor;
  7. final Color bufferColor;
  8. final Color playedColor;
  9. const ProgressBar({
  10. Key key,
  11. @required this.max,
  12. @required this.current,
  13. this.buffered,
  14. this.backgroundColor = const Color(0xFF616161),
  15. this.bufferColor = Colors.grey,
  16. this.playedColor = Colors.white,
  17. }) : super(key: key);
  18. @override
  19. Widget build(BuildContext context) {
  20. if (max == null || current == null) return _buildEmpty();
  21. var left = current / max;
  22. var mid = (buffered ?? 0) / max - left;
  23. if (mid < 0) {
  24. mid = 0;
  25. }
  26. var right = 1 - left - mid;
  27. var progress = buildProgress(left, mid, right);
  28. return progress;
  29. }
  30. _buildEmpty() {
  31. return Container();
  32. }
  33. Widget buildProgress(double left, double mid, double right) {
  34. return Row(
  35. children: <Widget>[
  36. buildColorWidget(playedColor, left),
  37. buildColorWidget(bufferColor, mid),
  38. buildColorWidget(backgroundColor, right),
  39. ],
  40. );
  41. }
  42. Widget buildColorWidget(Color color, double flex) {
  43. if (flex == double.nan ||
  44. flex == double.infinity ||
  45. flex == double.negativeInfinity) {
  46. flex = 0;
  47. }
  48. return Expanded(
  49. flex: (flex * 1000).toInt(),
  50. child: Container(
  51. color: color,
  52. ),
  53. );
  54. }
  55. }