oc-front/lib/widgets/sheduler_items/scheduler_calendar.dart

179 lines
7.7 KiB
Dart
Raw Normal View History

2024-08-08 08:42:32 +02:00
import 'package:flutter/material.dart';
import 'package:oc_front/core/sections/header/header.dart';
2024-11-08 13:59:22 +01:00
import 'package:oc_front/main.dart';
2024-08-08 08:42:32 +02:00
import 'package:oc_front/models/workflow.dart';
import 'package:oc_front/widgets/sheduler_items/schedule.dart';
import 'package:table_calendar/table_calendar.dart';
// ignore: must_be_immutable
class SchedulerCalendarWidget extends StatefulWidget {
Map<String, List<WorkflowExecution>> data;
DateTime start;
DateTime end;
DateTime focusedDay;
CalendarFormat format = CalendarFormat.month;
bool enabled = true;
ScheduleWidgetState? parent;
SchedulerCalendarWidget ({ super.key,
required this.data,
required this.start,
required this.end,
required this.parent,
required this.focusedDay,
this.enabled = true});
@override SchedulerCalendarWidgetState createState() => SchedulerCalendarWidgetState();
}
class SchedulerCalendarWidgetState extends State<SchedulerCalendarWidget> {
2024-11-13 08:12:37 +01:00
List<Color> colors = [Colors.blue, Colors.orange, redColor, Colors.green, redColor];
List<String> titles = ["SCHEDULED", "RUNNING", "FAILURE", "SUCCESS", "FORGOTTEN"];
2024-08-08 08:42:32 +02:00
bool isEvent(Map<String, List<WorkflowExecution>> data, DateTime day) {
if (data[day.toIso8601String()] == null || data[day.toIso8601String()]!.isEmpty) { return false; }
return true;
}
@override Widget build(BuildContext context) {
widget.focusedDay = widget.focusedDay.isBefore(widget.start) ? widget.start : (
widget.focusedDay.isAfter(widget.end) ? widget.end : widget.focusedDay );
return Container(
padding: const EdgeInsets.only(left: 20, right: 20, bottom: 20),
2024-11-08 13:59:22 +01:00
height: getMainHeight(context) - 50,
2024-08-08 08:42:32 +02:00
child: TableCalendar<Event>(
firstDay: widget.start,
lastDay: widget.end,
focusedDay: widget.focusedDay,
calendarStyle: const CalendarStyle(
2024-10-15 11:28:29 +02:00
markersMaxCount: 2,
2024-08-08 08:42:32 +02:00
markersAnchor: 0,
markersAlignment: Alignment.topCenter
),
selectedDayPredicate: (day) => day == widget.focusedDay,
calendarFormat: widget.format,
calendarBuilders: CalendarBuilders(
markerBuilder: (context, day, events) {
List<Widget> children = [];
for (var ev in events) {
2024-08-22 15:46:16 +02:00
if (children.length == 1 && events.length > 2) {
2024-08-08 08:42:32 +02:00
children.add( InkWell( onTap: () => widget.parent!.setState(() {
2024-08-22 15:46:16 +02:00
selected = day.toIso8601String();
selectedReal = null;
2024-08-08 08:42:32 +02:00
widget.parent!.widget.isDayPlanner = true;
}),
child: Container(
2024-08-22 15:46:16 +02:00
margin: const EdgeInsets.only(bottom: 5),
2024-08-08 08:42:32 +02:00
padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 2),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
2024-11-08 13:59:22 +01:00
color: midColor,
2024-08-08 08:42:32 +02:00
),
child: const Text("...", style: TextStyle(color: Colors.white, fontSize: 10)),
)));
break;
}
children.add(InkWell( onTap: () => widget.parent!.setState(() {
2024-08-22 15:46:16 +02:00
selected = day.toIso8601String();
selectedReal = ev.executionData;
if (selectedReal == null) {
2024-08-08 08:42:32 +02:00
widget.parent!.widget.isDayPlanner = true;
}
}),
child: Container(
2024-10-15 11:28:29 +02:00
margin: const EdgeInsets.only(bottom: 2.5),
2024-08-08 08:42:32 +02:00
padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 2),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: ev.color,
),
child: Text(ev.title.length < 30 ? ev.title : "${ev.title.substring(0, 28)}...", style: const TextStyle(color: Colors.white, fontSize: 10)),
)));
}
return Column(mainAxisAlignment: MainAxisAlignment.center, children: children);
},
defaultBuilder: (context, date, events) => Container(
2024-10-15 11:28:29 +02:00
alignment: !isEvent(widget.data, date) ? Alignment.center : Alignment.topCenter,
2024-08-08 08:42:32 +02:00
margin:const EdgeInsets.all(2.0),
decoration: BoxDecoration(
2024-11-08 13:59:22 +01:00
border: Border.all(color: midColor),
2024-08-08 08:42:32 +02:00
shape: BoxShape.rectangle,
),
child: !isEvent(widget.data, date) ? Text(
date.day.toString(),
style: const TextStyle(color: Colors.grey),
2024-10-15 11:28:29 +02:00
) : Column(
children: [ Container( padding: const EdgeInsets.symmetric(vertical: 5), child: Text(
2024-08-08 08:42:32 +02:00
date.day.toString(),
style: const TextStyle(color: Colors.grey),
)) ])
),
outsideBuilder: (context, day, focusedDay) => Container(
alignment: Alignment.center,
margin: const EdgeInsets.all(2.0),
decoration: BoxDecoration(
2024-11-08 13:59:22 +01:00
border: Border.all(color: midColor),
color: midColor,
2024-08-08 08:42:32 +02:00
shape: BoxShape.rectangle,
),
child: Text(
day.day.toString(),
style: const TextStyle(color: Colors.black),
),
),
selectedBuilder: (context, date, events) => Container(
2024-10-15 11:28:29 +02:00
alignment: !isEvent(widget.data, date) ? Alignment.center : Alignment.topCenter,
2024-08-08 08:42:32 +02:00
margin: const EdgeInsets.all(2.0),
decoration: BoxDecoration(
2024-11-08 13:59:22 +01:00
border: Border.all(color: lightColor, width: 2),
2024-08-08 08:42:32 +02:00
shape: BoxShape.rectangle,
),
child: !isEvent(widget.data, date) ? Text(
date.day.toString(),
style: const TextStyle(color: Colors.grey),
) : Column( children: [ Container( padding: const EdgeInsets.symmetric(vertical: 5), child: Text(
date.day.toString(),
style: const TextStyle(color: Colors.grey),
)) ]),
),
todayBuilder: (context, date, events) => Container(
margin: const EdgeInsets.all(2.0),
2024-10-15 11:28:29 +02:00
alignment: !isEvent(widget.data, date) ? Alignment.center : Alignment.topCenter,
2024-08-08 08:42:32 +02:00
decoration: BoxDecoration(
color: const Color.fromRGBO(38, 166, 154, .5),
shape: BoxShape.rectangle,
border: Border.all(color: Colors.grey),
),
child: Text(
date.day.toString(),
style: const TextStyle(color: Colors.white),
),
),
),
onFormatChanged: (format) => setState(() {
widget.format = format;
}),
onDaySelected: (selectedDay, focusedDay) {
widget.parent!.setState(() {
2024-08-22 15:46:16 +02:00
selected = selectedDay.toIso8601String();
selectedReal = null;
2024-08-08 08:42:32 +02:00
widget.parent!.widget.isDayPlanner = true;
});
},
shouldFillViewport: true,
eventLoader: (day) {
return widget.data[day.toIso8601String()] != null ? widget.data[day.toIso8601String()]!.map((e) {
DateTime dateTime = DateTime.parse(e.executionData!);
2024-10-15 11:28:29 +02:00
return Event("[${dateTime.hour > 9 ? dateTime.hour : "0${dateTime.hour}"}:${dateTime.minute > 9 ? dateTime.minute : "0${dateTime.minute}"}:${dateTime.second > 9 ? dateTime.second : "0${dateTime.second}"}] ${e.name}",
2024-08-08 08:42:32 +02:00
colors[(e.status ?? 1) - 1], e.executionData );
}).toList() : [];
},
));
}
}
class Event {
final String title;
String? executionData;
Color color;
Event(this.title, this.color, this.executionData);
@override
String toString() => title;
}