Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:habit_tracker/models/day.dart';
- import 'package:habit_tracker/services/habit_util.dart';
- import 'package:habit_tracker/services/providers/day_change_provider.dart';
- import 'package:provider/provider.dart';
- import 'package:table_calendar/table_calendar.dart';
- class MyTableCalendar extends StatefulWidget {
- final DateTime myFirstDay;
- const MyTableCalendar({
- super.key,
- required this.myFirstDay,
- });
- @override
- State<MyTableCalendar> createState() => _MyTableCalendarState();
- }
- class _MyTableCalendarState extends State<MyTableCalendar> {
- // variables
- final DateTime _focusedDay = DateTime.now();
- DateTime? _selectedDay;
- bool formOverride = false;
- CalendarFormat calendarFormat = CalendarFormat.month;
- // methods
- void _onDaySelected(DateTime selectedDay, DateTime focusedDay) {
- if (!isSameDay(_selectedDay, selectedDay)) {
- setState(() {
- _selectedDay = selectedDay;
- //_focusedDay = focusedDay;
- debugPrint(
- "IS SAME DAY CHECK: ${isSameDay(_selectedDay, _focusedDay)}");
- debugPrint(
- "IS SAME DAY CHECK: Selected day is - ${_selectedDay!.withoutTime()} ; Focused day is - ${_focusedDay.withoutTime()}");
- });
- }
- }
- void _onFormatChanged(CalendarFormat format) {
- if (calendarFormat != format) {
- setState(() {
- calendarFormat = format;
- });
- }
- }
- @override
- Widget build(BuildContext context) {
- ColorScheme colorScheme = Theme.of(context).colorScheme;
- TextTheme textTheme = Theme.of(context).textTheme;
- bool smallMobile = MediaQuery.sizeOf(context).height < 700;
- if (smallMobile && !formOverride) {
- calendarFormat = CalendarFormat.twoWeeks;
- formOverride = true;
- }
- return Container(
- padding: const EdgeInsets.all(4),
- decoration: BoxDecoration(
- border: Border.all(
- color: colorScheme.outline,
- ),
- borderRadius: BorderRadius.circular(16),
- color: colorScheme.primaryContainer,
- ),
- // start of TabelCalendar
- child: TableCalendar(
- rowHeight: 44,
- focusedDay: _focusedDay,
- firstDay: widget.myFirstDay,
- lastDay: DateTime.utc(2030, 12, 16),
- currentDay: Day.now(),
- startingDayOfWeek: StartingDayOfWeek.monday,
- selectedDayPredicate: (day) {
- return isSameDay(_selectedDay, day);
- },
- onDaySelected: _onDaySelected,
- calendarFormat: calendarFormat,
- onFormatChanged: _onFormatChanged,
- // calendar styling section
- calendarStyle: CalendarStyle(
- tablePadding: const EdgeInsets.all(8),
- outsideDecoration: BoxDecoration(
- shape: BoxShape.rectangle,
- borderRadius: BorderRadius.circular(12),
- ),
- ),
- headerStyle: const HeaderStyle(
- formatButtonShowsNext: false,
- leftChevronIcon: Icon(Icons.chevron_left_rounded),
- rightChevronIcon: Icon(Icons.chevron_right_rounded),
- ),
- // builders
- calendarBuilders: CalendarBuilders(
- defaultBuilder: (context, day, focusedDay) {
- // if it's the weekend
- if (day.weekday == DateTime.saturday ||
- day.weekday == DateTime.sunday) {
- return Container(
- alignment: Alignment.center,
- margin: const EdgeInsets.all(6.0),
- decoration: BoxDecoration(
- color: Provider.of<DayChangeProvider>(context).dayChanged
- ? Colors.amber
- : returnHeatMapColor(context, day.withoutTime()),
- borderRadius: BorderRadius.circular(8),
- ),
- child: Text(
- "${day.day}",
- style: textTheme.labelMedium!.copyWith(
- color: Colors.grey.shade500,
- ),
- ),
- );
- }
- // if not the weekend
- else {
- return Container(
- alignment: Alignment.center,
- margin: const EdgeInsets.all(6.0),
- decoration: BoxDecoration(
- color: returnHeatMapColor(context, day.withoutTime()),
- borderRadius: BorderRadius.circular(8),
- ),
- child: Text(
- "${day.day}",
- style: textTheme.labelMedium!
- .copyWith(color: colorScheme.onSecondaryContainer),
- ),
- );
- }
- },
- // today builder
- todayBuilder: (context, day, focusedDay) {
- return Container(
- alignment: Alignment.center,
- margin: const EdgeInsets.all(6.0),
- decoration: BoxDecoration(
- color: returnHeatMapColor(context, day.withoutTime()),
- border: Border.all(
- color: colorScheme.outline,
- width: 2,
- ),
- borderRadius: BorderRadius.circular(8),
- ),
- child: Text(
- "${day.day}",
- style: textTheme.labelMedium,
- ),
- );
- },
- // selected day builder
- selectedBuilder: (context, day, focusedDay) {
- return Container(
- alignment: Alignment.center,
- margin: const EdgeInsets.all(6.0),
- decoration: BoxDecoration(
- color: returnHeatMapColor(context, day.withoutTime()),
- shape: BoxShape.rectangle,
- border: isSameDay(_selectedDay, _focusedDay)
- ? Border.all(color: colorScheme.outline, width: 1.5)
- : Border.all(color: Colors.transparent),
- borderRadius: BorderRadius.circular(8),
- boxShadow: <BoxShadow>[
- BoxShadow(
- offset: const Offset(0, 1.2),
- blurRadius: 3,
- color: Colors.grey.shade400.withOpacity(0.7),
- )
- ]),
- child: Text(
- "${day.day}",
- style: textTheme.labelMedium!
- .copyWith(color: colorScheme.onSecondaryContainer),
- ),
- );
- },
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement