feat:增加数据接口

This commit is contained in:
2025-07-18 17:06:47 +08:00
parent 05fdbf9462
commit 65958d925e
7 changed files with 421 additions and 75 deletions

View File

@@ -1,4 +1,7 @@
import 'package:flutter/material.dart';
import 'package:food_hub_app/api/recipe.dart';
import 'package:food_hub_app/models/recipe.dart';
import 'package:food_hub_app/utils/date_util.dart';
import 'package:food_hub_app/utils/index.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
@@ -14,7 +17,41 @@ class _RecipeCalendarState extends State<RecipeCalendar> {
DateTime _selectedDay = DateTime.now();
DateTime _focusedDay = DateTime.now();
List<Record> recordList = [];
List<Record> selectRecordList = [];
@override
void initState() {
super.initState();
refreshRecord();
}
Future<void> refreshRecord() async {
final result = await queryRecordApi(
getFirstDayOfMonth(_focusedDay),
getLastDayOfMonth(_focusedDay),
);
setState(() {
recordList = result;
if (recordList.isNotEmpty) {
_selectedDay = DateTime.parse(recordList.last.date);
} else {
_selectedDay = _focusedDay;
}
refreshSelectRecord();
});
}
void refreshSelectRecord() {
setState(() {
selectRecordList =
recordList
.where((record) => record.date == formatDateTime(_selectedDay))
.toList();
});
}
TableCalendar recipeCalendar() {
return TableCalendar(
@@ -32,13 +69,23 @@ class _RecipeCalendarState extends State<RecipeCalendar> {
setState(() {
_selectedDay = selectedDay;
_focusedDay = focusedDay;
refreshSelectRecord();
});
}
},
onPageChanged: (focusedDay) {
print("cxx");
_focusedDay = focusedDay;
refreshRecord();
},
// 自定义日期单元格构建器
calendarBuilders: CalendarBuilders(
defaultBuilder:
(context, day, focusedDay) => _buildDateWidget(day, false, false),
selectedBuilder:
(context, day, focusedDay) => _buildDateWidget(day, true, false),
todayBuilder:
(context, day, focusedDay) => _buildDateWidget(day, false, true),
),
);
}
@@ -52,17 +99,27 @@ class _RecipeCalendarState extends State<RecipeCalendar> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(formatDateTime(_selectedDay, 'MM月dd日 EEEE')),
recipeRecordItem(),
if (selectRecordList.isEmpty)
TDEmpty(type: TDEmptyType.plain, emptyText: '暂无数据')
else
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: selectRecordList.length,
itemBuilder: (context, index) {
return recipeRecordItem(selectRecordList[index]);
},
),
],
),
),
);
}
Widget recipeRecordItem() {
Widget recipeRecordItem(Record record) {
return Card(
elevation: 0,
color: Colors.limeAccent.shade100,
color: Color(0xFFF5F5DC),
child: Padding(
padding: EdgeInsets.all(10),
child: Row(
@@ -70,7 +127,7 @@ class _RecipeCalendarState extends State<RecipeCalendar> {
TDAvatar(
size: TDAvatarSize.medium,
type: TDAvatarType.customText,
text: 'A',
text: record.category[0],
),
SizedBox(width: 10),
Expanded(
@@ -80,11 +137,11 @@ class _RecipeCalendarState extends State<RecipeCalendar> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"青椒土豆丝",
record.name,
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: 5),
Text("家常菜", style: TextStyle(color: Colors.grey)),
Text(record.category, style: TextStyle(color: Colors.grey)),
],
),
),
@@ -101,13 +158,57 @@ class _RecipeCalendarState extends State<RecipeCalendar> {
);
}
Widget _buildDateWidget(DateTime day, bool isSelected, bool isToday) {
// 检查当前日期是否在需要显示红点的列表中
bool shouldShowRedDot = recordList.any(
(item) => item.date == formatDateTime(day),
);
return Container(
width: 40,
height: 40,
margin: EdgeInsets.all(2),
decoration: BoxDecoration(
color:
isSelected
? Colors.blue
: isToday
? Colors.grey[200]
: Colors.transparent,
shape: BoxShape.circle,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
// 日期数字
Text(
day.day.toString(),
style: TextStyle(color: isSelected ? Colors.white : Colors.black87),
),
// 底部红点 - 只在指定日期显示
if (shouldShowRedDot)
Container(
margin: EdgeInsets.only(top: 2),
width: 6,
height: 6,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Card(elevation: 0, color: Colors.white, child: recipeCalendar()),
const SizedBox(height: 10),
dailyItem(),
Expanded(child: dailyItem()),
],
);
}