当前位置: 首页 > news >正文

【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
http://www.jsqmd.com/news/730638/

相关文章:

  • 第二十天打卡 | 150. 逆波兰表达式求值
  • TWIG框架:视觉生成中的动态文本推理技术
  • CurateClick 2026年4月每周精选:发现、访问与创意AI
  • 告别安卓模拟器:Windows原生APK安装器的技术革命
  • AI工具Awesome List:社区驱动的资源导航与实战选型指南
  • NVIDIA Profile Inspector终极指南:3步解锁显卡隐藏性能的免费神器
  • 多模态提示优化(MPO):提升MLLMs性能的关键技术
  • 基于微信小程序的校园失物招领管理系统【uniapp+springboot+vue】
  • 多模态模型演进与UniT框架实践解析
  • 深度解析残差网络的知识表示与传播机制
  • 将 claude code 编程助手无缝对接至 taotoken 聚合平台
  • 别再死记硬背公式了!用MATLAB手把手复现MSK调制与解调(附完整代码和眼图分析)
  • KLayout开源版图设计工具:从新手到专家的完整指南
  • Java 中的 `float` 和 `double`的底层编码
  • 中年男人的梦魇:房产缩水、失业危机与痛失至亲
  • 【flutter for open harmony】第三方库Flutter 鸿蒙版 骨架屏 实战指南(适配 1.0.0)✨
  • 自托管团队协作工具Flock:轻量级架构、实时通信与部署实战
  • UOS忘记密码别慌!用LiveCD工具5分钟搞定,附命令行救援模式详细步骤
  • 018、PID控制器的离散化实现
  • WebForms ArrayList:深入理解与最佳实践
  • 告别Printf:用Qt Creator+GDB Server远程调试ARM程序,实时查看变量和内存
  • RTL仿真性能优化:张量代数方法解析
  • 高斯计的读数是越大还是越小好?
  • 使用【ChatGPT Images 2】高效生成文旅海报
  • SOCD Cleaner完全指南:彻底解决键盘输入冲突,提升游戏操作精度
  • QQ音乐解码神器:3分钟学会qmcdump将qmcflac/qmc0/qmc3转成通用音频格式
  • 多模态AI在超声影像分析中的应用与优化
  • 多功能数据库与协议爆破测试工具(支持MySQL、Redis、Oracle等)
  • Codex 使用技巧(免费使用方法)
  • 10分钟高效掌握SMU调试工具:AMD Ryzen处理器配置优化实战指南