Files
eatvio_app/lib/features/diets/presentation/widgets/calendar_widget.dart

133 lines
4.5 KiB
Dart

import 'package:eatvio_app/core/utils/string_extentions.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:table_calendar/table_calendar.dart';
class CalendarWidget extends StatefulWidget {
const CalendarWidget({super.key, required this.startDate});
final List<DateTime> startDate;
@override
State<CalendarWidget> createState() => _CalendarWidgetState();
}
class _CalendarWidgetState extends State<CalendarWidget> {
CalendarFormat _calendarFormat = CalendarFormat.week;
late DateTime _focusedDay;
DateTime? _selectedDay;
@override
void initState() {
super.initState();
_selectedDay = widget.startDate.isEmpty ? DateTime.now() : widget.startDate[0];
_focusedDay = widget.startDate.isEmpty ? DateTime.now() : widget.startDate[0];
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(top: 5),
child: TableCalendar(
firstDay: widget.startDate.isEmpty ? DateTime.now() : widget.startDate[0],
lastDay: DateTime.utc(2030, 3, 14),
locale: 'ru_RU',
startingDayOfWeek: StartingDayOfWeek.monday,
focusedDay: _focusedDay,
calendarFormat: _calendarFormat,
calendarBuilders: CalendarBuilders(
headerTitleBuilder: (context, day) {
final dateFormatter = DateFormat('MMMM', 'ru_RU');
return Center(
child: Text(
dateFormatter.format(day).capitalize(),
style: TextStyle(color: appTheme.black, fontSize: 16, fontWeight: FontWeight.w400),
),
);
},
),
headerStyle: const HeaderStyle(
titleCentered: true,
formatButtonVisible: false,
leftChevronVisible: false,
rightChevronVisible: false,
),
rowHeight: 64,
selectedDayPredicate: (day) {
return isSameDay(_selectedDay, day);
},
onDaySelected: (selectedDay, focusedDay) {
if (!isSameDay(_selectedDay, selectedDay)) {
setState(() {
_selectedDay = selectedDay;
_focusedDay = focusedDay;
});
if (widget.startDate.isNotEmpty) {
widget.startDate[0] = selectedDay;
}
}
},
onFormatChanged: (format) {
if (_calendarFormat != format) {
setState(() {
_calendarFormat = format;
});
}
},
onPageChanged: (focusedDay) {
_focusedDay = focusedDay;
},
calendarStyle: CalendarStyle(
outsideDaysVisible: true,
weekendTextStyle: TextStyle(
color: appTheme.gray,
),
defaultTextStyle: TextStyle(
color: appTheme.gray,
),
weekendDecoration: BoxDecoration(
color: appTheme.dividerColor,
shape: BoxShape.circle,
),
defaultDecoration: BoxDecoration(
color: appTheme.dividerColor,
shape: BoxShape.circle,
),
selectedTextStyle: TextStyle(
color: appTheme.whiteA700,
),
selectedDecoration: BoxDecoration(
color: appTheme.blueA400,
shape: BoxShape.circle,
),
outsideTextStyle: TextStyle(
color: appTheme.gray,
),
outsideDecoration: BoxDecoration(
color: appTheme.dividerColor,
shape: BoxShape.circle,
),
withinRangeDecoration: BoxDecoration(
color: appTheme.blueA400,
shape: BoxShape.circle,
),
todayTextStyle: TextStyle(
color: appTheme.gray,
),
todayDecoration: BoxDecoration(
border: Border.all(
color: appTheme.blueA400,
width: 2,
),
color: appTheme.dividerColor,
shape: BoxShape.circle,
),
),
),
),
],
);
}
}