-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmain.dart
262 lines (240 loc) · 8.38 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// ignore_for_file: avoid_print
import 'dart:async';
import 'package:black_hole_flutter/black_hole_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:time/time.dart';
import 'package:timetable/timetable.dart';
// ignore: unused_import
import 'positioning_demo.dart';
import 'utils.dart';
Future<void> main() async {
initDebugOverlay();
runApp(const ExampleApp(child: TimetableExample()));
}
class TimetableExample extends StatefulWidget {
const TimetableExample({super.key});
@override
State<TimetableExample> createState() => _TimetableExampleState();
}
class _TimetableExampleState extends State<TimetableExample>
with TickerProviderStateMixin {
var _visibleDateRange = PredefinedVisibleDateRange.week;
void _updateVisibleDateRange(PredefinedVisibleDateRange newValue) {
setState(() {
_visibleDateRange = newValue;
_dateController.visibleRange = newValue.visibleDateRange;
});
}
bool get _isRecurringLayout =>
_visibleDateRange == PredefinedVisibleDateRange.fixed;
late final _dateController = DateController(
// All parameters are optional.
// initialDate: DateTimeTimetable.today(),
visibleRange: _visibleDateRange.visibleDateRange,
);
final _timeController = TimeController(
// All parameters are optional.
// minDuration: 1.hours,
// maxDuration: 10.hours,
// initialRange: TimeRange(8.hours, 20.hours),
maxRange: TimeRange(0.hours, 24.hours),
);
final _draggedEvents = <BasicEvent>[];
@override
void dispose() {
_timeController.dispose();
_dateController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return TimetableConfig<BasicEvent>(
// Required:
dateController: _dateController,
timeController: _timeController,
eventBuilder: (context, event) => _buildPartDayEvent(event),
// ignore: sort_child_properties_last
child: Column(
children: [
_buildAppBar(),
Expanded(
child: _isRecurringLayout
? RecurringMultiDateTimetable<BasicEvent>()
: MultiDateTimetable<BasicEvent>(),
),
],
),
// Optional:
eventProvider: eventProviderFromFixedList(positioningDemoEvents),
allDayEventBuilder: (context, event, info) => BasicAllDayEventWidget(
event,
info: info,
onTap: () => _showSnackBar('All-day event $event tapped'),
),
timeOverlayProvider: mergeTimeOverlayProviders([
positioningDemoOverlayProvider,
(context, date) => _draggedEvents
.map(
(it) =>
it.toTimeOverlay(date: date, widget: BasicEventWidget(it)),
)
.nonNulls
.toList(),
]),
callbacks: TimetableCallbacks(
onWeekTap: (week) {
_showSnackBar('Tapped on week $week.');
_updateVisibleDateRange(PredefinedVisibleDateRange.week);
unawaited(
_dateController.animateTo(
week.getDayOfWeek(DateTime.monday),
vsync: this,
),
);
},
onDateTap: (date) {
_showSnackBar('Tapped on date $date.');
unawaited(_dateController.animateTo(date, vsync: this));
},
onDateBackgroundTap: (date) =>
_showSnackBar('Tapped on date background at $date.'),
onDateTimeBackgroundTap: (dateTime) =>
_showSnackBar('Tapped on date-time background at $dateTime.'),
onMultiDateHeaderOverflowTap: (date) =>
_showSnackBar('Tapped on the overflow of $date.'),
),
theme: TimetableThemeData(
context,
// startOfWeek: DateTime.monday,
// dateDividersStyle: DateDividersStyle(
// context,
// color: Colors.blue.withOpacity(.3),
// width: 2,
// ),
// nowIndicatorStyle: NowIndicatorStyle(
// context,
// lineColor: Colors.green,
// shape: TriangleNowIndicatorShape(color: Colors.green),
// ),
// timeIndicatorStyleProvider: (time) => TimeIndicatorStyle(
// context,
// time,
// alwaysUse24HourFormat: false,
// ),
),
);
}
Widget _buildPartDayEvent(BasicEvent event) {
final roundedTo = 15.minutes;
return PartDayDraggableEvent(
onDragStart: () => setState(() => _draggedEvents.add(event)),
onDragUpdate: (dateTime) => setState(() {
dateTime = dateTime.roundTimeToMultipleOf(roundedTo);
final index = _draggedEvents.indexWhere((it) => it.id == event.id);
final oldEvent = _draggedEvents[index];
_draggedEvents[index] = oldEvent.copyWith(
start: dateTime,
end: dateTime + oldEvent.duration,
);
}),
onDragEnd: (dateTime) {
dateTime = (dateTime ?? event.start).roundTimeToMultipleOf(roundedTo);
setState(() => _draggedEvents.removeWhere((it) => it.id == event.id));
_showSnackBar('Dragged event to $dateTime.');
},
onDragCanceled: (isMoved) => _showSnackBar('Your finger moved: $isMoved'),
child: BasicEventWidget(
event,
onTap: () => _showSnackBar('Part-day event $event tapped'),
),
);
}
Widget _buildAppBar() {
final colorScheme = context.theme.colorScheme;
Widget child = AppBar(
elevation: 0,
titleTextStyle: TextStyle(color: colorScheme.onSurface),
iconTheme: IconThemeData(color: colorScheme.onSurface),
systemOverlayStyle: SystemUiOverlayStyle.light,
backgroundColor: Colors.transparent,
title: _isRecurringLayout
? null
: MonthIndicator.forController(_dateController),
actions: [
IconButton(
icon: const Icon(Icons.today),
onPressed: () {
unawaited(_dateController.animateToToday(vsync: this));
unawaited(_timeController.animateToShowFullDay(vsync: this));
},
tooltip: 'Go to today',
),
const SizedBox(width: 8),
DropdownButton(
onChanged: (visibleRange) => _updateVisibleDateRange(visibleRange!),
value: _visibleDateRange,
items: [
for (final visibleRange in PredefinedVisibleDateRange.values)
DropdownMenuItem(
value: visibleRange,
child: Text(visibleRange.title),
),
],
),
const SizedBox(width: 16),
],
);
if (!_isRecurringLayout) {
child = Column(
children: [
child,
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Builder(
builder: (context) {
return DefaultTimetableCallbacks(
callbacks: DefaultTimetableCallbacks.of(context)!.copyWith(
onDateTap: (date) {
_showSnackBar('Tapped on date $date.');
_updateVisibleDateRange(PredefinedVisibleDateRange.day);
unawaited(_dateController.animateTo(date, vsync: this));
},
),
child: CompactMonthTimetable(),
);
},
),
),
],
);
}
return Material(color: colorScheme.surface, elevation: 4, child: child);
}
void _showSnackBar(String content) =>
context.scaffoldMessenger.showSnackBar(SnackBar(content: Text(content)));
}
enum PredefinedVisibleDateRange { day, threeDays, workWeek, week, fixed }
extension on PredefinedVisibleDateRange {
VisibleDateRange get visibleDateRange {
return switch (this) {
PredefinedVisibleDateRange.day => VisibleDateRange.days(1),
PredefinedVisibleDateRange.threeDays => VisibleDateRange.days(3),
PredefinedVisibleDateRange.workWeek => VisibleDateRange.weekAligned(5),
PredefinedVisibleDateRange.week => VisibleDateRange.week(),
PredefinedVisibleDateRange.fixed => VisibleDateRange.fixed(
DateTimeTimetable.today(),
DateTime.daysPerWeek,
),
};
}
String get title {
return switch (this) {
PredefinedVisibleDateRange.day => 'Day',
PredefinedVisibleDateRange.threeDays => '3 Days',
PredefinedVisibleDateRange.workWeek => 'Work Week',
PredefinedVisibleDateRange.week => 'Week',
PredefinedVisibleDateRange.fixed => '7 Days (fixed)',
};
}
}