news 2026/4/20 10:57:52

Flutter与OpenHarmony作品详情页面开发

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Flutter与OpenHarmony作品详情页面开发

前言

作品详情页面是内容平台中展示单个作品完整信息的核心页面。它需要展示作品图片、标题、作者信息、详细描述、互动数据等内容,并提供点赞、收藏、评论、分享等交互功能。本文将详细介绍如何在Flutter和OpenHarmony平台上实现一个功能完善的作品详情页面。

作品详情页面的设计需要突出作品本身,同时提供丰富的互动入口。页面布局应该清晰有序,让用户能够快速获取关键信息。

Flutter作品详情页面实现

页面结构设计

作品详情页面接收作品信息参数。

classArtworkDetailPageextendsStatefulWidget{finalStringtitle;finalStringauthor;constArtworkDetailPage({super.key,requiredthis.title,requiredthis.author});@overrideState<ArtworkDetailPage>createState()=>_ArtworkDetailPageState();}class_ArtworkDetailPageStateextendsState<ArtworkDetailPage>{bool _isLiked=false;bool _isCollected=false;int _likeCount=128;

使用StatefulWidget管理点赞和收藏状态。title和author通过构造函数传入。

页面布局实现

使用CustomScrollView实现可折叠的头部效果。

@overrideWidgetbuild(BuildContextcontext){returnScaffold(body:CustomScrollView(slivers:[SliverAppBar(expandedHeight:300,pinned:true,backgroundColor:constColor(0xFF8B4513),leading:IconButton(icon:constIcon(Icons.arrow_back,color:Colors.white),onPressed:()=>Navigator.pop(context),),actions:[IconButton(icon:constIcon(Icons.share,color:Colors.white),onPressed:(){}),],flexibleSpace:FlexibleSpaceBar(background:Container(color:Colors.grey[300],child:constCenter(child:Icon(Icons.image,size:80,color:Colors.grey)),),),),

SliverAppBar实现可折叠的图片头部。expandedHeight设置展开高度。pinned使AppBar在滚动时固定在顶部。

作品信息区域

展示作品标题、作者和描述。

SliverToBoxAdapter(child:Container(padding:constEdgeInsets.all(16),child:Column(crossAxisAlignment:CrossAxisAlignment.start,children:[Text(widget.title,style:constTextStyle(fontSize:22,fontWeight:FontWeight.bold,color:Color(0xFF8B4513))),constSizedBox(height:12),Row(children:[CircleAvatar(radius:20,backgroundColor:constColor(0xFF8B4513),child:Text(widget.author[0],style:constTextStyle(color:Colors.white)),),constSizedBox(width:12),Column(crossAxisAlignment:CrossAxisAlignment.start,children:[Text(widget.author,style:constTextStyle(fontSize:14,fontWeight:FontWeight.bold)),Text('发布于 2023-12-10',style:TextStyle(fontSize:12,color:Colors.grey[600])),],),constSpacer(),ElevatedButton(onPressed:(){},style:ElevatedButton.styleFrom(backgroundColor:constColor(0xFF8B4513)),child:constText('关注',style:TextStyle(color:Colors.white,fontSize:12)),),],),constSizedBox(height:16),constText('这是一幅精美的刺绣作品,采用传统苏绣技法,历时三个月完成。作品以牡丹为主题,寓意富贵吉祥。针法细腻,色彩丰富,展现了中国传统刺绣艺术的独特魅力。',style:TextStyle(fontSize:14,height:1.6,color:Color(0xFF666666)),),

作者信息区域包含头像、名称、发布时间和关注按钮。作品描述使用较大的行高提升可读性。

互动按钮区域

底部固定的互动按钮栏。

],),),),],),bottomNavigationBar:Container(padding:constEdgeInsets.all(16),decoration:BoxDecoration(color:Colors.white,boxShadow:[BoxShadow(color:Colors.black.withOpacity(0.1),blurRadius:10,offset:constOffset(0,-5))],),child:Row(mainAxisAlignment:MainAxisAlignment.spaceAround,children:[_buildActionButton(icon:_isLiked?Icons.favorite:Icons.favorite_border,label:'$_likeCount',color:_isLiked?Colors.red:Colors.grey,onTap:()=>setState((){_isLiked=!_isLiked;_likeCount+=_isLiked?1:-1;}),),_buildActionButton(icon:Icons.chat_bubble_outline,label:'32',color:Colors.grey,onTap:(){}),_buildActionButton(icon:_isCollected?Icons.bookmark:Icons.bookmark_border,label:'收藏',color:_isCollected?constColor(0xFF8B4513):Colors.grey,onTap:()=>setState(()=>_isCollected=!_isCollected),),_buildActionButton(icon:Icons.share_outlined,label:'分享',color:Colors.grey,onTap:(){}),],),),);}Widget_buildActionButton({requiredIconDataicon,requiredStringlabel,requiredColorcolor,requiredVoidCallbackonTap}){returnGestureDetector(onTap:onTap,child:Column(mainAxisSize:MainAxisSize.min,children:[Icon(icon,color:color,size:24),constSizedBox(height:4),Text(label,style:TextStyle(fontSize:12,color:color)),],),);}}

bottomNavigationBar固定在页面底部。点赞和收藏按钮根据状态显示不同图标和颜色。点赞数实时更新。

OpenHarmony鸿蒙实现

页面定义

鸿蒙平台使用路由参数接收作品信息。

