news 2026/5/1 10:31:32

Flutter与OpenHarmony商品详情页面开发

作者头像

张小明

前端开发工程师

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

前言

商品详情页面是电商应用中最重要的转化页面。它需要展示商品图片、价格、规格、描述、评价等信息,并提供加入购物车和立即购买的入口。本文将详细介绍如何在Flutter和OpenHarmony平台上实现一个功能完善的商品详情页面。

商品详情页面的设计直接影响用户的购买决策,需要在信息展示和购买引导之间取得平衡。

Flutter商品详情页面实现

页面结构设计

商品详情页面接收商品名称参数。

classProductDetailPageextendsStatefulWidget{finalStringproductName;constProductDetailPage({super.key,requiredthis.productName});@overrideState<ProductDetailPage>createState()=>_ProductDetailPageState();}class_ProductDetailPageStateextendsState<ProductDetailPage>{int _quantity=1;bool _isFavorite=false;

使用StatefulWidget管理数量和收藏状态。

商品图片区域

使用PageView实现商品图片轮播。

@overrideWidgetbuild(BuildContextcontext){returnScaffold(body:CustomScrollView(slivers:[SliverAppBar(expandedHeight:350,pinned:true,backgroundColor:constColor(0xFF8B4513),leading:IconButton(icon:constIcon(Icons.arrow_back,color:Colors.white),onPressed:()=>Navigator.pop(context),),actions:[IconButton(icon:Icon(_isFavorite?Icons.favorite:Icons.favorite_border,color:_isFavorite?Colors.red:Colors.white),onPressed:()=>setState(()=>_isFavorite=!_isFavorite),),IconButton(icon:constIcon(Icons.share,color:Colors.white),onPressed:(){}),],flexibleSpace:FlexibleSpaceBar(background:Container(color:Colors.grey[200],child:constCenter(child:Icon(Icons.shopping_bag,size:100,color:Colors.grey)),),),),

SliverAppBar实现可折叠的图片头部。收藏按钮根据状态显示不同图标和颜色。

商品信息区域

展示价格、名称和销量信息。

SliverToBoxAdapter(child:Container(padding:constEdgeInsets.all(16),color:Colors.white,child:Column(crossAxisAlignment:CrossAxisAlignment.start,children:[Row(crossAxisAlignment:CrossAxisAlignment.end,children:[constText('¥',style:TextStyle(fontSize:16,color:Color(0xFFE53935),fontWeight:FontWeight.bold)),constText('299',style:TextStyle(fontSize:28,color:Color(0xFFE53935),fontWeight:FontWeight.bold)),constSizedBox(width:8),Text('¥399',style:TextStyle(fontSize:14,color:Colors.grey[500],decoration:TextDecoration.lineThrough)),],),constSizedBox(height:8),Text(widget.productName,style:constTextStyle(fontSize:18,fontWeight:FontWeight.bold)),constSizedBox(height:8),Row(children:[Container(padding:constEdgeInsets.symmetric(horizontal:6,vertical:2),decoration:BoxDecoration(color:constColor(0xFFE53935).withOpacity(0.1),borderRadius:BorderRadius.circular(4)),child:constText('限时特惠',style:TextStyle(fontSize:10,color:Color(0xFFE53935))),),constSizedBox(width:8),Text('已售 1.2K',style:TextStyle(fontSize:12,color:Colors.grey[600])),constSpacer(),Row(children:[constIcon(Icons.star,size:14,color:Colors.amber),constSizedBox(width:2),Text('4.9',style:TextStyle(fontSize:12,color:Colors.grey[600])),],),],),],),),),

价格使用红色突出显示,原价使用删除线。促销标签和销量信息增强购买信心。

数量选择与购买按钮

底部固定的购买操作栏。

],),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(children:[Column(mainAxisSize:MainAxisSize.min,children:[constIcon(Icons.store_outlined,color:Colors.grey),Text('店铺',style:TextStyle(fontSize:10,color:Colors.grey[600])),],),constSizedBox(width:16),Column(mainAxisSize:MainAxisSize.min,children:[constIcon(Icons.chat_bubble_outline,color:Colors.grey),Text('客服',style:TextStyle(fontSize:10,color:Colors.grey[600])),],),constSizedBox(width:16),Expanded(child:Row(children:[Expanded(child:ElevatedButton(onPressed:(){},style:ElevatedButton.styleFrom(backgroundColor:constColor(0xFFFF9800),padding:constEdgeInsets.symmetric(vertical:12),shape:constRoundedRectangleBorder(borderRadius:BorderRadius.horizontal(left:Radius.circular(25)),),),child:constText('加入购物车',style:TextStyle(color:Colors.white)),),),Expanded(child:ElevatedButton(onPressed:(){},style:ElevatedButton.styleFrom(backgroundColor:constColor(0xFFE53935),padding:constEdgeInsets.symmetric(vertical:12),shape:constRoundedRectangleBorder(borderRadius:BorderRadius.horizontal(right:Radius.circular(25)),),),child:constText('立即购买',style:TextStyle(color:Colors.white)),),),],),),],),),);}}

底部栏包含店铺、客服入口和购买按钮。加入购物车和立即购买使用不同颜色区分。

OpenHarmony鸿蒙实现

页面定义

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

