trimProjectTo method

Duration trimProjectTo(
  1. Duration target
)

Shortens the project to target by trimming the end. One undo step. Returns how much was removed.

Implementation

Duration trimProjectTo(Duration target) {
  final before = totalDuration;
  if (target <= Duration.zero || before <= target) return Duration.zero;
  final kept = <VideoEditorClip>[];
  var at = Duration.zero;
  for (final clip in clips) {
    if (at >= target) break;
    final length = clip.effectiveDuration;
    if (at + length <= target) {
      kept.add(clip);
    } else {
      final remaining = target - at;
      if (clip.isImage) {
        kept.add(clip.copyWith(duration: remaining, endTime: remaining)..markEdited());
      } else {
        final speed = clip.playbackSpeed > 0 ? clip.playbackSpeed : 1.0;
        final start = clip.startTime ?? Duration.zero;
        final end = start + Duration(milliseconds: (remaining.inMilliseconds * speed).round());
        kept.add(clip.copyWith(endTime: end)..markEdited());
      }
    }
    at += length;
  }
  clipController.restore(kept);
  if (_currentGlobalTime.value > totalDuration) {
    _currentGlobalTime.value = totalDuration;
  }
  _saveState();
  notifyListeners();
  _updateVideoPlayerForCurrentClip();
  return before - totalDuration;
}