本文原创发布在华为开发者社区

介绍

本示例使用\@CustomDialog实现隐私弹窗,使用AppStorage+PersistentStorage保存用户是否同意。

实现隐私弹窗源码链接

效果预览

请添加链接描述

使用说明

  1. 用户进入应用弹出隐私弹窗,在点击同意后进入应用首页。
  2. 点击不同意弹出温馨提示弹窗,再次请求用户同意隐私保护协议。点击同意进入应用首页。
  3. 点击仍不同意后弹出需要同意隐私协议才能再次使用应用。点击再次查看回到温馨提示弹窗,点击退出应用结束使用。

    实现思路

使用\@CustomDialog实现隐私弹窗,使用AppStorage+PersistentStorage保存用户是否同意。核心代码如下:


Button('同意')
  .margin(5)
  .width(250)
  .backgroundColor($r('app.color.button_back_color'))
  .fontColor($r('sys.color.comp_background_list_card'))
  .onClick(() => {
    if (this.controller != undefined) {
      this.controller.close()
    }
    // 存储用户同意
    AppStorage.setOrCreate('isAgreed', true)
    PersistentStorage.persistProp('isAgreed', true)
    router.replaceUrl({ url: 'pages/MainPage' });
  })
  .height(40)


鸿蒙场景化代码
1 声望0 粉丝