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

feat: ✨ Added show time on live timeline #217

Merged
Changes from 2 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
49 changes: 49 additions & 0 deletions example/lib/widgets/day_view_widget.dart
Original file line number Diff line number Diff line change
@@ -18,6 +18,17 @@ class DayViewWidget extends StatelessWidget {
return DayView<Event>(
key: state,
width: width,
startDuration: Duration(hours: 8),
showHalfHours: true,
heightPerMinute: 3,
timeLineBuilder: _timeLineBuilder,
hourIndicatorSettings: HourIndicatorSettings(
color: Theme.of(context).dividerColor,
),
halfHourIndicatorSettings: HourIndicatorSettings(
color: Theme.of(context).dividerColor,
lineStyle: LineStyle.dashed,
),
verticalLineOffset: 0,
timeLineWidth: 65,
showLiveTimeLineInAllDays: true,
@@ -29,4 +40,42 @@ class DayViewWidget extends StatelessWidget {
),
);
}

Widget _timeLineBuilder(DateTime date) {
if (date.minute != 0) {
return Stack(
clipBehavior: Clip.none,
children: [
Positioned.fill(
top: -8,
right: 8,
child: Text(
"${date.hour}:${date.minute}",
textAlign: TextAlign.right,
style: TextStyle(
color: Colors.black.withAlpha(50),
fontStyle: FontStyle.italic,
fontSize: 12,
),
),
),
],
);
}

final hour = ((date.hour - 1) % 12) + 1;
return Stack(
clipBehavior: Clip.none,
children: [
Positioned.fill(
top: -8,
right: 8,
child: Text(
"$hour ${date.hour ~/ 12 == 0 ? "am" : "pm"}",
textAlign: TextAlign.right,
),
),
],
);
}
}
72 changes: 30 additions & 42 deletions lib/src/components/_internal_components.dart
Original file line number Diff line number Diff line change
@@ -127,24 +127,24 @@ class TimeLine extends StatefulWidget {
/// This will display time string in timeline.
final DateWidgetBuilder timeLineBuilder;

/// Flag to display half hours.
final bool showHalfHours;

/// settings for time line. Defines color, extra offset,
/// height of indicator and also allow to show time with custom format.
final LiveTimeIndicatorSettings liveTimeIndicatorSettings;

static DateTime get _date => DateTime.now();

double get _halfHourHeight => hourHeight / 2;

/// Time line to display time at left side of day or week view.
const TimeLine(
{Key? key,
required this.timeLineWidth,
required this.hourHeight,
required this.height,
required this.timeLineOffset,
required this.timeLineBuilder,
required this.liveTimeIndicatorSettings})
: super(key: key);
const TimeLine({
Key? key,
required this.timeLineWidth,
required this.hourHeight,
required this.height,
required this.timeLineOffset,
required this.timeLineBuilder,
required this.liveTimeIndicatorSettings,
this.showHalfHours = false,
}) : super(key: key);

@override
State<TimeLine> createState() => _TimeLineState();
@@ -153,10 +153,14 @@ class TimeLine extends StatefulWidget {
class _TimeLineState extends State<TimeLine> {
late Timer _timer;
late TimeOfDay _currentTime;
late DateTime _date;
late double _halfHourHeight;

@override
void initState() {
super.initState();
_date = DateTime.now();
_halfHourHeight = widget.hourHeight / 2;
_currentTime = widget.liveTimeIndicatorSettings.showTime
? TimeOfDay.now()
: TimeOfDay(hour: 0, minute: 16);
@@ -196,41 +200,25 @@ class _TimeLineState extends State<TimeLine> {
child: Stack(
children: [
for (int i = 1; i < Constants.hoursADay; i++)

/// To avoid overlap of live time line indicator, show timeline when
/// current min is less than 45 min and is previous hour or
/// current min is greater than 15 min and is current hour
/// To avoid overlap of live time line indicator with timeline.
/// Timeline will be hidden for below scenario
/// Eg: Between 1:45 and 2:15
Visibility(
visible:
!((_currentTime.minute >= 45 && _currentTime.hour == i - 1) ||
(_currentTime.minute <= 15 && _currentTime.hour == i)),
child: Positioned(
top: widget.hourHeight * i - widget.timeLineOffset,
left: 0,
right: 0,
bottom: widget.height -
(widget.hourHeight * (i + 1)) +
widget.timeLineOffset,
child: Container(
height: widget.hourHeight,
width: widget.timeLineWidth,
child: widget.timeLineBuilder.call(
DateTime(
TimeLine._date.year,
TimeLine._date.month,
TimeLine._date.day,
i,
),
),
),
child: _timelinePositioned(
topPosition: widget.hourHeight * i - widget.timeLineOffset,
bottomPosition: widget.height - (widget.hourHeight * (i + 1)) + widget.timeLineOffset,
hour: i,
),
),
if (showHalfHours)
if (widget.showHalfHours)
for (int i = 0; i < Constants.hoursADay; i++)
_timelinePositioned(
topPosition: hourHeight * i - timeLineOffset + _halfHourHeight,
topPosition: widget.hourHeight * i - widget.timeLineOffset + _halfHourHeight,
bottomPosition:
height - (hourHeight * (i + 1)) + timeLineOffset,
widget.height - (widget.hourHeight * (i + 1)) + widget.timeLineOffset,
hour: i,
minutes: 30,
),
@@ -251,9 +239,9 @@ class _TimeLineState extends State<TimeLine> {
right: 0,
bottom: bottomPosition,
child: Container(
height: hourHeight,
width: timeLineWidth,
child: timeLineBuilder.call(
height: widget.hourHeight,
width: widget.timeLineWidth,
child: widget.timeLineBuilder.call(
DateTime(
_date.year,
_date.month,
1 change: 1 addition & 0 deletions lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
@@ -261,6 +261,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
late EventArranger<T> _eventArranger;

late HourIndicatorSettings _hourIndicatorSettings;
late HourIndicatorSettings _halfHourIndicatorSettings;
late LiveTimeIndicatorSettings _liveTimeIndicatorSettings;

late PageController _pageController;
20 changes: 20 additions & 0 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
@@ -165,6 +165,26 @@ extension MinutesExtension on MinuteSlotSize {
}
}

extension MyList on List<CalendarEventData> {
// Below function will add the new event in sorted manner(startTimeWise) in
// the existing list of CalendarEventData.
void addEventInSortedManner(CalendarEventData event) {
var addIndex = -1;
for (var i = 0; i < this.length; i++) {
if (event.startTime!.compareTo(this[i].startTime!) <= 0) {
addIndex = i;
break;
}
}

if (addIndex > -1) {
this.insert(addIndex, event);
} else {
this.add(event);
}
}
}

extension IntExtension on int {
String appendLeadingZero() {
return toString().padLeft(2, '0');