feat:增加数据存储
This commit is contained in:
211
lib/widget/record/record_daily.dart
Normal file
211
lib/widget/record/record_daily.dart
Normal file
@@ -0,0 +1,211 @@
|
||||
import 'package:fitnote/models/health.dart';
|
||||
import 'package:fitnote/provider/health_provider.dart';
|
||||
import 'package:fitnote/service/health_service.dart';
|
||||
import 'package:fitnote/widget/record/common.dart';
|
||||
import 'package:fitnote/widget/record/record_form.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:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class RecordDaily extends StatefulWidget {
|
||||
const RecordDaily({super.key});
|
||||
|
||||
@override
|
||||
State<RecordDaily> createState() => _RecordDailyState();
|
||||
}
|
||||
|
||||
class _RecordDailyState extends State<RecordDaily> {
|
||||
final HealthService service = HealthService();
|
||||
|
||||
late HealthRecord _record;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_record = service.getAllRecordByDate('2025-12-01');
|
||||
}
|
||||
|
||||
final recordForm = HealthRecord(
|
||||
id: 1,
|
||||
sportProject: '羽毛球',
|
||||
sportDuration: 20,
|
||||
sleepStartTime: '23:00',
|
||||
sleepEndTime: '24:00',
|
||||
sleepDuration: 60,
|
||||
weight: 70,
|
||||
date: '2025-11-28',
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CommonCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
buildCardTitle(
|
||||
context: context,
|
||||
title: '今日记录',
|
||||
icon: FontAwesomeIcons.calendarCheck,
|
||||
),
|
||||
_buildTodayTag(),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ListView.separated(
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
itemCount: RecordType.values.length,
|
||||
separatorBuilder: (context, index) => SizedBox(height: 8),
|
||||
itemBuilder: (context, index) {
|
||||
final recordType = RecordType.values[index];
|
||||
return _buildRecordListTile(recordType);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTodayTag() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF1F5F9),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Text(
|
||||
DateFormat('MM月dd日').format(DateTime.now()),
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF64748B),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecordListTile(RecordType type) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [type.color.withAlpha(10), Colors.white.withAlpha(200)],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(10),
|
||||
blurRadius: 12,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ListTile(
|
||||
leading: Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
color: type.color.withAlpha(30),
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
),
|
||||
child: Icon(type.icon, color: type.color, size: 22),
|
||||
),
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
type.title,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: type.color,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
_buildRecordContent(type),
|
||||
],
|
||||
),
|
||||
trailing: GestureDetector(
|
||||
onTap: () => onTapRecord(type),
|
||||
child: Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: type.color.withAlpha(30),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Icon(
|
||||
_checkHasRecordValue(type)
|
||||
? FontAwesomeIcons.pencil
|
||||
: FontAwesomeIcons.plus,
|
||||
color: type.color,
|
||||
size: 18,
|
||||
),
|
||||
),
|
||||
),
|
||||
splashColor: Colors.transparent,
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 20,
|
||||
vertical: 12,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool _checkHasRecordValue(RecordType type) {
|
||||
switch (type) {
|
||||
case RecordType.weight:
|
||||
return _record.weight != 0;
|
||||
case RecordType.sport:
|
||||
return _record.sportProject != '';
|
||||
case RecordType.sleep:
|
||||
return _record.sleepStartTime != '';
|
||||
}
|
||||
}
|
||||
|
||||
void onTapRecord(RecordType type) {
|
||||
final provider = context.read<HealthProvider>();
|
||||
provider.initForm(HealthRecord.getDefault());
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => Dialog(child: RecordForm(type: type)),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecordContent(RecordType type) {
|
||||
if (!_checkHasRecordValue(type)) {
|
||||
return Text(
|
||||
"尚未记录今日数据,点击添加",
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey[500]),
|
||||
);
|
||||
}
|
||||
|
||||
late String title;
|
||||
switch (type) {
|
||||
case RecordType.weight:
|
||||
title = "${_record.weight} kg";
|
||||
case RecordType.sport:
|
||||
title = "${_record.sportProject} · ${_record.sportDuration}分钟";
|
||||
case RecordType.sleep:
|
||||
title =
|
||||
"入睡 ${_record.sleepStartTime} · 起床 ${_record.sleepEndTime} · ${_record.sleepDuration}分钟";
|
||||
}
|
||||
|
||||
return Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Colors.grey[800],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user