@Entry
@Component
export struct VideoBindSheet {
@State videoSrc: Resource = $rawfile('harmonyos-next-pv-video-popup.mp4'); // 资源更换为实际视频
@State previewUri: Resource = $r('app.media.img'); // 资源更换为实际预览图
@State curSpeed: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X;
@State showFirstFrame: boolean = false;
@State showBindSheet: boolean = false;
@State containerHeight: number | undefined = undefined;
@State sheetHeight: number = 0;
controller: VideoController = new VideoController();
// 半模态页面
@Builder
bindSheetBuilder() {
Column() {
Text('Comment')
.height(200)
.width('100%')
.textAlign(TextAlign.Center)
.fontSize('30fp');
TextInput({ placeholder: 'comment' })
.width('95%')
.fontSize('18fp')
.type(InputType.Normal);
};
}
build() {
Column() {
Column() {
Video({
src: this.videoSrc,
previewUri: this.previewUri,
currentProgressRate: this.curSpeed,
controller: this.controller,
posterOptions: { showFirstFrame: this.showFirstFrame }
})
.width('100%')
.height(this.containerHeight === undefined ? '100%' :
this.containerHeight - this.sheetHeight) // 首次布局时撑满容器,后续随半模态页面高度变化
.objectFit(ImageFit.Contain) // 边界内完全显示
.onFinish(() => {
this.controller.reset();
});
Button('Comment')
.type(ButtonType.Capsule)
.position({ right: 10, bottom: 50 + this.sheetHeight })
.onClick(() => {
// 拉起半模态页面
this.showBindSheet = true;
});
}
.height('100%')
.width('100%')
.bindSheet(
!!this.showBindSheet,
this.bindSheetBuilder(),
{
height: 450,
preferType: SheetType.POPUP, // 跟手弹窗
showClose: true,
placement: Placement.Bottom, // 底部弹窗
shouldDismiss: () => {
// 页面交互式关闭时,修改显示状态为false,半模态页面高度重置为0
this.showBindSheet = false;
this.sheetHeight = 0;
},
onHeightDidChange: (height: number) => {
// 根据每一帧高度,动态修改页面高度,需注意处理导航栏高度,导航栏高度为91px或28vp;若页面不显示,则重置为0
this.sheetHeight = this.getUIContext().px2vp(height - 91);
if (!this.showBindSheet) {
this.sheetHeight = 0;
}
},
onWillAppear: () => this.sheetHeight = 422 // 半模态页面高度450vp,需避让导航栏高度28vp,实际Video抬起高度为422vp
}
)
.onAreaChange((oldValue, newValue) => {
console.info(`Old Height: ${oldValue.height}`);
// 获取容器高度
this.containerHeight = newValue.height as number;
});
}
.height('100%')
.width('100%')
.justifyContent(FlexAlign.Center)
.expandSafeArea([SafeAreaType.SYSTEM])
.backgroundColor(Color.Black);
}
} 效果图:

