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

鸿蒙:侧边栏显示与隐藏

前言:

本篇博客侧边栏显示与隐藏是基于Stack、transition、animation实现应用侧边栏点击显示和左滑隐藏的效果。

我们还是老样子,跟着官方文档学习与实践,链接如下:

https://developer.huawei.com/consumer/cn/doc/architecture-guides/sidebar_slide-0000002308440974https://developer.huawei.com/consumer/cn/doc/architecture-guides/sidebar_slide-0000002308440974大致给大伙写一下实现流程:

1、定义变量【我使用的是V2装饰器,你使用V1也可以】:

  leftTitle: string = "这是左侧边栏"rightTitle: string = "这是右侧边栏"@Local isShowLeftBuilder: boolean = false // 左侧侧边栏是否显示@Local isShowRightBuilder: boolean = false // 右侧侧边栏是否显示@Local screenWidthPx: number = 0 // 屏幕宽度

2、UI初始化前获取屏幕宽度:

aboutToAppear(): void {this.screenWidthPx = display.getDefaultDisplaySync().width}

3、创建一个组件或自定义构建函数,用来装左侧或右侧的侧边栏组件:

@BuildertheBarBuilder(title: string) {Column() {Text(title).fontSize(30).fontWeight(FontWeight.Bold).padding(20)Text("点击阴影处关闭侧边栏")}.backgroundColor(Color.White).justifyContent(FlexAlign.Center).width("85%").height("100%").expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]).transition(TransitionEffect.opacity(0.8).animation({ duration: 300, curve: Curve.Smooth }).combine(TransitionEffect.translate({x: this.isShowLeftBuilder ? -this.screenWidthPx * 0.85 : this.screenWidthPx * 0.85})))}

4、整体页面布局:

 Stack() {// 主页面Column({ space: 20 }) {Text("👏案例页面👍").fontSize(30).fontWeight(FontWeight.Bold)Button("点击我开启左侧边栏").onClick(() => {this.isShowLeftBuilder = true})Text("1111111111111111111111111111111111111111111111111111111111111111111111111111111111")Button("点击我开启右侧边栏").onClick(() => {this.isShowRightBuilder = true})Text("1111111111111111111111111111111111111111111111111111111111111111111111111111111111")}.justifyContent(FlexAlign.Center).width("100%").height("100%")// 遮罩层if (this.isShowLeftBuilder || this.isShowRightBuilder) {Column().onClick(() => {this.isShowLeftBuilder = falsethis.isShowRightBuilder = false}).width("100%").height("100%").expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]).backgroundColor(Color.Black).opacity(0.5)}// 侧边栏if (this.isShowLeftBuilder) {this.theBarBuilder(this.leftTitle)} else if (this.isShowRightBuilder) {this.theBarBuilder(this.rightTitle)}}.width("100%").height("100%").alignContent(this.isShowLeftBuilder ? Alignment.Start : Alignment.End)

5、Index.ets 完整代码:

import { display } from '@kit.ArkUI'@Entry
@ComponentV2
struct Index {leftTitle: string = "这是左侧边栏"rightTitle: string = "这是右侧边栏"@Local isShowLeftBuilder: boolean = false // 左侧侧边栏是否显示@Local isShowRightBuilder: boolean = false // 右侧侧边栏是否显示@Local screenWidthPx: number = 0 // 屏幕宽度aboutToAppear(): void {this.screenWidthPx = display.getDefaultDisplaySync().width}build() {Stack() {// 主页面Column({ space: 20 }) {Text("👏案例页面👍").fontSize(30).fontWeight(FontWeight.Bold)Button("点击我开启左侧边栏").onClick(() => {this.isShowLeftBuilder = true})Text("1111111111111111111111111111111111111111111111111111111111111111111111111111111111")Button("点击我开启右侧边栏").onClick(() => {this.isShowRightBuilder = true})Text("1111111111111111111111111111111111111111111111111111111111111111111111111111111111")}.justifyContent(FlexAlign.Center).width("100%").height("100%")// 遮罩层if (this.isShowLeftBuilder || this.isShowRightBuilder) {Column().onClick(() => {this.isShowLeftBuilder = falsethis.isShowRightBuilder = false}).width("100%").height("100%").expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]).backgroundColor(Color.Black).opacity(0.5)}// 侧边栏if (this.isShowLeftBuilder) {this.theBarBuilder(this.leftTitle)} else if (this.isShowRightBuilder) {this.theBarBuilder(this.rightTitle)}}.width("100%").height("100%").alignContent(this.isShowLeftBuilder ? Alignment.Start : Alignment.End)}@BuildertheBarBuilder(title: string) {Column() {Text(title).fontSize(30).fontWeight(FontWeight.Bold).padding(20)Text("点击阴影处关闭侧边栏")}.backgroundColor(Color.White).justifyContent(FlexAlign.Center).width("85%").height("100%").expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]).transition(TransitionEffect.opacity(0.8).animation({ duration: 300, curve: Curve.Smooth }).combine(TransitionEffect.translate({x: this.isShowLeftBuilder ? -this.screenWidthPx * 0.85 : this.screenWidthPx * 0.85})))}
}

练手项目效果图:

如果觉得有帮助,欢迎点赞、收藏或关注

http://www.dtcms.com/a/410342.html

相关文章:

  • 【开题答辩实录分享】以《基于爬虫的娱乐新闻采集系统设计与实现》为例进行答辩实录分享
  • BMAD方法论与自动化工具的协同演进:重塑Go语言微服务的技术债务
  • 月匣 - 百度推出的AI情感陪伴与剧情互动应用
  • dw网站建设教程网站建设费税率
  • 网站样版风格排版WordPress超级链接不跳转
  • Redis 五大核心数据结构知识点梳理
  • 微信小程序学习(一)
  • 零售企业档案检索慢?档案管理系统解决档案管理痛点
  • Docker 全阶段学习指南
  • Arbess从入门到实战(3) - 使用Arbess+GitLab实现Vue.js项目自动化部署
  • 智慧停车大屏数据分析与设计文档
  • 网站建设商家注册免费
  • 做网站后台系统的规范个人小程序商城
  • Linux 进程程序替换
  • 铜钟音乐前端技术解析
  • Flutter 运行IOS真机,提示无法打开“****”
  • Windows配置C/C++环境:MinGW+Vscode
  • Docker安装(Centos/Ubuntu/UOS)
  • 自动化运维利器:MCP Server + 阿里云
  • 常州公司建站模板网站怎么做定位功能
  • 网站弹窗代码网络游戏开发成本
  • Linux的网络基础
  • 福田网站建设课程考试wordpress页面链接404
  • I2C EEPROM存储影响到Rx response
  • qt使用opencv的imread读取图像为空
  • 单片机中用printf输出函数的地址
  • 做视频用的网站铁岭 开原网站建设
  • 4. ChatClient 的初始,快速使用上手
  • 【编号525】1969-2008北京五环路历年道路路网数据
  • 淘宝团购上线:本地生活的“两种解法”