@Entry@Componentstruct ProductDetailPage{@StateproductName:string=''@StateisFavorite:boolean=false@Statequantity:number=1aboutToAppear(){constparams=router.getParams()asRecord<string,string>this.productName=params?.name||'商品详情'}

页面布局实现

使用Scroll构建可滚动页面。

build(){Column(){Scroll(){Column(){Stack(){Image($r('app.media.product_placeholder')).width('100%').height(350).objectFit(ImageFit.Cover)Row(){Image($r('app.media.back')).width(24).height(24).fillColor(Color.White).onClick(()=>router.back())Blank()Image(this.isFavorite?$r('app.media.heart_filled'):$r('app.media.heart')).width(24).height(24).fillColor(this.isFavorite?'#F44336':Color.White).onClick(()=>{this.isFavorite=!this.isFavorite})Image($r('app.media.share')).width(24).height(24).fillColor(Color.White).margin({left:16})}.width('100%').padding(16).position({y:40})}.width('100%').height(350).backgroundColor('#F0F0F0')Column(){Row(){Text('¥').fontSize(16).fontColor('#E53935').fontWeight(FontWeight.Bold)Text('299').fontSize(28).fontColor('#E53935').fontWeight(FontWeight.Bold)Text('¥399').fontSize(14).fontColor('#999999').decoration({type:TextDecorationType.LineThrough}).margin({left:8})}.alignItems(VerticalAlign.Bottom).width('100%')Text(this.productName).fontSize(18).fontWeight(FontWeight.Bold).fontColor('#333333').margin({top:8}).width('100%')Row(){Text('限时特惠').fontSize(10).fontColor('#E53935').backgroundColor('#E539351A').borderRadius(4).padding({left:6,right:6,top:2,bottom:2})Text('已售 1.2K').fontSize(12).fontColor('#666666').margin({left:8})Blank()Image($r('app.media.star')).width(14).height(14).fillColor('#FFC107')Text('4.9').fontSize(12).fontColor('#666666').margin({left:2})}.width('100%').margin({top:8})}.width('100%').padding(16).backgroundColor(Color.White)}}.layoutWeight(1)Row(){Column(){Image($r('app.media.store')).width(20).height(20).fillColor('#999999')Text('店铺').fontSize(10).fontColor('#666666')}Column(){Image($r('app.media.service')).width(20).height(20).fillColor('#999999')Text('客服').fontSize(10).fontColor('#666666')}.margin({left:16})Row(){Button('加入购物车').fontSize(14).fontColor(Color.White).backgroundColor('#FF9800').layoutWeight(1).borderRadius({topLeft:25,bottomLeft:25})Button('立即购买').fontSize(14).fontColor(Color.White).backgroundColor('#E53935').layoutWeight(1).borderRadius({topRight:25,bottomRight:25})}.layoutWeight(1).margin({left:16})}.width('100%').height(60).padding({left:16,right:16}).backgroundColor(Color.White)}.width('100%').height('100%')}}

底部购买栏固定显示。按钮使用不同颜色区分功能。

总结

本文介绍了Flutter和OpenHarmony平台上商品详情页面的实现方法。商品详情页面是电商应用的核心转化页面,其设计需要突出商品信息并提供便捷的购买入口。

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

NextStep-1震撼发布:140亿参数AI绘图新突破

NextStep-1震撼发布&#xff1a;140亿参数AI绘图新突破 【免费下载链接】NextStep-1-Large 项目地址: https://ai.gitcode.com/StepFun/NextStep-1-Large 导语&#xff1a;StepFun AI推出140亿参数的NextStep-1-Large文本到图像生成模型&#xff0c;采用创新的自回归连…

作者头像 李华
网站建设 2026/5/1 4:08:55

【C++】Template:深入理解特化与分离编译,破解编译难题

C新增的array采用的就是第二种方法&#xff1a;代码语言&#xff1a;javascriptAI代码解释// 定义一个模板类型的静态数组 template<class T, size_t N 10> class array { public:T& operator[](size_t index) { return _array[index]; }const T& operator[](si…

作者头像 李华
网站建设 2026/5/1 4:48:07

PaddlePaddle中文文档质量评测:新手友好度高于TensorFlow?

PaddlePaddle中文文档质量评测&#xff1a;新手友好度高于TensorFlow&#xff1f; 在深度学习框架竞争日益激烈的今天&#xff0c;开发者的选择早已不再局限于“哪个技术更强”&#xff0c;而是转向了更现实的问题&#xff1a;哪个平台能让我更快上手、少踩坑、快速交付项目&am…

作者头像 李华
网站建设 2026/5/1 4:48:05

Starward启动器:重新定义你的米哈游游戏体验

Starward启动器&#xff1a;重新定义你的米哈游游戏体验 【免费下载链接】Starward Game Launcher for miHoYo - 米家游戏启动器 项目地址: https://gitcode.com/gh_mirrors/st/Starward Starward启动器是一款专为米哈游游戏玩家设计的第三方启动器&#xff0c;通过智能…

作者头像 李华
网站建设 2026/5/1 4:49:10

OpenCore Legacy Patcher深度解析:突破旧Mac升级限制的终极系统解决方案

还在为手中的老款Mac无法安装最新系统而苦恼吗&#xff1f;通过OpenCore Legacy Patcher这款革命性工具&#xff0c;你可以轻松实现旧Mac升级&#xff0c;让老设备焕发新生。无论你是拥有2012年的MacBook Pro还是更早期的设备&#xff0c;这款工具都能为你提供完整的系统升级解…

作者头像 李华
网站建设 2026/5/1 4:42:30

OASIS-code-1.3B:代码搜索新基准,超越Ada-002!

OASIS-code-1.3B&#xff1a;代码搜索新基准&#xff0c;超越Ada-002&#xff01; 【免费下载链接】OASIS-code-1.3B 项目地址: https://ai.gitcode.com/hf_mirrors/Kwaipilot/OASIS-code-1.3B 代码搜索技术迎来新突破——Kwaipilot团队近日发布的OASIS-code-1.3B模型在…

作者头像 李华