import { window, KeyboardAvoidMode } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
// 创建LocalStorage实例并初始化
let para: Record<string, number> = { 'dialogHeight': 0 };
let storage: LocalStorage = new LocalStorage(para);
// NavDestination组件构造函数
@Builder
function pageBuilder(name: string) {
if (name === 'VideoPage') {
VideoPage();
} else if (name === 'CommentPage') {
CommentPage();
} else if (name === 'PersonPage') {
PersonPage();
}
}
// Entry页面添加storage
@Entry(storage)
@Component
export struct VideoNavigation {
@Provide pageInfos: NavPathStack = new NavPathStack();
build() {
Navigation(this.pageInfos) {
Column() {
Button('跳转Video')
.onClick(() => {
this.pageInfos.pushPath({ name: 'VideoPage', param: this.pageInfos }); // 跳转至Video组件页面
});
}
.height(200)
.justifyContent(FlexAlign.Center);
}
.hideTitleBar(true)
.mode(NavigationMode.Stack) // 单栏模式
.navDestination(pageBuilder); // 创建NavDestination组件
}
}
// Video页面
@Component
struct VideoPage {
@Consume pageInfos: NavPathStack = new NavPathStack();
controller: VideoController = new VideoController();
@State videoSrc: Resource = $rawfile('harmonyos-next-pv-video-popup.mp4'); // 资源更换为实际视频
@State previewUri: Resource = $r('app.media.img'); // 资源更换为实际预览图
@State curSpeed: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X;
@State showFirstFrame: boolean = false;
@State containerHeight: number | undefined = undefined;
@LocalStorageLink('dialogHeight') dialogHeight: number = 0; // 与LocalStorage中对应属性建立双向绑定
build() {
NavDestination() {
Column() {
Video({
src: this.videoSrc,
previewUri: this.previewUri,
currentProgressRate: this.curSpeed,
controller: this.controller,
posterOptions: { showFirstFrame: this.showFirstFrame }
})
.width('100%')
// 首次布局时撑满容器,后续随半模态页面高度变化
.height(this.containerHeight === undefined ? '100%' : this.containerHeight - this.dialogHeight)
.objectFit(ImageFit.Contain) // 边界内完全显示
.onFinish(() => {
this.controller.reset();
});
Button('Comments')
.type(ButtonType.Capsule)
.position({ right: 10, bottom: 50 + this.dialogHeight })
.onClick(() => {
this.pageInfos.pushPath({ name: 'CommentPage' }); // 跳转至弹窗页面
});
}
.height('96%')
.onAreaChange((oldValue, newValue) => {
console.info(`Old Height: ${oldValue.height}`);
this.containerHeight = newValue.height as number; // 获取容器高度
});
}
.height('100%')
.hideTitleBar(true)
.hideToolBar(true)
.backgroundColor(Color.Black)
.ignoreLayoutSafeArea([LayoutSafeAreaType.SYSTEM]);
}
}
// 弹窗页面
@Component
struct CommentPage {
@Consume pageInfos: NavPathStack;
@LocalStorageLink('dialogHeight') dialogHeight: number = 0; // 与LocalStorage中对应属性建立双向绑定
@State preHeight: number = 0;
@State commentContainerHeight: number | undefined = undefined;
@State keyboardHeight: number = 0;
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
aboutToAppear(): void {
window.getLastWindow(this.context, (err, currentWindow) => {
currentWindow.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.NONE); // 不避让键盘
currentWindow.on('keyboardHeightChange', (data) => {
let keyboardHgt = this.getUIContext().px2vp(data); // 获取键盘高度
this.keyboardHeight = keyboardHgt - 28; // 获取避让高度,28为导航栏高度
});
console.info(`err: ${err.message}`);
});
}
build() {
NavDestination() {
Stack({ alignContent: Alignment.Bottom }) {
Column() {
Text('Slips Area')
.fontSize('16fp')
.textAlign(TextAlign.Center)
.width('100%')
.backgroundColor(Color.Gray)
.borderColor({ bottom: Color.Black })
.gesture(
// 顺序识别手势组合
GestureGroup(GestureMode.Sequence,
LongPressGesture({ repeat: true, duration: 100 }), // 长按手势
PanGesture({ direction: PanDirection.Vertical }) // 滑动手势,识别纵向滑动
.onActionStart(() => {
this.preHeight = this.dialogHeight; // 当前弹窗高度
})
.onActionUpdate((event) => {
// 动态修改弹窗高度,通过LocalStorage同步传入Video页面,以修改Video组件高度
this.dialogHeight = this.preHeight - event.offsetY;
// 动态修改外容器高度
this.commentContainerHeight = this.dialogHeight;
})
)
);
Column({ space: 24 }) {
TextInput({ placeholder: 'My Comment' })
.fontSize('18fp')
.type(InputType.Normal)
.width('92%')
.offset({ bottom: this.keyboardHeight }); // 输入框偏移避让高度
Button('Persons')
.onClick(() => {
this.pageInfos.pushPath({ name: 'PersonPage' }); // 跳转至人员页面
});
Text('Comments')
.fontSize('30fp')
.textAlign(TextAlign.Center)
.height(200)
.width('100%');
}
.layoutWeight(1)
.padding({ bottom: 48 })
.reverse(true);
}
.height(this.commentContainerHeight ?? '60%')
.backgroundColor(Color.White)
.onAreaChange((oldValue, newValue) => {
console.info(`Old Height: ${oldValue.height}`);
this.dialogHeight = newValue.height as number; // 获取弹窗高度
this.commentContainerHeight = this.dialogHeight; // 获取外容器高度
});
}
.height('100%')
.backgroundColor(Color.Transparent)
.onClick(() => {
this.pageInfos.pop(); // 点击遮罩层,弹窗页面弹出路由栈
});
}
.mode(NavDestinationMode.DIALOG) // 弹窗模式
.hideTitleBar(true)
.onWillDisappear(() => {
this.dialogHeight = 0;
})
.ignoreLayoutSafeArea([LayoutSafeAreaType.SYSTEM]);
}
}
// 人员页面
@Component
struct PersonPage {
@Consume pageInfos: NavPathStack;
build() {
NavDestination() {
Column() {
Text('Person Page')
.fontSize('30fp');
}
.height(300)
.justifyContent(FlexAlign.Center)
.onClick(() => {
this.pageInfos.pop(); // 当前页面弹出路由栈
});
};
}
} 效果图:
