Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:habit_tracker/database/habit_database.dart';
- import 'package:habit_tracker/models/habit.dart';
- import 'package:habit_tracker/services/extensions/day.dart';
- import 'package:habit_tracker/services/lists_enums_maps.dart';
- import 'package:habit_tracker/services/providers/day_change_provider.dart';
- import 'package:provider/provider.dart';
- import 'package:table_calendar/table_calendar.dart';
- class DefaultTileBuilder extends StatelessWidget {
- const DefaultTileBuilder({
- super.key,
- required this.day,
- });
- final DateTime day;
- @override
- Widget build(BuildContext context) {
- // ignore: unused_local_variable
- final colorScheme = Theme.of(context).colorScheme;
- final textTheme = Theme.of(context).textTheme;
- 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),
- ),
- );
- }
- }
- class SelectedTileBuilder extends StatelessWidget {
- const SelectedTileBuilder({
- super.key,
- required DateTime? selectedDay,
- required DateTime focusedDay,
- required this.day,
- }) : _selectedDay = selectedDay,
- _focusedDay = focusedDay;
- final DateTime? _selectedDay;
- final DateTime _focusedDay;
- final DateTime day;
- @override
- Widget build(BuildContext context) {
- final colorScheme = Theme.of(context).colorScheme;
- final textTheme = Theme.of(context).textTheme;
- 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),
- ),
- );
- }
- }
- class TodayTileBuilder extends StatelessWidget {
- const TodayTileBuilder({
- super.key,
- required this.day,
- });
- final DateTime day;
- @override
- Widget build(BuildContext context) {
- final colorScheme = Theme.of(context).colorScheme;
- final textTheme = Theme.of(context).textTheme;
- 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,
- ),
- );
- }
- }
- class WeekEndTileBuilder extends StatelessWidget {
- const WeekEndTileBuilder({
- super.key,
- required this.day,
- });
- final DateTime day;
- @override
- Widget build(BuildContext context) {
- // ignore: unused_local_variable
- final colorScheme = Theme.of(context).colorScheme;
- final textTheme = Theme.of(context).textTheme;
- return Container(
- alignment: Alignment.center,
- margin: const EdgeInsets.all(6.0),
- decoration: BoxDecoration(
- color: context.watch<DayChangeProvider>().isDayChanged
- ? Colors.amber
- : returnHeatMapColor(context, day.withoutTime()),
- borderRadius: BorderRadius.circular(8),
- ),
- child: Text(
- "${day.day}",
- style: textTheme.labelMedium!.copyWith(
- color: Colors.grey.shade500,
- ),
- ),
- );
- }
- }
- Map<DateTime, int> prepareHeatMapDataset(List<Habit> habits) {
- final dataset = <DateTime, int>{};
- for (Habit habit in habits) {
- for (DateTime date in habit.completedDays) {
- // normalize date to avoid time mismatch
- final normalizedDate = DateTime(date.year, date.month, date.day);
- // if date already exists in dataset, increment its count
- if (dataset.containsKey(normalizedDate)) {
- dataset[normalizedDate] = dataset[normalizedDate]! + 1;
- }
- // initialize it with a count of 1
- else {
- dataset[normalizedDate] = 1;
- }
- }
- }
- return dataset;
- }
- Color returnHeatMapColor(BuildContext context, DateTime day) {
- final habitDatabase = context.watch<HabitDatabase>();
- final currentHabits = habitDatabase.listOfHabits;
- final dataset = prepareHeatMapDataset(currentHabits);
- final maxInt = heatMapColors.length;
- Color? color;
- if (dataset.containsKey(day)) {
- if (dataset[day]! <= maxInt) {
- color = heatMapColors[dataset[day]];
- } else {
- color = heatMapColors[maxInt];
- }
- } else {
- color = Colors.white;
- }
- return color!;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement