BarrageSendWidget
)和显示弹幕消息的 Widget(BarrageDisplayWidget
):BarrageSendWidget
:发送弹幕消息 Widget,点击后可以拉起输入界面。BarrageDisplayWidget
:接收到弹幕消息后,会显示弹幕消息到该 Widget 上。BarrageSendWidget | BarrageDisplayWidget | LiveKit 中接入barrage组件效果 |
| | |
dependencies:flutter:sdk: flutterflutter_localizations:sdk: flutterintl: ^0.19.0# 添加 barrage 本地依赖barrage:path: ../barrage
BarrageSendController _sendController = BarrageSendController(roomId: "liveRoomId", /// liveRoomId 替换为您的直播间IDownerId: "liveOwnerId", /// liveOwnerId 替换为您的直播间主播IDselfUserId: "selfUserId", /// selfUserId 替换为您的当前登录的用户IDselfName: "selfUserName"; /// selfUserName 替换为您的当前登录的用户昵称BarrageSendWidget(controller: _sendController);
BarrageDisplayController _displayController = BarrageDisplayController(roomId: "liveRoomId", /// liveRoomId 替换为您的直播间IDownerId: "liveOwnerId", /// liveOwnerId 替换为您的直播间主播IDselfUserId: "selfUserId", /// selfUserId 替换为您的当前登录的用户IDselfName: "selfUserName"; /// selfUserName 替换为您的当前登录的用户昵称BarrageDisplayWidget(controller: _displayController);
BarrageUser barrageUser = BarrageUser();barrageUser.userId = "enterRoomUserId"; /// 弹幕上展示的用户Id信息barrageUser.userName = "enterRoomUserName"; /// 弹幕上展示的用户昵称信息barrageUser.avatarUrl = "enterRoomUserAvatar"; /// 弹幕上展示的用户头像信息barrageUser.level = "66"; /// 弹幕上展示的用户等级信息Barrage barrage = Barrage();barrage.user = barrageUser;barrage.content = "进入房间"; /// 弹幕上展示的文本内容_displayController.insertMessage(barrage);
/// 1、定义一个自定义弹幕 item 构建器class GiftBarrageItemBuilder extends CustomBarrageBuilder {@overrideWidget buildWidget(BuildContext context, Barrage barrage) { /// 当 shouldCustomizeBarrageItem 返回true时,自定义Widgetreturn const Text(barrage.content,style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700, color: Colors.red),);}@overridebool shouldCustomizeBarrageItem(Barrage barrage) { /// 当弹幕消息的数据模型,判断当前弹幕消息是否需要自定义if (barrage.extInfo.keys.isNotEmpty) {return true;}return false;}}/// 2、给 BarrageDisplayWidget 设置 setCustomBarrageBuilder_displayController.setCustomBarrageBuilder(GiftBarrageItemBuilder());
本页内容是否解决了您的问题?