@Entry@Componentstruct ArtworkDetailPage{@Statetitle:string=''@Stateauthor:string=''@StateisLiked:boolean=false@StateisCollected:boolean=false@StatelikeCount:number=128aboutToAppear(){constparams=router.getParams()asRecord<string,string>this.title=params?.title||'作品详情'this.author=params?.author||'未知作者'}

页面布局实现

使用Scroll和Column构建页面。

build(){Column(){Scroll(){Column(){Stack(){Image($r('app.media.artwork_placeholder')).width('100%').height(300).objectFit(ImageFit.Cover)Row(){Image($r('app.media.back')).width(24).height(24).fillColor(Color.White).onClick(()=>router.back())Blank()Image($r('app.media.share')).width(24).height(24).fillColor(Color.White)}.width('100%').padding(16).position({y:40})}.width('100%').height(300).backgroundColor('#F0F0F0')Column(){Text(this.title).fontSize(22).fontWeight(FontWeight.Bold).fontColor('#8B4513').width('100%')Row(){Text(this.author.charAt(0)).fontSize(14).fontColor(Color.White).width(40).height(40).borderRadius(20).backgroundColor('#8B4513').textAlign(TextAlign.Center)Column(){Text(this.author).fontSize(14).fontWeight(FontWeight.Bold)Text('发布于 2023-12-10').fontSize(12).fontColor('#666666').margin({top:2})}.alignItems(HorizontalAlign.Start).margin({left:12})Blank()Button('关注').fontSize(12).fontColor(Color.White).backgroundColor('#8B4513').height(32)}.width('100%').margin({top:12})Text('这是一幅精美的刺绣作品,采用传统苏绣技法,历时三个月完成。作品以牡丹为主题,寓意富贵吉祥。').fontSize(14).fontColor('#666666').lineHeight(22).margin({top:16}).width('100%')}.width('100%').padding(16)}}.layoutWeight(1)Row(){this.ActionButton($r('app.media.heart'),this.likeCount.toString(),this.isLiked?'#F44336':'#999999',()=>{this.isLiked=!this.isLikedthis.likeCount+=this.isLiked?1:-1})this.ActionButton($r('app.media.comment'),'32','#999999',()=>{})this.ActionButton($r('app.media.bookmark'),'收藏',this.isCollected?'#8B4513':'#999999',()=>{this.isCollected=!this.isCollected})this.ActionButton($r('app.media.share'),'分享','#999999',()=>{})}.width('100%').height(60).padding({left:16,right:16}).backgroundColor(Color.White).justifyContent(FlexAlign.SpaceAround)}.width('100%').height('100%')}@BuilderActionButton(icon:Resource,label:string,color:string,onClick:()=>void){Column(){Image(icon).width(24).height(24).fillColor(color)Text(label).fontSize(12).fontColor(color).margin({top:4})}.onClick(onClick)}}

@Builder定义可复用的互动按钮。点赞和收藏状态通过@State管理,变化时自动更新UI。

总结

本文介绍了Flutter和OpenHarmony平台上作品详情页面的实现方法。详情页面是内容展示的核心页面,其设计需要突出内容本身并提供丰富的互动功能。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/17 22:11:30

Dify平台的冷启动优化策略研究

Dify平台的冷启动优化策略研究 在大模型技术迅猛发展的今天&#xff0c;越来越多企业试图将LLM&#xff08;大语言模型&#xff09;融入实际业务场景。然而现实却常常令人沮丧&#xff1a;一个看似简单的智能客服或知识问答系统&#xff0c;从构思到可演示原型往往需要数周甚至…

作者头像 李华
网站建设 2026/4/19 0:11:51

Dify平台如何保障长时间运行任务的稳定性?

Dify平台如何保障长时间运行任务的稳定性&#xff1f; 在当今企业级AI应用日益复杂的背景下&#xff0c;一个常被忽视但至关重要的问题浮出水面&#xff1a;当AI系统需要持续运行数小时甚至跨天交互时&#xff0c;如何确保它不会“断片”、不会丢状态、不会因一次网络抖动而前功…

作者头像 李华
网站建设 2026/4/17 21:01:46

Dify镜像部署后的日志轮转配置建议

Dify镜像部署后的日志轮转配置建议 在现代 AI 应用平台的生产部署中&#xff0c;Dify 作为一款功能完整的开源 LLM 应用开发框架&#xff0c;正被越来越多企业用于构建智能客服、自动化 Agent 和 RAG 系统。然而&#xff0c;随着服务持续运行&#xff0c;一个看似不起眼却极易引…

作者头像 李华
网站建设 2026/4/18 9:57:41

RePKG:Wallpaper Engine资源提取终极指南

RePKG&#xff1a;Wallpaper Engine资源提取终极指南 【免费下载链接】repkg Wallpaper engine PKG extractor/TEX to image converter 项目地址: https://gitcode.com/gh_mirrors/re/repkg 想要解锁Wallpaper Engine壁纸包中的隐藏资源吗&#xff1f;RePKG这款强大的开…

作者头像 李华
网站建设 2026/4/17 22:06:03

虚拟串口软件与真实串口对比分析通俗解释

虚拟串口 vs 真实串口&#xff1a;一场软硬之间的通信博弈你有没有遇到过这样的场景&#xff1f;手头一台轻薄本&#xff0c;连个DB9接口都没有&#xff0c;却要调试一块STM32开发板&#xff1b;或者想测试一个串口协议解析器&#xff0c;但买十个GPS模块成本太高、布线还乱得像…

作者头像 李华
网站建设 2026/4/18 19:27:12

RePKG完全攻略:3步搞定Wallpaper Engine资源提取与转换

RePKG完全攻略&#xff1a;3步搞定Wallpaper Engine资源提取与转换 【免费下载链接】repkg Wallpaper engine PKG extractor/TEX to image converter 项目地址: https://gitcode.com/gh_mirrors/re/repkg 想要深度定制Wallpaper Engine壁纸却苦于无法访问PKG资源包&…

作者头像 李华