import 'package:intl/intl.dart'; import 'package:flutter/material.dart'; import 'package:syncfusion_flutter_datagrid/datagrid.dart'; import 'package:workinghours/data/publicHolidays.dart'; class Booking { DateTime bookingDay; DateTime come1; DateTime leave1; DateTime come2; DateTime leave2; DateTime come3; DateTime leave3; DateTime come4; DateTime leave4; DateTime come5; DateTime leave5; String code; String information; int weekday = -1; String holiday = ''; String dayLeft = ''; String dayRight = ''; String pause = ''; String durationIs = ''; String durationShould = ''; String diff = ''; String currentDiff = ''; String decimal = ''; Booking({ required this.bookingDay, required this.come1, required this.leave1, required this.come2, required this.leave2, required this.come3, required this.leave3, required this.come4, required this.leave4, required this.come5, required this.leave5, required this.code, required this.information, }) { PublicHolidays h = PublicHolidays(year: bookingDay.year); weekday = bookingDay.weekday; holiday = h.holiday(bookingDay); dayLeft = DateFormat('EEEE - dd').format(bookingDay); dayRight = DateFormat('dd - EEEE').format(bookingDay); pause = ''; durationIs = ''; durationShould = ''; diff = ''; currentDiff = ''; decimal = ''; } // factory Booking.fromJson(Map json) { // return Movie( // movieID: int.parse(json['movieID']), // movieTitle: json['movieTitle'] as String, // overview: json['overview'] as String, // releaseDate: DateTime.parse(json['releaseDate']), // state: int.parse(json['state']), // resolution: json['resolution'] as String, // ); // } } class BookingDataSource extends DataGridSource { /// Creates the employee data source class with required details. BookingDataSource(this.booking) { buildDataGridRow(); } void buildDataGridRow() { _bookingDataGridRows = booking .map((e) => DataGridRow(cells: [ DataGridCell(columnName: 'code', value: e.code), DataGridCell(columnName: 'weekday', value: e.weekday), DataGridCell(columnName: 'dayLeft', value: e.dayLeft), DataGridCell(columnName: 'holiday', value: e.holiday), DataGridCell( columnName: 'come1', value: e.come1.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.come1)), DataGridCell( columnName: 'leave1', value: e.leave1.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.leave1)), DataGridCell( columnName: 'come2', value: e.come2.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.come2)), DataGridCell( columnName: 'leave2', value: e.leave2.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.leave2)), DataGridCell( columnName: 'come3', value: e.come3.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.come3)), DataGridCell( columnName: 'leave3', value: e.leave3.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.leave3)), DataGridCell( columnName: 'come4', value: e.come4.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.come4)), DataGridCell( columnName: 'leave4', value: e.leave4.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.leave4)), DataGridCell( columnName: 'come5', value: e.come5.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.come5)), DataGridCell( columnName: 'leave5', value: e.leave5.year == 1900 ? '99:99' : DateFormat('hh:mm').format(e.leave5)), DataGridCell(columnName: 'pause', value: e.pause), DataGridCell(columnName: 'code', value: e.code), DataGridCell( columnName: 'durationIs', value: e.durationIs), DataGridCell( columnName: 'durationShould', value: e.durationShould), DataGridCell(columnName: 'diff', value: e.diff), DataGridCell( columnName: 'information', value: e.information), DataGridCell( columnName: 'currentDiff', value: e.currentDiff), DataGridCell(columnName: 'decimal', value: e.decimal), DataGridCell(columnName: 'dayRight', value: e.dayRight), ])) .toList(); } List booking = []; List _bookingDataGridRows = []; @override List get rows => _bookingDataGridRows; @override DataGridRowAdapter buildRow(DataGridRow row) { Color colBG = Colors.white; String code = row.getCells().toList()[0].value; int weekday = row.getCells().toList()[1].value; String holiday = row.getCells().toList()[3].value; if (code == 'U') { colBG = Color.fromARGB(255, 127, 127, 255); } if (weekday == 6 || weekday == 7) { colBG = Color.fromARGB(255, 255, 165, 0); } if (holiday != '') { colBG = Color.fromARGB(255, 255, 165, 0); } if (code == 'T') { colBG = Color.fromARGB(255, 127, 127, 255); } if (code == 'SU') { colBG = Color.fromARGB(255, 127, 127, 255); } if (code == 'K') { colBG = Colors.yellow; } if (code == 'G') { colBG = Color.fromARGB(255, 191, 191, 255); } if (code == 'T') { colBG = Colors.red; } return DataGridRowAdapter( cells: row.getCells().map((e) { if (e.columnName == 'dayRight' || e.columnName == 'holiday' || e.columnName == 'information') { return Container( alignment: Alignment.centerLeft, color: colBG, padding: const EdgeInsets.all(8.0), child: Text(e.value.toString() == '99:99' ? '' : e.value.toString()), ); } return Container( alignment: Alignment.centerRight, color: colBG, padding: const EdgeInsets.all(8.0), child: Text(e.value.toString() == '99:99' ? '' : e.value.toString()), ); }).toList()); } void updateDataGrid() { notifyListeners(); } }