You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemm-flutter-app/lib/widgets/my_video_progress_indicator...

106 lines
2.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class MyVideoProgressIndicator extends StatefulWidget {
const MyVideoProgressIndicator(
this.controller, {
Key? key,
this.colors = const VideoProgressColors(),
required this.allowScrubbing,
this.padding = const EdgeInsets.only(top: 5.0),
}) : super(key: key);
final VideoPlayerController controller;
final VideoProgressColors colors;
final bool allowScrubbing;
final EdgeInsets padding;
@override
State<MyVideoProgressIndicator> createState() => _VideoProgressIndicatorState();
}
class _VideoProgressIndicatorState extends State<MyVideoProgressIndicator> {
_VideoProgressIndicatorState() {
listener = () {
if (!mounted) {
return;
}
setState(() {});
};
}
late VoidCallback listener;
VideoPlayerController get controller => widget.controller;
VideoProgressColors get colors => widget.colors;
@override
void initState() {
super.initState();
controller.addListener(listener);
}
@override
void deactivate() {
controller.removeListener(listener);
super.deactivate();
}
@override
Widget build(BuildContext context) {
Widget progressIndicator;
if (controller.value.isInitialized) {
int duration = controller.value.duration.inMilliseconds;
int position = controller.value.position.inMilliseconds;
int maxBuffering = 0;
for (DurationRange range in controller.value.buffered) {
int end = range.end.inMilliseconds;
if (end > maxBuffering) {
maxBuffering = end;
}
}
progressIndicator = Stack(
fit: StackFit.passthrough,
children: <Widget>[
LinearProgressIndicator(
value: maxBuffering / duration,
valueColor: AlwaysStoppedAnimation<Color>(colors.bufferedColor),
backgroundColor: colors.backgroundColor,
minHeight: 8,
),
LinearProgressIndicator(
value: position / duration,
valueColor: AlwaysStoppedAnimation<Color>(colors.playedColor),
backgroundColor: Colors.transparent,
minHeight: 8,
),
],
);
} else {
progressIndicator = LinearProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(colors.playedColor),
backgroundColor: colors.backgroundColor,
minHeight: 8,
);
}
Widget paddedProgressIndicator = Padding(
padding: widget.padding,
child: progressIndicator,
);
if (widget.allowScrubbing) {
return VideoScrubber(
controller: controller,
child: paddedProgressIndicator,
);
} else {
return paddedProgressIndicator;
}
}
}