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

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平台上作品详情页面的实现方法。详情页面是内容展示的核心页面,其设计需要突出内容本身并提供丰富的互动功能。

http://www.rkmt.cn/news/154618.html

相关文章:

  • 用Dify打造智能客服机器人,只需三步完成模型集成与发布
  • 构建生产级AI应用不再难——Dify平台全功能使用手册
  • Dify开源项目Pull Request审核标准说明
  • 电源路径管理(PPM)设计新手教程
  • 一文说清PCBA设计与打样的关键差异与联系
  • 深入浅出讲解UDS协议NRC错误响应逻辑
  • Dify可视化流程中数据脱敏节点的应用场景
  • css垂直居中的多种写法
  • 月薪100万,你能接受996吗?
  • Python 深度学习环境报错:核心要点解析 libcudart.so 问题
  • 一文说清Chrome Driver在Web自动化中的作用机制
  • 标题起啥啊
  • 远程SSH中screen命令应用:新手教程防掉线方案
  • Dify平台支持的OCR文字识别集成方案
  • Dify镜像在客户服务场景中的情感分析应用
  • 电源层布线技巧:Altium Designer线宽电流关系实践
  • 小熊猫Dev-C++实战手册:从零到精通的完整教程
  • RePKG神器使用全攻略:解锁Wallpaper Engine隐藏资源
  • Dify平台支持的批量数据处理模式实战
  • Dify镜像在学术论文摘要生成中的准确率测试
  • RePKG:Wallpaper Engine资源提取与转换工具完全指南
  • Dify平台如何保障长时间运行任务的稳定性?
  • 百度网盘直链解析工具:3步实现高速下载自由
  • RePKG完全攻略:3步搞定Wallpaper Engine资源提取与转换
  • 2025年终两天一夜游推荐路线:行程体验与特色维度实测TOP3推荐。 - 品牌推荐
  • Dify开源项目的CI/CD流水线构建实践
  • 数字识别对比
  • 全面讲解:es可视化管理工具与ELK栈集成日志分析流程
  • 2025年终三峡旅游最佳路线推荐:主流产品横向测评与3条高性价比路线盘点。 - 品牌推荐
  • Dify平台支持的语音识别与合成集成路径