【flutter for open harmony】第三方库Flutter 鸿蒙版 体重记录 实战指南(适配 1.0.0)✨
【flutter for open harmony】第三方库Flutter 鸿蒙版 体重记录 实战指南(适配 1.0.0)✨
Flutter 三方库 cached_network_image 的鸿蒙化适配与实战指南
欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
本文详细介绍如何在Flutter鸿蒙应用中实现体重记录功能,包括数据录入、列表展示和统计分析。
一、前言
体重记录是健康管理的重要工具,可以帮助用户追踪体重变化。本文将介绍如何在Flutter鸿蒙应用中实现体重记录功能。
二、效果展示
2.1 功能特性
| 功能 | 描述 |
|---|---|
| 体重录入 | 输入并记录体重数据 |
| 列表展示 | 展示历史体重记录 |
| 统计分析 | 显示最新、最高、最低体重 |
| 删除功能 | 支持删除记录 |
三、项目背景与目标
3.1 项目背景
健康管理是现代人关注的话题,体重记录是健康管理的基础功能。
3.2 项目目标
- 实现体重录入功能
- 实现列表展示功能
- 实现统计分析功能
- 实现删除功能
四、技术架构设计
4.1 架构概述
体重记录应用使用List存储数据,通过DateTime记录时间,实现数据的增删查功能。
4.2 数据模型
classWeightEntry{finaldouble weight;finalDateTimedate;}五、详细实现
5.1 Flutter端实现
import'package:flutter/material.dart';classWeightRecordPageextendsStatefulWidget{constWeightRecordPage({super.key});@overrideState<WeightRecordPage>createState()=>_WeightRecordPageState();}class_WeightRecordPageStateextendsState<WeightRecordPage>{finalList<WeightEntry>_entries=[];finalTextEditingController_weightController=TextEditingController();double?_minWeight;double?_maxWeight;void_addWeight(){finalweight=double.tryParse(_weightController.text);if(weight==null||weight<=0||weight>500){ScaffoldMessenger.of(context).showSnackBar(constSnackBar(content:Text('请输入有效体重(1-500kg)')),);return;}setState((){_entries.insert(0,WeightEntry(weight:weight));_updateMinMax();_weightController.clear();});}void_updateMinMax(){if(_entries.isEmpty){_minWeight=null;_maxWeight=null;return;}finalweights=_entries.map((e)=>e.weight).toList();_minWeight=weights.reduce((a,b)=>a<b?a:b);_maxWeight=weights.reduce((a,b)=>a>b?a:b);}void_deleteEntry(int index){setState((){_entries.removeAt(index);_updateMinMax();});}@overrideWidgetbuild(BuildContextcontext){returnScaffold(appBar:AppBar(title:constText('体重记录'),centerTitle:true,backgroundColor:Colors.blue,foregroundColor:Colors.white,),body:Column(children:[Container(padding:constEdgeInsets.all(16),color:Colors.blue.withOpacity(0.1),child:Row(children:[Expanded(child:TextField(controller:_weightController,keyboardType:constTextInputType.numberWithOptions(decimal:true),decoration:constInputDecoration(labelText:'体重 (kg)',border:OutlineInputBorder(),suffixText:'kg',),onSubmitted:(_)=>_addWeight(),),),constSizedBox(width:16),ElevatedButton(onPressed:_addWeight,style:ElevatedButton.styleFrom(backgroundColor:Colors.blue,foregroundColor:Colors.white,),child:constText('记录'),),],),),if(_entries.isNotEmpty)...[Padding(padding:constEdgeInsets.all(16),child:Row(mainAxisAlignment:MainAxisAlignment.spaceAround,children:[_buildStatCard('最新',_entries.first.weight,Colors.blue),_buildStatCard('最低',_minWeight??0,Colors.green),_buildStatCard('最高',_maxWeight??0,Colors.red),],),),],Expanded(child:_entries.isEmpty?constCenter(child:Text('暂无记录',style:TextStyle(color:Colors.grey,fontSize:18))):ListView.builder(padding:constEdgeInsets.all(16),itemCount:_entries.length,itemBuilder:(context,index){finalentry=_entries[index];returnCard(child:ListTile(leading:constIcon(Icons.monitor_weight,color:Colors.blue),title:Text('${entry.weight}kg'),subtitle:Text('${entry.date.year}/${entry.date.month}/${entry.date.day}'),trailing:IconButton(icon:constIcon(Icons.delete,color:Colors.red),onPressed:()=>_deleteEntry(index),),),);},),),],),);}Widget_buildStatCard(Stringlabel,double value,Colorcolor){returnColumn(children:[Text(label,style:TextStyle(fontSize:14,color:Colors.grey[600])),constSizedBox(height:4),Text('${value.toStringAsFixed(1)}kg',style:TextStyle(fontSize:18,fontWeight:FontWeight.bold,color:color),),],);}}classWeightEntry{finaldouble weight;finalDateTimedate;WeightEntry({requiredthis.weight}):date=DateTime.now();}5.2 核心功能解析
数据录入
通过TextField获取用户输入,验证后添加到列表。
统计分析
使用reduce方法计算最高和最低体重。
时间记录
在创建WeightEntry时自动记录当前时间。
六、实际应用场景
6.1 健康管理
追踪体重变化,管理健康。
6.2 减肥记录
记录减肥过程中的体重变化。
6.3 健身追踪
配合健身计划记录体重。
七、优化建议
7.1 数据持久化
使用本地数据库保存数据。
7.2 图表展示
添加体重变化曲线图。
7.3 目标设定
设定目标体重并追踪进度。
八、常见问题与解决方案
8.1 输入验证
使用tryParse验证数字输入。
8.2 空列表处理
检查列表是否为空再计算统计值。
8.3 数据排序
使用insert(0, …)将新数据插入列表头部。
九、总结
本文详细介绍了Flutter鸿蒙体重记录功能的实现过程,包括数据录入、列表展示和统计分析。通过本实例,开发者可以掌握Flutter列表操作、数据处理、状态管理等关键技术点。
十、参考资料
- Flutter官方文档:https://flutter.dev
- HarmonyOS开发者文档:https://developer.harmonyos.com
- Flutter中国社区:https://flutter-io.cn
