用户3116712
用户3116712
  • 发布:16 分钟前
  • 更新:16 分钟前
  • 阅读:4

列表选择弹窗ActionSheet的sheets属性无法实时更新

分类:HBuilder

问题现象
动态改变列表选择弹窗ActionSheet的sheets属性,弹窗内容无法实时更新。问题代码示例参考如下:

@Entry
@Component
struct showActionSheetExample {
@State sheets: Array<SheetInfo> = [{
title: 'apples',
action: () => {
}
}];
@State count: number = 0;

build() {
Column() {
Button('showActionSheet')
.margin(30)
.onClick(() => {
setInterval(() => {
this.count++;
this.sheets.push({
title: 'bananas' + this.count,
action: () => {
}
});
}, 1000);

      this.getUIContext().showActionSheet({  
        title: 'ActionSheet title',  
        message: 'message',  
        confirm: {  
          value: 'Confirm button',  
          action: () => {  
            console.info('Get Alert Dialog handled');  
          }  
        },  
        alignment: DialogAlignment.Center,  
        sheets: this.sheets  
      });  
    });  
}.width('100%');  

}
}
背景知识
列表选择弹窗 (ActionSheet)是一个列表选择器弹窗适用于呈现多个操作选项,尤其当界面中仅需展示操作列表而无其他内容时。固定样式,当用户需要关注或确认的信息存在列表选择时使用。
当用户需要自定义弹出框内动态更新弹出框属性和内容时,使用不依赖UI组件的自定义弹出框 (openCustomDialog)。存在两种入参方式创建自定义弹出框:
openCustomDialog(传参为ComponentContent形式):通过ComponentContent封装内容可以与UI界面解耦,调用更加灵活,可以满足开发者的封装诉求。具有较高的灵活性,弹出框样式完全自定义,并且在弹出框打开后可以使用updateCustomDialog方法动态更新弹出框的参数。
openCustomDialog(传builder的形式):相对于ComponentContent,builder必须要与上下文做绑定,与UI存在一定耦合。此方法有默认的弹出框样式,适合于开发者想要实现与系统弹窗默认风格一致的效果。
问题定位
在showActionSheet打开列表选择弹窗后,对sheets属性进行动态修改,弹窗UI未变化。

分析结论
ActionSheet列表选择弹窗不支持动态更新属性,需要通过自定义弹出框实现。

修改建议
下面通过openCustomDialog实现,并且以传builder的形式为例,实现动态更新自定义弹出框的内容。

import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
private customDialogComponentId: number = 0;
@State count: number = 0;
intervalId?: number;

@Builder
customDialogComponent() {
Column() {
Text('充电详情').fontSize(25);
Text(已充电时间:${this.count}秒);
Row({ space: 50 }) {
Button('确认').onClick(() => {
this.getUIContext().getPromptAction().closeCustomDialog(this.customDialogComponentId);
clearInterval(this.intervalId);
});
Button('取消').onClick(() => {
this.getUIContext().getPromptAction().closeCustomDialog(this.customDialogComponentId);
clearInterval(this.intervalId);
});
};
}.height(200).padding(5).justifyContent(FlexAlign.SpaceAround);
}

build() {
Row() {
Column({ space: 20 }) {
Text('组件内弹窗')
.fontSize(30)
.onClick(() => {
this.getUIContext()
.getPromptAction()
.openCustomDialog({
builder: () => {
this.customDialogComponent();
}
})
.then((dialogId: number) => {
this.customDialogComponentId = dialogId;
})
.catch((error: BusinessError) => {
console.error(openCustomDialog error code is ${error.code}, message is ${error.message});
});

        this.intervalId = setInterval(() => {  
          this.count++;  
        }, 1 * 1000);  
      });  
  }  
  .width('100%');  
}  
.height('100%');  

}
}
https://coub.com/view/06dq1lpfmv
https://coub.com/view/h9okejkccd
https://coub.com/view/z59288b3bc
https://coub.com/view/jdsgw25pj5
https://coub.com/view/20ms41ljxh
https://coub.com/view/cyjima3av9
https://coub.com/view/xhymuinpm8
https://coub.com/view/gqqk4335y1
https://coub.com/view/9uflyvtfzv
https://coub.com/view/ogd56p24ng
https://coub.com/view/zydhfymmc3
https://coub.com/view/pzd0ac0kg5
https://coub.com/view/obdv3r4cy0
https://coub.com/view/ka2mdh07zg
https://coub.com/view/k8417q6t33
https://coub.com/view/b797hoxnvb
https://coub.com/view/p3wb9ayf0i
https://coub.com/view/cylp42xa95
https://coub.com/view/5vyv5859hb
https://coub.com/view/uolo157r87
https://coub.com/view/7bmp0nxmze
https://coub.com/view/d2q6yzkrli
https://coub.com/view/zs1hg1bhrv
https://coub.com/view/yz4tqllp0u
https://coub.com/view/a2kvibvldp
https://coub.com/view/09kqx4xcfx
https://coub.com/view/2c1zyd3kx3
https://coub.com/view/wep5eg226t
https://coub.com/view/su365ycwsu
https://coub.com/view/nzdz5k3kr0
https://coub.com/view/qg568ebu9j
https://coub.com/view/md5pqvk50f
https://coub.com/view/0gt0ay1zk3
https://coub.com/view/hecylej9t3
https://coub.com/view/wmxvxv1kkj
https://coub.com/view/bxxpl0brtv
https://coub.com/view/pholjf70ut
https://coub.com/view/m8l2xwwy39
https://coub.com/view/1c1x2w7t08
https://coub.com/view/53mubpegym
https://coub.com/view/on6slxqrt5
https://coub.com/view/55q881xx6l
https://coub.com/view/s77w2ok24r
https://coub.com/view/4e9hy0237n
https://coub.com/view/3entpjmdbw
https://coub.com/view/8besg49zbq
https://coub.com/view/usqat13gtl
https://coub.com/view/66yxwx6676
https://coub.com/view/np30bv52rn
https://coub.com/view/3nk88ivl0b
https://coub.com/view/83eyn5yj6r
https://coub.com/view/bymmywq8fr
https://coub.com/view/9bj0bp5xii
https://coub.com/view/agvyb0zhme
https://coub.com/view/8bcvp8upb3
https://coub.com/view/c8p7zv5zan
https://coub.com/view/yry7la9tco
https://coub.com/view/ho2dopbp7j
https://coub.com/view/amubk2uwuc
https://coub.com/view/2jwoy1e4u4
https://coub.com/view/ezz7hv58aq
https://coub.com/view/sxb8y6f7cm
https://coub.com/view/y4kvuhob20
https://coub.com/view/t3iyg4zbk6
https://coub.com/view/hlu3q922ul
https://coub.com/view/wr5t1i5hjn
https://coub.com/view/ig12bx6jem
https://coub.com/view/elh7b4dm2v
https://coub.com/view/p3aozbkqis
https://coub.com/view/1x387gqa9h
https://coub.com/view/11vq0o0urq
https://coub.com/view/n95945i2xa
https://coub.com/view/kwxot0bqpd
https://coub.com/view/0bzsnthjf8
https://coub.com/view/l6n7cci0v1
https://coub.com/view/e33t42b2e8
https://coub.com/view/uufau92iew
https://coub.com/view/8nu2gzpi87
https://coub.com/view/bzvc0eyha5
https://coub.com/view/m7i31eulsq

0 关注 分享

要回复文章请先登录注册