-
Notifications
You must be signed in to change notification settings - Fork 315
✨ Fixes issue #331: Add auto-scroll to current time for Day, Week, and Multi-Day views #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lavigarg-simform
wants to merge
1
commit into
fix/scroll_exception_issue
Choose a base branch
from
feature/add-auto-scroll-to-current-time
base: fix/scroll_exception_issue
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright (c) 2021 Simform Solutions. All rights reserved. | ||
| // Use of this source code is governed by a MIT-style license | ||
| // that can be found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
|
|
||
| import 'zoom_scroll_controller.dart'; | ||
|
|
||
| /// Provides scroll-to-current-time functionality shared by DayView, WeekView, | ||
| /// and MultiDayView state classes. | ||
| mixin ScrollToCurrentTimeMixin<W extends StatefulWidget> on State<W> { | ||
| // --- Abstract interface (implemented by the host state) --- | ||
|
|
||
| /// Returns the [currentTimeProvider] from the view's | ||
| /// [LiveTimeIndicatorSettings], or null to use [DateTime.now]. | ||
| DateTime Function()? get currentTimeProvider; | ||
|
|
||
| /// First hour shown in the timeline (0–23). | ||
| int get viewStartHour; | ||
|
|
||
| /// Last hour shown in the timeline (1–24). | ||
| int get viewEndHour; | ||
|
|
||
| /// Pixels per minute used to calculate scroll offsets. | ||
| double get viewHeightPerMinute; | ||
|
|
||
| /// Currently active scroll controller, or null if not yet attached. | ||
| ZoomScrollController? get activeScrollController; | ||
|
|
||
| /// Called by [jumpToCurrentTime] so the host can persist the new offset | ||
| /// (e.g. update _lastScrollOffset and _pageOffsets[_currentIndex]). | ||
| void onCurrentTimeJumped(double offset); | ||
|
|
||
| // --- Shared implementation --- | ||
|
|
||
| int _currentTimeScrollAttempts = 0; | ||
|
|
||
| /// Current time, honoring [currentTimeProvider] when provided. | ||
| DateTime get currentTime => currentTimeProvider?.call() ?? DateTime.now(); | ||
|
|
||
| /// Top-aligned pixel offset of [time] within the visible timeline range. | ||
| /// | ||
| /// Accounts for [viewStartHour]/[viewEndHour] and clamps the result so that | ||
| /// times outside the range map to the nearest edge. | ||
| double offsetForTime(DateTime time) { | ||
| final minutesFromStart = (time.hour - viewStartHour) * 60 + time.minute; | ||
| final visibleMinutes = (viewEndHour - viewStartHour) * 60; | ||
| // Guard against an inverted range (endHour < startHour). Asserts that are | ||
| // meant to prevent this are stripped in release builds, and clamp() throws | ||
| // when its lower bound exceeds the upper bound, so fail gracefully instead. | ||
| if (visibleMinutes <= 0) return 0; | ||
| final clampedMinutes = minutesFromStart.clamp(0, visibleMinutes); | ||
| return viewHeightPerMinute * clampedMinutes; | ||
| } | ||
|
|
||
| double? _currentTimeScrollOffset({required bool center}) { | ||
| final controller = activeScrollController; | ||
| if (controller == null || !controller.hasClients) return null; | ||
| final position = controller.position; | ||
| var offset = offsetForTime(currentTime); | ||
| if (center) offset -= position.viewportDimension / 2; | ||
| return offset.clamp( | ||
| position.minScrollExtent, | ||
| position.maxScrollExtent, | ||
| ); | ||
| } | ||
|
|
||
| /// Centers the current time once the scrollable is ready. | ||
| /// | ||
| /// Retries for a few frames if the controller is not attached yet, then | ||
| /// gives up to avoid an endless frame-scheduling loop. | ||
| void scrollToCurrentTimeAfterLayout() { | ||
| if (!mounted) return; | ||
| final controller = activeScrollController; | ||
| if (controller == null || !controller.hasClients) { | ||
| if (_currentTimeScrollAttempts++ >= 5) return; | ||
| WidgetsBinding.instance.addPostFrameCallback( | ||
| (_) => scrollToCurrentTimeAfterLayout(), | ||
| ); | ||
| return; | ||
| } | ||
| jumpToCurrentTime(); | ||
| } | ||
|
|
||
| /// Instantly positions the timeline so the current time is visible. | ||
| /// | ||
| /// When [center] is true (default) the current time is placed at the | ||
| /// vertical center of the viewport; otherwise it aligns to the top. | ||
| /// Does nothing if the scrollable is not yet attached. | ||
| void jumpToCurrentTime({bool center = true}) { | ||
| final offset = _currentTimeScrollOffset(center: center); | ||
| if (offset == null) return; | ||
| onCurrentTimeJumped(offset); | ||
| activeScrollController?.jumpTo(offset); | ||
| } | ||
|
|
||
| /// Animates the timeline so that the current time becomes visible. | ||
| /// | ||
| /// When [center] is true (default) the current time is positioned at the | ||
| /// vertical center of the viewport; otherwise it aligns to the top. The | ||
| /// target is clamped to the scrollable range. Does nothing if the view has | ||
| /// not been laid out yet. | ||
| Future<void> animateToCurrentTime({ | ||
| bool center = true, | ||
| Duration duration = const Duration(milliseconds: 200), | ||
| Curve curve = Curves.linear, | ||
| }) async { | ||
| final offset = _currentTimeScrollOffset(center: center); | ||
| if (offset == null) return; | ||
| final controller = activeScrollController; | ||
| if (controller == null || !controller.hasClients) return; | ||
| // Persist the target so a rebuild mid-animation (which seeds the page from | ||
| // the stored offset) doesn't reset the position, matching jumpToCurrentTime. | ||
| onCurrentTimeJumped(offset); | ||
| await controller.animateTo(offset, duration: duration, curve: curve); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.