Advertisement
harmonyV

Before

Jul 26th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.47 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:habit_tracker/models/day.dart';
  3. import 'package:habit_tracker/services/habit_util.dart';
  4. import 'package:habit_tracker/services/providers/day_change_provider.dart';
  5. import 'package:provider/provider.dart';
  6. import 'package:table_calendar/table_calendar.dart';
  7.  
  8. class MyTableCalendar extends StatefulWidget {
  9.   final DateTime myFirstDay;
  10.  
  11.   const MyTableCalendar({
  12.     super.key,
  13.     required this.myFirstDay,
  14.   });
  15.  
  16.   @override
  17.   State<MyTableCalendar> createState() => _MyTableCalendarState();
  18. }
  19.  
  20. class _MyTableCalendarState extends State<MyTableCalendar> {
  21.   // variables
  22.   final DateTime _focusedDay = DateTime.now();
  23.   DateTime? _selectedDay;
  24.  
  25.   bool formOverride = false;
  26.   CalendarFormat calendarFormat = CalendarFormat.month;
  27.  
  28.   // methods
  29.   void _onDaySelected(DateTime selectedDay, DateTime focusedDay) {
  30.     if (!isSameDay(_selectedDay, selectedDay)) {
  31.       setState(() {
  32.         _selectedDay = selectedDay;
  33.         //_focusedDay = focusedDay;
  34.         debugPrint(
  35.             "IS SAME DAY CHECK: ${isSameDay(_selectedDay, _focusedDay)}");
  36.         debugPrint(
  37.             "IS SAME DAY CHECK: Selected day is - ${_selectedDay!.withoutTime()} ; Focused day is - ${_focusedDay.withoutTime()}");
  38.       });
  39.     }
  40.   }
  41.  
  42.   void _onFormatChanged(CalendarFormat format) {
  43.     if (calendarFormat != format) {
  44.       setState(() {
  45.         calendarFormat = format;
  46.       });
  47.     }
  48.   }
  49.  
  50.   @override
  51.   Widget build(BuildContext context) {
  52.     ColorScheme colorScheme = Theme.of(context).colorScheme;
  53.     TextTheme textTheme = Theme.of(context).textTheme;
  54.  
  55.  
  56.     bool smallMobile = MediaQuery.sizeOf(context).height < 700;
  57.  
  58.     if (smallMobile && !formOverride) {
  59.       calendarFormat = CalendarFormat.twoWeeks;
  60.       formOverride = true;
  61.     }
  62.  
  63.     return Container(
  64.       padding: const EdgeInsets.all(4),
  65.       decoration: BoxDecoration(
  66.         border: Border.all(
  67.           color: colorScheme.outline,
  68.         ),
  69.         borderRadius: BorderRadius.circular(16),
  70.         color: colorScheme.primaryContainer,
  71.       ),
  72.  
  73.       // start of TabelCalendar
  74.       child: TableCalendar(
  75.         rowHeight: 44,
  76.         focusedDay: _focusedDay,
  77.         firstDay: widget.myFirstDay,
  78.         lastDay: DateTime.utc(2030, 12, 16),
  79.         currentDay: Day.now(),
  80.         startingDayOfWeek: StartingDayOfWeek.monday,
  81.         selectedDayPredicate: (day) {
  82.           return isSameDay(_selectedDay, day);
  83.         },
  84.         onDaySelected: _onDaySelected,
  85.         calendarFormat: calendarFormat,
  86.         onFormatChanged: _onFormatChanged,
  87.  
  88.         // calendar styling section
  89.         calendarStyle: CalendarStyle(
  90.           tablePadding: const EdgeInsets.all(8),
  91.           outsideDecoration: BoxDecoration(
  92.             shape: BoxShape.rectangle,
  93.             borderRadius: BorderRadius.circular(12),
  94.           ),
  95.         ),
  96.         headerStyle: const HeaderStyle(
  97.           formatButtonShowsNext: false,
  98.           leftChevronIcon: Icon(Icons.chevron_left_rounded),
  99.           rightChevronIcon: Icon(Icons.chevron_right_rounded),
  100.         ),
  101.  
  102.         // builders
  103.         calendarBuilders: CalendarBuilders(
  104.           defaultBuilder: (context, day, focusedDay) {
  105.             // if it's the weekend
  106.             if (day.weekday == DateTime.saturday ||
  107.                 day.weekday == DateTime.sunday) {
  108.               return Container(
  109.                 alignment: Alignment.center,
  110.                 margin: const EdgeInsets.all(6.0),
  111.                 decoration: BoxDecoration(
  112.                   color: Provider.of<DayChangeProvider>(context).dayChanged
  113.                       ? Colors.amber
  114.                       : returnHeatMapColor(context, day.withoutTime()),
  115.                   borderRadius: BorderRadius.circular(8),
  116.                 ),
  117.                 child: Text(
  118.                   "${day.day}",
  119.                   style: textTheme.labelMedium!.copyWith(
  120.                     color: Colors.grey.shade500,
  121.                   ),
  122.                 ),
  123.               );
  124.             }
  125.             // if not the weekend
  126.             else {
  127.               return Container(
  128.                 alignment: Alignment.center,
  129.                 margin: const EdgeInsets.all(6.0),
  130.                 decoration: BoxDecoration(
  131.                   color: returnHeatMapColor(context, day.withoutTime()),
  132.                   borderRadius: BorderRadius.circular(8),
  133.                 ),
  134.                 child: Text(
  135.                   "${day.day}",
  136.                   style: textTheme.labelMedium!
  137.                       .copyWith(color: colorScheme.onSecondaryContainer),
  138.                 ),
  139.               );
  140.             }
  141.           },
  142.           // today builder
  143.           todayBuilder: (context, day, focusedDay) {
  144.             return Container(
  145.               alignment: Alignment.center,
  146.               margin: const EdgeInsets.all(6.0),
  147.               decoration: BoxDecoration(
  148.                 color: returnHeatMapColor(context, day.withoutTime()),
  149.                 border: Border.all(
  150.                   color: colorScheme.outline,
  151.                   width: 2,
  152.                 ),
  153.                 borderRadius: BorderRadius.circular(8),
  154.               ),
  155.               child: Text(
  156.                 "${day.day}",
  157.                 style: textTheme.labelMedium,
  158.               ),
  159.             );
  160.           },
  161.           // selected day builder
  162.           selectedBuilder: (context, day, focusedDay) {
  163.             return Container(
  164.               alignment: Alignment.center,
  165.               margin: const EdgeInsets.all(6.0),
  166.               decoration: BoxDecoration(
  167.                   color: returnHeatMapColor(context, day.withoutTime()),
  168.                   shape: BoxShape.rectangle,
  169.                   border: isSameDay(_selectedDay, _focusedDay)
  170.                       ? Border.all(color: colorScheme.outline, width: 1.5)
  171.                       : Border.all(color: Colors.transparent),
  172.                   borderRadius: BorderRadius.circular(8),
  173.                   boxShadow: <BoxShadow>[
  174.                     BoxShadow(
  175.                       offset: const Offset(0, 1.2),
  176.                       blurRadius: 3,
  177.                       color: Colors.grey.shade400.withOpacity(0.7),
  178.                     )
  179.                   ]),
  180.               child: Text(
  181.                 "${day.day}",
  182.                 style: textTheme.labelMedium!
  183.                     .copyWith(color: colorScheme.onSecondaryContainer),
  184.               ),
  185.             );
  186.           },
  187.         ),
  188.       ),
  189.     );
  190.   }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement