forked from eatvio/eatvio_app
161 lines
5.9 KiB
Dart
161 lines
5.9 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, this.date});
|
|
final DateTime? date;
|
|
|
|
@override
|
|
State<CalendarWidget> createState() => _CalendarWidgetState();
|
|
}
|
|
|
|
class _CalendarWidgetState extends State<CalendarWidget> {
|
|
static const _kDaysOfWeekHeight = 16.0;
|
|
static const _kRowHeight = 52.0;
|
|
|
|
CalendarFormat _calendarFormat = CalendarFormat.week;
|
|
DateTime _focusedDay = DateTime.now();
|
|
DateTime _selectedDay = DateTime.now();
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (widget.date != null) {
|
|
_selectedDay = widget.date!;
|
|
_focusedDay = widget.date!;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(CalendarWidget oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.date != null && widget.date != oldWidget.date) {
|
|
_selectedDay = widget.date!;
|
|
_focusedDay = widget.date!;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = S.of(context);
|
|
final textScaler = MediaQuery.textScalerOf(context);
|
|
final daysOfWeekHeight = _scaledDaysOfWeekHeight(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,
|
|
daysOfWeekHeight: daysOfWeekHeight,
|
|
rowHeight: textScaler.scale(_kRowHeight),
|
|
headerStyle: const HeaderStyle(titleCentered: true, formatButtonVisible: false, leftChevronVisible: false, rightChevronVisible: false),
|
|
onDaySelected: (selectedDay, focusedDay) async {
|
|
if (!isSameDay(_selectedDay, selectedDay)) {
|
|
final previousSelectedDay = _selectedDay;
|
|
final previousFocusedDay = _selectedDay;
|
|
setState(() {
|
|
_selectedDay = selectedDay;
|
|
_focusedDay = focusedDay;
|
|
});
|
|
final canOpenNextDay = await context.read<DiaryCubit>().setSelectedDay(selectedDay);
|
|
if (!canOpenNextDay && context.mounted) {
|
|
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
|
setState(() {
|
|
_selectedDay = previousSelectedDay;
|
|
_focusedDay = previousFocusedDay;
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
},
|
|
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(),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
double _scaledDaysOfWeekHeight(BuildContext context) {
|
|
final painter = TextPainter(
|
|
text: TextSpan(text: 'Щy', style: DefaultTextStyle.of(context).style),
|
|
textDirection: Directionality.of(context),
|
|
textScaler: MediaQuery.textScalerOf(context),
|
|
maxLines: 1,
|
|
)..layout();
|
|
final height = painter.height;
|
|
painter.dispose();
|
|
return height.clamp(_kDaysOfWeekHeight, double.infinity);
|
|
}
|
|
|
|
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),
|
|
),
|
|
);
|
|
}
|
|
}
|