feat:增加变化提醒功能

This commit is contained in:
2025-12-03 14:28:39 +08:00
parent e672442d6a
commit 8d23f06f74
10 changed files with 194 additions and 68 deletions

View File

@@ -1,4 +1,6 @@
import 'package:fitnote/utils/record_utils.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
Widget buildCardTitle({
required BuildContext context,
@@ -45,3 +47,40 @@ BoxDecoration buildBoxDecoration(BuildContext context) {
),
);
}
Widget buildProgressItem({
required String title,
required String current,
required String target,
required String unit,
required double progress,
required Color color,
}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(title),
Text(
'$current/$target $unit',
style: TextStyle(fontWeight: FontWeight.w500, color: color),
),
],
),
const SizedBox(height: 8),
GFProgressBar(
animation: true,
lineHeight: 20,
percentage: progress,
progressBarColor: color,
child: Text(
'${roundNum(progress * 100)}%',
textAlign: TextAlign.end,
style: TextStyle(color: Colors.white),
),
),
],
);
}

View File

@@ -7,7 +7,6 @@ import 'package:fitnote/widget/record/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common/widget/common_widget.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:getwidget/getwidget.dart';
import 'package:provider/provider.dart';
class GoalProgress extends StatefulWidget {
@@ -48,11 +47,11 @@ class _GoalProgressState extends State<GoalProgress> {
icon: FontAwesomeIcons.trophy,
),
const SizedBox(height: 28),
_buildProgressItem(
buildProgressItem(
title: '目标体重',
current: provider.latestWeight.toString(),
target: AppConfig.goalWeight.toString(),
unit: 'kg',
unit: RecordType.weight.unit,
progress: calWeightProgress(
provider.latestWeight,
AppConfig.goalWeight,
@@ -61,22 +60,22 @@ class _GoalProgressState extends State<GoalProgress> {
color: RecordType.weight.color,
),
const SizedBox(height: 28),
_buildProgressItem(
buildProgressItem(
title: '运动总时长',
current: provider.totalSpot.toString(),
target: AppConfig.goalTotalSpot.toString(),
unit: '分钟',
unit: RecordType.sport.unit,
progress: formatProgress(
provider.totalSpot / AppConfig.goalTotalSpot,
),
color: RecordType.sport.color,
),
const SizedBox(height: 28),
_buildProgressItem(
buildProgressItem(
title: '日均睡眠',
current: provider.avgSleep.toString(),
target: AppConfig.goalAvgSleep.toString(),
unit: '小时',
unit: RecordType.sleep.unit,
progress: formatProgress(
provider.avgSleep / AppConfig.goalAvgSleep,
),
@@ -86,41 +85,4 @@ class _GoalProgressState extends State<GoalProgress> {
),
);
}
Widget _buildProgressItem({
required String title,
required String current,
required String target,
required String unit,
required double progress,
required Color color,
}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(title),
Text(
'$current/$target $unit',
style: TextStyle(fontWeight: FontWeight.w500, color: color),
),
],
),
const SizedBox(height: 8),
GFProgressBar(
animation: true,
lineHeight: 20,
percentage: progress,
progressBarColor: color,
child: Text(
'${roundNum(progress * 100)}%',
textAlign: TextAlign.end,
style: TextStyle(color: Colors.white),
),
),
],
);
}
}

View File

@@ -1,3 +1,4 @@
import 'package:fitnote/models/health.dart';
import 'package:fitnote/provider/health_provider.dart';
import 'package:fitnote/service/health_service.dart';
import 'package:flutter/material.dart';
@@ -32,7 +33,9 @@ class _RecordCalendarState extends State<RecordCalendar> {
onDaySelected: (selectedDay, focusedDay) {
if (!isSameDay(provider.selectedDate, selectedDay)) {
provider.updateSelectDate(selectedDay);
provider.updateRecord(service.getRecordByDate(provider.selectedDate));
HealthRecord record = service.getRecordByDate(provider.selectedDate);
provider.updateRecord(record);
provider.initForm(record);
}
}
);

View File

@@ -108,6 +108,12 @@ class _RecordDailyState extends State<RecordDaily> {
_buildRecordTitle(type),
const SizedBox(height: 4),
_buildRecordContent(provider.currentRecord, type),
if (checkHasRecordValue(provider.currentRecord, type))
_buildRecordTip(
provider.currentRecord,
provider.selectedDate,
type,
),
],
),
trailing: GestureDetector(
@@ -279,4 +285,49 @@ class _RecordDailyState extends State<RecordDaily> {
return Text(formatContent(record, type), style: TextStyle(fontSize: 14));
}
Widget _buildRecordTip(HealthRecord record, DateTime date, RecordType type) {
late num lastValue = 0;
late String change = '';
switch (type) {
case RecordType.weight:
lastValue = service.getLastWeight(formatDate(date));
break;
case RecordType.sport:
lastValue = service.getLastSport(formatDate(date));
break;
case RecordType.sleep:
lastValue = service.getLastSleep(formatDate(date));
break;
}
if (lastValue == 0) {
return const SizedBox.shrink();
}
switch (type) {
case RecordType.weight:
num difference = record.weight - lastValue;
String formattedDiff = NumberFormat('+0.00;-0.00').format(difference);
change = '较上次 $formattedDiff ${type.unit}';
break;
case RecordType.sport:
num difference = record.sportDuration - lastValue;
String formattedDiff = NumberFormat('+0;-0').format(difference);
change = '较上次 $formattedDiff ${type.unit}';
break;
case RecordType.sleep:
String startTime = record.sleepStartTime;
String endTime = record.sleepEndTime;
num difference = calSleepMinDuration(startTime, endTime) - lastValue;
String formattedDiff = NumberFormat('+0.00;-0.00').format(difference / 60);
change = '较上次 $formattedDiff ${type.unit}';
break;
}
return Text(
change,
style: TextStyle(fontSize: 14, color: Colors.grey[500]),
);
}
}