forked from eatvio/eatvio_app
feat(issue-1154, issue-851): diary UX changed, price added to a active plan card
This commit is contained in:
@@ -122,7 +122,10 @@ class _DiaryScreenContentState extends State<DiaryScreenContent> {
|
||||
centerTitle: true,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.only(bottom: 16, top: 8),
|
||||
padding: EdgeInsets.only(
|
||||
bottom: 16,
|
||||
top: widget.diaryId == null ? 0 : 8,
|
||||
),
|
||||
physics: const BouncingScrollPhysics(),
|
||||
child: Column(
|
||||
children: [
|
||||
@@ -133,7 +136,12 @@ class _DiaryScreenContentState extends State<DiaryScreenContent> {
|
||||
],
|
||||
if (state is DiaryStateLoaded && _canShowInsights(state))
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(4, 12, 4, 12),
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
4,
|
||||
widget.diaryId == null ? 0 : 12,
|
||||
4,
|
||||
12,
|
||||
),
|
||||
child: _DiaryInsights(
|
||||
slug: state.isConsultantView ? _ownerSlug(state) : null,
|
||||
canAdd: !state.isConsultantView,
|
||||
@@ -325,6 +333,7 @@ class _DiaryInsightsState extends State<_DiaryInsights> {
|
||||
canAdd: widget.canAdd,
|
||||
refreshToken: _refreshToken,
|
||||
initialIntraDate: widget.diaryDate,
|
||||
showShadow: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ 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:material_ui/material_ui.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:table_calendar/table_calendar.dart';
|
||||
|
||||
class CalendarWidget extends StatefulWidget {
|
||||
@@ -15,13 +17,21 @@ class CalendarWidget extends StatefulWidget {
|
||||
State<CalendarWidget> createState() => _CalendarWidgetState();
|
||||
}
|
||||
|
||||
class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
class _CalendarWidgetState extends State<CalendarWidget>
|
||||
with SingleTickerProviderStateMixin {
|
||||
static const _locale = 'ru_RU';
|
||||
static const _kDaysOfWeekHeight = 16.0;
|
||||
static const _kRowHeight = 52.0;
|
||||
static const _dayCellMargin = 6.0;
|
||||
static const _dragHandlePadding = 5.0;
|
||||
|
||||
CalendarFormat _calendarFormat = CalendarFormat.week;
|
||||
late final _expansion = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 180),
|
||||
);
|
||||
DateTime _focusedDay = DateTime.now();
|
||||
DateTime _selectedDay = DateTime.now();
|
||||
|
||||
DateTime? _dayFromState(DiaryState state) {
|
||||
if (state is DiaryStateLoaded) {
|
||||
return state.selectedDay;
|
||||
@@ -35,11 +45,9 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final selected = _dayFromState(context.read<DiaryCubit>().state);
|
||||
if (widget.date != null) {
|
||||
_selectedDay = widget.date!;
|
||||
_focusedDay = widget.date!;
|
||||
} else if (selected != null) {
|
||||
final selected =
|
||||
widget.date ?? _dayFromState(context.read<DiaryCubit>().state);
|
||||
if (selected != null) {
|
||||
_selectedDay = selected;
|
||||
_focusedDay = selected;
|
||||
}
|
||||
@@ -54,13 +62,30 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_expansion.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@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) {
|
||||
final rowHeight = MediaQuery.textScalerOf(context).scale(_kRowHeight);
|
||||
final firstOfMonth = DateTime.utc(_focusedDay.year, _focusedDay.month);
|
||||
final daysInMonth = DateTime.utc(
|
||||
_focusedDay.year,
|
||||
_focusedDay.month + 1,
|
||||
0,
|
||||
).day;
|
||||
final daysBeforeMonth = firstOfMonth.weekday - DateTime.monday;
|
||||
final weekCount = (daysBeforeMonth + daysInMonth + 6) ~/ 7;
|
||||
final focusedWeek = (daysBeforeMonth + _focusedDay.day - 1) ~/ 7;
|
||||
final extraHeight = (weekCount - 1) * rowHeight;
|
||||
final week = _buildDays(rowHeight, CalendarFormat.week);
|
||||
final month = _buildDays(rowHeight, CalendarFormat.month);
|
||||
|
||||
return BlocListener<DiaryCubit, DiaryState>(
|
||||
listener: (context, state) {
|
||||
final selected = _dayFromState(state);
|
||||
if (selected != null) {
|
||||
setState(() {
|
||||
@@ -69,80 +94,167 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
});
|
||||
}
|
||||
},
|
||||
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 cubit = context.read<DiaryCubit>();
|
||||
final canOpenNextDay = await cubit.setSelectedDay(selectedDay);
|
||||
if (!context.mounted || cubit.state is DiaryStateError) {
|
||||
return;
|
||||
}
|
||||
if (!canOpenNextDay) {
|
||||
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,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: AnimatedBuilder(
|
||||
animation: _expansion,
|
||||
child: _buildWeekdays(context),
|
||||
builder: (context, weekdays) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
weekdays!,
|
||||
ClipRect(
|
||||
child: SizedBox(
|
||||
height: rowHeight + extraHeight * _expansion.value,
|
||||
child: OverflowBox(
|
||||
// При раскрытии показываем строки вокруг открытой недели.
|
||||
alignment: Alignment(
|
||||
0,
|
||||
2 * focusedWeek / (weekCount - 1) - 1,
|
||||
),
|
||||
minHeight: 0,
|
||||
maxHeight: double.infinity,
|
||||
child: _expansion.value == 0 ? week : month,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
_arrowDown(),
|
||||
],
|
||||
);
|
||||
},
|
||||
_buildDragHandle(extraHeight),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader(BuildContext context) {
|
||||
final s = S.of(context);
|
||||
final expanded = _expansion.value >= 0.5;
|
||||
final label = DateFormat.yMMMM(_locale).format(_focusedDay);
|
||||
|
||||
return Tooltip(
|
||||
message: expanded ? s.hide : s.expand,
|
||||
child: InkWell(
|
||||
onTap: () => _expansion.value = expanded ? 0 : 1,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(label, style: const HeaderStyle().titleTextStyle),
|
||||
const SizedBox(width: 4),
|
||||
Icon(
|
||||
expanded
|
||||
? Icons.keyboard_arrow_up_rounded
|
||||
: Icons.keyboard_arrow_down_rounded,
|
||||
size: 18,
|
||||
color: appTheme.blueA400,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWeekdays(BuildContext context) {
|
||||
const style = DaysOfWeekStyle();
|
||||
final format = DateFormat.E(_locale);
|
||||
|
||||
return ExcludeSemantics(
|
||||
child: SizedBox(
|
||||
height: _scaledDaysOfWeekHeight(context),
|
||||
child: Row(
|
||||
children: List.generate(DateTime.daysPerWeek, (index) {
|
||||
final day = DateTime.utc(
|
||||
_focusedDay.year,
|
||||
_focusedDay.month,
|
||||
_focusedDay.day - _focusedDay.weekday + DateTime.monday + index,
|
||||
);
|
||||
return Expanded(
|
||||
child: Center(
|
||||
child: Text(
|
||||
format.format(day),
|
||||
style: day.weekday >= DateTime.saturday
|
||||
? style.weekendStyle
|
||||
: style.weekdayStyle,
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDays(double rowHeight, CalendarFormat format) {
|
||||
return TableCalendar(
|
||||
key: ValueKey(format),
|
||||
focusedDay: _focusedDay,
|
||||
firstDay: DateTime.utc(2020, 10, 16),
|
||||
lastDay: DateTime.utc(2030, 3, 14),
|
||||
locale: _locale,
|
||||
startingDayOfWeek: StartingDayOfWeek.monday,
|
||||
calendarFormat: format,
|
||||
availableGestures: AvailableGestures.horizontalSwipe,
|
||||
headerVisible: false,
|
||||
daysOfWeekVisible: false,
|
||||
rowHeight: rowHeight,
|
||||
onDaySelected: _selectDay,
|
||||
selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
|
||||
onPageChanged: (focusedDay) => setState(() => _focusedDay = focusedDay),
|
||||
calendarStyle: CalendarStyle(
|
||||
cellMargin: const EdgeInsets.all(_dayCellMargin),
|
||||
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,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _selectDay(DateTime selectedDay, DateTime focusedDay) async {
|
||||
if (isSameDay(_selectedDay, selectedDay)) return;
|
||||
|
||||
final previousSelectedDay = _selectedDay;
|
||||
final previousFocusedDay = _focusedDay;
|
||||
setState(() {
|
||||
_selectedDay = selectedDay;
|
||||
_focusedDay = focusedDay;
|
||||
});
|
||||
final cubit = context.read<DiaryCubit>();
|
||||
final canOpenNextDay = await cubit.setSelectedDay(selectedDay);
|
||||
if (!mounted || cubit.state is DiaryStateError || canOpenNextDay) return;
|
||||
|
||||
final s = S.of(context);
|
||||
showErrorDialog(
|
||||
context,
|
||||
title: s.want_to_see_tomorrow,
|
||||
description: s.can_not_see_next_day,
|
||||
);
|
||||
setState(() {
|
||||
_selectedDay = previousSelectedDay;
|
||||
_focusedDay = previousFocusedDay;
|
||||
});
|
||||
}
|
||||
|
||||
double _scaledDaysOfWeekHeight(BuildContext context) {
|
||||
final painter = TextPainter(
|
||||
text: TextSpan(text: 'Щy', style: DefaultTextStyle.of(context).style),
|
||||
@@ -155,26 +267,44 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
return height.clamp(_kDaysOfWeekHeight, double.infinity);
|
||||
}
|
||||
|
||||
void _nextCalendarFormat() {
|
||||
setState(() {
|
||||
_calendarFormat = CalendarFormat.values[(_calendarFormat.index - 1) % CalendarFormat.values.length];
|
||||
});
|
||||
}
|
||||
|
||||
GestureDetector _arrowDown() {
|
||||
Widget _buildDragHandle(double extraHeight) {
|
||||
return GestureDetector(
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onVerticalDragStart: (_) {
|
||||
_nextCalendarFormat();
|
||||
behavior: HitTestBehavior.opaque,
|
||||
dragStartBehavior: DragStartBehavior.down,
|
||||
onVerticalDragStart: (_) => _expansion.stop(),
|
||||
onVerticalDragUpdate: (details) {
|
||||
_expansion.value = (_expansion.value + details.delta.dy / extraHeight)
|
||||
.clamp(0.0, 1.0);
|
||||
},
|
||||
onTap: () {
|
||||
_nextCalendarFormat();
|
||||
},
|
||||
child: Container(
|
||||
onVerticalDragEnd: (details) =>
|
||||
_settleExpansion(details.velocity.pixelsPerSecond.dy),
|
||||
onVerticalDragCancel: _settleExpansion,
|
||||
child: SizedBox(
|
||||
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),
|
||||
child: Padding(
|
||||
// Отступ ячеек уже добавляет место над полоской.
|
||||
padding: const EdgeInsets.only(
|
||||
top: _dragHandlePadding,
|
||||
bottom: _dragHandlePadding + _dayCellMargin,
|
||||
),
|
||||
child: Center(
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: appTheme.blueA400,
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
),
|
||||
child: const SizedBox(width: 28, height: 4),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _settleExpansion([double velocity = 0]) {
|
||||
final shouldExpand = velocity.abs() >= 400
|
||||
? velocity > 0
|
||||
: _expansion.value >= 0.5;
|
||||
_expansion.animateTo(shouldExpand ? 1 : 0, curve: Curves.easeOutCubic);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user