Skip to content

Commit

Permalink
clamp buffer and progress within total range (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
suragch committed Aug 24, 2023
1 parent b4327b7 commit f31a754
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [2.0.0] - August 24, 2023

- Bump to Dart 3.0 and Flutter 3.0.
- Fix time label overflow (#60).

## [1.0.1] - March 22, 2023

- Fix String format when Duration is negative (@swiftymf) (#38)
Expand Down
32 changes: 19 additions & 13 deletions lib/audio_video_progress_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -545,15 +545,16 @@ class _RenderProgressBar extends RenderBox {
Duration get progress => _progress;
Duration _progress = Duration.zero;
set progress(Duration value) {
if (_progress == value) {
final clamp = _clampDuration(value);
if (_progress == clamp) {
return;
}
if (_labelLengthDifferent(_progress, value)) {
if (_labelLengthDifferent(_progress, clamp)) {
_clearLabelCache();
}
if (!_userIsDraggingThumb) {
_progress = value;
_thumbValue = _proportionOfTotal(value);
_progress = clamp;
_thumbValue = _proportionOfTotal(clamp);
}
markNeedsPaint();
}
Expand Down Expand Up @@ -614,13 +615,14 @@ class _RenderProgressBar extends RenderBox {
Duration get total => _total;
Duration _total;
set total(Duration value) {
if (_total == value) {
final clamp = (value.isNegative) ? Duration.zero : value;
if (_total == clamp) {
return;
}
if (_labelLengthDifferent(_total, value)) {
if (_labelLengthDifferent(_total, clamp)) {
_clearLabelCache();
}
_total = value;
_total = clamp;
if (!_userIsDraggingThumb) {
_thumbValue = _proportionOfTotal(progress);
}
Expand All @@ -631,13 +633,20 @@ class _RenderProgressBar extends RenderBox {
Duration get buffered => _buffered;
Duration _buffered;
set buffered(Duration value) {
if (_buffered == value) {
final clamp = _clampDuration(value);
if (_buffered == clamp) {
return;
}
_buffered = value;
_buffered = clamp;
markNeedsPaint();
}

Duration _clampDuration(Duration value) {
if (value.isNegative) return Duration.zero;
if (value.compareTo(_total) > 0) return _total;
return value;
}

/// A callback for the audio duration position to where the thumb was moved.
ValueChanged<Duration>? get onSeek => _onSeek;
ValueChanged<Duration>? _onSeek;
Expand Down Expand Up @@ -1050,16 +1059,13 @@ class _RenderProgressBar extends RenderBox {
}

double _proportionOfTotal(Duration duration) {
if (total.inMilliseconds == 0 || duration.isNegative) {
if (total.inMilliseconds == 0) {
return 0.0;
}
return duration.inMilliseconds / total.inMilliseconds;
}

String _getTimeString(Duration time) {
if (time.isNegative) {
return '0:00';
}
final minutes =
time.inMinutes.remainder(Duration.minutesPerHour).toString();
final seconds = time.inSeconds
Expand Down

0 comments on commit f31a754

Please sign in to comment.