forked from eatvio/eatvio_app
163 lines
6.0 KiB
Dart
163 lines
6.0 KiB
Dart
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
|
import 'package:eatvio_app/core/widgets/show_error_dialog.dart';
|
|
import 'package:eatvio_app/features/diary/presentation/bloc/diary_cubit.dart';
|
|
import 'package:eatvio_app/features/diary/presentation/bloc/diary_state.dart';
|
|
import 'package:eatvio_app/generated/l10n.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:table_calendar/table_calendar.dart';
|
|
|
|
class CalendarWidget extends StatefulWidget {
|
|
const CalendarWidget({super.key});
|
|
|
|
@override
|
|
State<CalendarWidget> createState() => _CalendarWidgetState();
|
|
}
|
|
|
|
class _CalendarWidgetState extends State<CalendarWidget> {
|
|
CalendarFormat _calendarFormat = CalendarFormat.week;
|
|
DateTime _focusedDay = DateTime.now();
|
|
DateTime _selectedDay = DateTime.now();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = S.of(context);
|
|
return BlocConsumer<DiaryCubit, DiaryState>(
|
|
listener: (BuildContext context, DiaryState state) {
|
|
if (state is DiaryStateLoaded) {
|
|
setState(() {
|
|
_selectedDay = state.selectedDay;
|
|
});
|
|
}
|
|
},
|
|
builder: (BuildContext context, DiaryState state) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 16),
|
|
child: TableCalendar(
|
|
focusedDay: _focusedDay,
|
|
firstDay: DateTime.utc(2020, 10, 16),
|
|
lastDay: DateTime.utc(2030, 3, 14),
|
|
locale: 'ru_RU',
|
|
startingDayOfWeek: StartingDayOfWeek.monday,
|
|
calendarFormat: _calendarFormat,
|
|
headerStyle: const HeaderStyle(
|
|
titleCentered: true,
|
|
formatButtonVisible: false,
|
|
leftChevronVisible: false,
|
|
rightChevronVisible: false,
|
|
),
|
|
onDaySelected: (selectedDay, focusedDay) async {
|
|
if (!isSameDay(_selectedDay, selectedDay)) {
|
|
final today = DateTime.now();
|
|
if (selectedDay.isAfter(today)) {
|
|
if ((state is DiaryStateLoaded)) {
|
|
final result = await context.read<DiaryCubit>().setSelectedDay(_selectedDay, forDietOnly: true);
|
|
if (result == false && context.mounted) {
|
|
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
|
return;
|
|
}
|
|
setState(() {
|
|
_selectedDay = selectedDay;
|
|
_focusedDay = focusedDay;
|
|
});
|
|
return;
|
|
}
|
|
if (state is DiaryStateLoadedTomorrowWithDiet) {
|
|
final result = await context.read<DiaryCubit>().setSelectedDay(_selectedDay, forDietOnly: true);
|
|
if (result == false && context.mounted) {
|
|
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
|
return;
|
|
}
|
|
setState(() {
|
|
_selectedDay = selectedDay;
|
|
_focusedDay = focusedDay;
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
setState(() {
|
|
_selectedDay = selectedDay;
|
|
_focusedDay = focusedDay;
|
|
});
|
|
context.read<DiaryCubit>().setSelectedDay(_selectedDay);
|
|
}
|
|
},
|
|
selectedDayPredicate: (day) {
|
|
return isSameDay(_selectedDay, day);
|
|
},
|
|
onPageChanged: (focusedDay) {
|
|
setState(() {
|
|
_focusedDay = focusedDay;
|
|
});
|
|
},
|
|
onFormatChanged: (format) {
|
|
if (_calendarFormat != format) {
|
|
setState(() {
|
|
_calendarFormat = format;
|
|
});
|
|
}
|
|
},
|
|
calendarStyle: CalendarStyle(
|
|
weekendDecoration: const BoxDecoration(
|
|
color: Colors.white,
|
|
shape: BoxShape.circle,
|
|
),
|
|
defaultDecoration: const BoxDecoration(
|
|
color: Colors.white,
|
|
shape: BoxShape.circle,
|
|
),
|
|
selectedDecoration: BoxDecoration(
|
|
color: appTheme.blueA400,
|
|
shape: BoxShape.circle,
|
|
),
|
|
todayTextStyle: const TextStyle(
|
|
color: Colors.black,
|
|
),
|
|
todayDecoration: BoxDecoration(
|
|
border: Border.all(
|
|
color: appTheme.blueA400,
|
|
width: 2,
|
|
),
|
|
color: Colors.white,
|
|
shape: BoxShape.circle,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
_arrowDown(),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void _nextCalendarFormat() {
|
|
setState(() {
|
|
_calendarFormat = CalendarFormat.values[(_calendarFormat.index - 1) % CalendarFormat.values.length];
|
|
});
|
|
}
|
|
|
|
GestureDetector _arrowDown() {
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.translucent,
|
|
onVerticalDragStart: (_) {
|
|
_nextCalendarFormat();
|
|
},
|
|
onTap: () {
|
|
_nextCalendarFormat();
|
|
},
|
|
child: Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.only(top: 8, bottom: 16),
|
|
child: Icon(
|
|
_calendarFormat == CalendarFormat.month ? Icons.keyboard_arrow_up_rounded : Icons.keyboard_arrow_down_rounded,
|
|
color: Colors.grey.shade600,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|