123 lines
3.7 KiB
Dart
123 lines
3.7 KiB
Dart
import 'package:fitnote/models/health.dart';
|
|
import 'package:fitnote/provider/health_provider.dart';
|
|
import 'package:fitnote/service/health_service.dart';
|
|
import 'package:fitnote/utils/record_utils.dart';
|
|
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 {
|
|
const GoalProgress({super.key});
|
|
|
|
@override
|
|
State<GoalProgress> createState() => _GoalProgressState();
|
|
}
|
|
|
|
class _GoalProgressState extends State<GoalProgress> {
|
|
final HealthService service = HealthService();
|
|
|
|
final int initWeight = 72;
|
|
final int goalWeight = 70;
|
|
final int goalTotalSpot = 600;
|
|
final int goalAvgSleep = 8;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
final provider = context.read<HealthProvider>();
|
|
provider.totalSpot = service.getTotalSport();
|
|
provider.avgSleep = service.getAvgSleep();
|
|
provider.latestWeight = service.getLatestWeight();
|
|
|
|
provider.updateRecord(service.getRecordByDate(provider.selectedDate));
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<HealthProvider>();
|
|
|
|
return CommonCard(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
buildCardTitle(
|
|
context: context,
|
|
title: '本月目标进度',
|
|
icon: FontAwesomeIcons.trophy,
|
|
),
|
|
const SizedBox(height: 28),
|
|
_buildProgressItem(
|
|
title: '目标体重',
|
|
current: provider.latestWeight.toString(),
|
|
target: goalWeight.toString(),
|
|
unit: 'kg',
|
|
progress: calWeightProgress(provider.latestWeight, goalWeight, initWeight),
|
|
color: RecordType.weight.color,
|
|
),
|
|
const SizedBox(height: 28),
|
|
_buildProgressItem(
|
|
title: '运动总时长',
|
|
current: provider.totalSpot.toString(),
|
|
target: goalTotalSpot.toString(),
|
|
unit: '分钟',
|
|
progress: formatProgress(provider.totalSpot / goalTotalSpot),
|
|
color: RecordType.sport.color,
|
|
),
|
|
const SizedBox(height: 28),
|
|
_buildProgressItem(
|
|
title: '日均睡眠',
|
|
current: provider.avgSleep.toString(),
|
|
target: goalAvgSleep.toString(),
|
|
unit: '小时',
|
|
progress: formatProgress(provider.avgSleep / goalAvgSleep),
|
|
color: RecordType.sleep.color,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
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(
|
|
'${progress * 100}%',
|
|
textAlign: TextAlign.end,
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|