Skip to content
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

Added autocenter to timeline widget #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/src/widgets/time_line_widget/timeline_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TimeLineWidget extends StatefulWidget {
required this.focusedDate,
required this.activeDayTextColor,
required this.activeDayColor,
this.autoCenter = true,
}) : assert(timeLineProps.hPadding > -1,
"Can't set timeline hPadding less than zero."),
assert(timeLineProps.separatorPadding > -1,
Expand Down Expand Up @@ -59,6 +60,11 @@ class TimeLineWidget extends StatefulWidget {
/// The background color of the selected day.
final Color activeDayColor;

/// Automatically centers the selected day in the timeline.
/// If set to `true`, the timeline will automatically scroll to center the selected day.
/// If set to `false`, the timeline will not scroll when the selected day changes.
final bool autoCenter;

@override
State<TimeLineWidget> createState() => _TimeLineWidgetState();
}
Expand Down Expand Up @@ -181,5 +187,13 @@ class _TimeLineWidgetState extends State<TimeLineWidget> {
void _onDayChanged(bool isSelected, DateTime currentDate) {
// A date is selected
widget.onDateChange?.call(currentDate);
// Mantain the selected day in the center of the timeline
if (widget.autoCenter)
_controller.animateTo(
_calculateDateOffset(currentDate) -
(MediaQuery.of(context).size.width - _dayOffsetConstrains) / 2.09,
duration: Duration(milliseconds: 500),
curve: Curves.decelerate,
);
}
}