feat:增加数据接口
This commit is contained in:
@@ -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()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
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/widgets/recipe/recipe_card.dart';
|
||||
import 'package:tdesign_flutter/tdesign_flutter.dart';
|
||||
|
||||
class RecipeList extends StatelessWidget {
|
||||
final List<Recipe> recipeList;
|
||||
class RecipeList extends StatefulWidget {
|
||||
const RecipeList({super.key});
|
||||
|
||||
const RecipeList({super.key, required this.recipeList});
|
||||
|
||||
@override
|
||||
State<RecipeList> createState() => _RecipeListState();
|
||||
}
|
||||
|
||||
class _RecipeListState extends State<RecipeList> {
|
||||
List<Recipe> recipeList = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
refreshRecipeList();
|
||||
}
|
||||
|
||||
Future<void> refreshRecipeList() async {
|
||||
final result = await queryRecipeApi(RecipeQuery(category: ""));
|
||||
|
||||
setState(() {
|
||||
recipeList = result;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (recipeList.isEmpty) {
|
||||
return const TDEmpty(
|
||||
type: TDEmptyType.plain,
|
||||
emptyText: '暂无数据',
|
||||
);
|
||||
return const TDEmpty(type: TDEmptyType.plain, emptyText: '暂无数据');
|
||||
} else {
|
||||
return ListView.separated(
|
||||
itemCount: recipeList.length,
|
||||
|
||||
@@ -1,26 +1,71 @@
|
||||
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/widgets/common/index.dart';
|
||||
import 'package:tdesign_flutter/tdesign_flutter.dart';
|
||||
import 'package:timelines_plus/timelines_plus.dart';
|
||||
|
||||
class RecipeTimeline extends StatelessWidget {
|
||||
final List<Record> recordList;
|
||||
class RecipeTimeline extends StatefulWidget {
|
||||
const RecipeTimeline({super.key});
|
||||
|
||||
const RecipeTimeline({super.key, required this.recordList});
|
||||
@override
|
||||
State<StatefulWidget> createState() => _RecipeTimeline();
|
||||
}
|
||||
|
||||
class _RecipeTimeline extends State<RecipeTimeline> {
|
||||
List<Record> recordList = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
refreshRecord();
|
||||
}
|
||||
|
||||
Future<void> refreshRecord() async {
|
||||
final result = await queryRecordApi("2025-01-01", "2025-07-08");
|
||||
setState(() {
|
||||
recordList = result;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Timeline.tileBuilder(
|
||||
theme: TimelineThemeData(nodePosition: 0, indicatorPosition: 0),
|
||||
builder: TimelineTileBuilder.connected(
|
||||
itemCount: recordList.length,
|
||||
connectorBuilder:
|
||||
(context, index, type) => Connector.solidLine(thickness: 2),
|
||||
indicatorBuilder: (context, index) {
|
||||
return Indicator.dot(size: 12.0);
|
||||
},
|
||||
contentsBuilder: (context, index) {
|
||||
return TimelineCard(record: recordList[index]);
|
||||
},
|
||||
return Column(
|
||||
children: [
|
||||
YearSelector(
|
||||
initialYear: DateTime.now().year,
|
||||
minYear: 2000,
|
||||
maxYear: 2100,
|
||||
onYearChanged: (year) {
|
||||
print('选中的年份: $year');
|
||||
},
|
||||
accentColor: Colors.blue,
|
||||
),
|
||||
Expanded(child: TimelineContainer(recordList))
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget TimelineContainer(List<Record> recordList) {
|
||||
if (recordList.isEmpty) {
|
||||
return const TDEmpty(type: TDEmptyType.plain, emptyText: '暂无数据');
|
||||
} else {
|
||||
return Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: Timeline.tileBuilder(
|
||||
theme: TimelineThemeData(nodePosition: 0, indicatorPosition: 0),
|
||||
builder: TimelineTileBuilder.connected(
|
||||
itemCount: recordList.length,
|
||||
connectorBuilder:
|
||||
(context, index, type) => Connector.solidLine(thickness: 2),
|
||||
indicatorBuilder: (context, index) {
|
||||
return Indicator.dot(size: 12.0);
|
||||
},
|
||||
contentsBuilder: (context, index) {
|
||||
return TimelineCard(record: recordList[index]);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -36,7 +81,7 @@ class TimelineCard extends StatelessWidget {
|
||||
return SizedBox(
|
||||
width: double.infinity,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 10, bottom: 10),
|
||||
padding: const EdgeInsets.only(left: 10, bottom: 5),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
@@ -52,12 +97,15 @@ class TimelineCard extends StatelessWidget {
|
||||
child: Column(
|
||||
children: [
|
||||
Text(record.name, style: TextStyle(fontSize: 16)),
|
||||
const SizedBox(height: 10),
|
||||
const SizedBox(height: 5),
|
||||
Image.network(
|
||||
record.imageUrl,
|
||||
'http://172.29.101.108:8100/${record.imageUrl}',
|
||||
width: double.infinity,
|
||||
height: 250,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder:
|
||||
(context, error, stackTrace) =>
|
||||
errorImageContainer(250),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -69,3 +117,132 @@ class TimelineCard extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class YearSelector extends StatefulWidget {
|
||||
final int initialYear;
|
||||
final int? minYear;
|
||||
final int? maxYear;
|
||||
final Function(int) onYearChanged;
|
||||
final Color? accentColor;
|
||||
|
||||
const YearSelector({
|
||||
super.key,
|
||||
required this.initialYear,
|
||||
required this.onYearChanged,
|
||||
this.minYear,
|
||||
this.maxYear,
|
||||
this.accentColor,
|
||||
});
|
||||
|
||||
@override
|
||||
State<YearSelector> createState() => _YearSelectorState();
|
||||
}
|
||||
|
||||
class _YearSelectorState extends State<YearSelector>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late int _currentYear;
|
||||
late Color _accentColor;
|
||||
|
||||
// 用于动画效果
|
||||
late AnimationController _animationController;
|
||||
late Animation<double> _scaleAnimation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_currentYear = widget.initialYear;
|
||||
_accentColor = widget.accentColor ?? Theme
|
||||
.of(context)
|
||||
.primaryColor;
|
||||
|
||||
// 初始化动画控制器
|
||||
_animationController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
);
|
||||
|
||||
// 缩放动画
|
||||
_scaleAnimation = Tween<double>(begin: 1.0, end: 1.1).animate(
|
||||
CurvedAnimation(parent: _animationController, curve: Curves.easeInOut),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_animationController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// 切换到上一年
|
||||
void _previousYear() {
|
||||
if (widget.minYear == null || _currentYear > widget.minYear!) {
|
||||
_animateYearChange(() {
|
||||
setState(() {
|
||||
_currentYear--;
|
||||
});
|
||||
widget.onYearChanged(_currentYear);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// 切换到下一年
|
||||
void _nextYear() {
|
||||
if (widget.maxYear == null || _currentYear < widget.maxYear!) {
|
||||
_animateYearChange(() {
|
||||
setState(() {
|
||||
_currentYear++;
|
||||
});
|
||||
widget.onYearChanged(_currentYear);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// 年份变化时的动画效果
|
||||
void _animateYearChange(VoidCallback onComplete) {
|
||||
_animationController.forward().then((_) {
|
||||
onComplete();
|
||||
_animationController.reverse();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
TDButton(
|
||||
icon: Icons.chevron_left,
|
||||
size: TDButtonSize.small,
|
||||
type: TDButtonType.fill,
|
||||
shape: TDButtonShape.circle,
|
||||
theme: TDButtonTheme.primary,
|
||||
onTap: () => _previousYear(),
|
||||
),
|
||||
SizedBox(width: 10),
|
||||
// 年份显示
|
||||
AnimatedBuilder(
|
||||
animation: _scaleAnimation,
|
||||
builder: (context, child) {
|
||||
return Transform.scale(scale: _scaleAnimation.value, child: child);
|
||||
},
|
||||
child: Text(
|
||||
'$_currentYear',
|
||||
style: TextStyle(
|
||||
fontSize: 28,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: _accentColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
TDButton(
|
||||
icon: Icons.chevron_right,
|
||||
size: TDButtonSize.small,
|
||||
type: TDButtonType.fill,
|
||||
shape: TDButtonShape.circle,
|
||||
theme: TDButtonTheme.primary,
|
||||
onTap: () => _nextYear(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user