mod: 直播弹幕开关

This commit is contained in:
guozhigq
2024-08-23 23:47:15 +08:00
parent ddaf258062
commit 05361c7fdd
3 changed files with 105 additions and 55 deletions

View File

@ -129,7 +129,7 @@ class _PlDanmakuState extends State<PlDanmaku> {
// double initDuration = box.maxWidth / 12; // double initDuration = box.maxWidth / 12;
return Obx( return Obx(
() => AnimatedOpacity( () => AnimatedOpacity(
opacity: playerController.isOpenDanmu.value ? 1 : 1, opacity: playerController.isOpenDanmu.value ? 1 : 0,
duration: const Duration(milliseconds: 100), duration: const Duration(milliseconds: 100),
child: DanmakuView( child: DanmakuView(
createdController: (DanmakuController e) async { createdController: (DanmakuController e) async {

View File

@ -45,6 +45,8 @@ class LiveRoomController extends GetxController {
TextEditingController inputController = TextEditingController(); TextEditingController inputController = TextEditingController();
// 加入直播间提示 // 加入直播间提示
RxMap<String, String> joinRoomTip = {'userName': '', 'message': ''}.obs; RxMap<String, String> joinRoomTip = {'userName': '', 'message': ''}.obs;
// 直播间弹幕开关 默认打开
RxBool danmakuSwitch = true.obs;
@override @override
void onInit() { void onInit() {
@ -69,6 +71,9 @@ class LiveRoomController extends GetxController {
userId = userInfo.mid; userId = userInfo.mid;
} }
liveDanmakuInfo().then((value) => initSocket()); liveDanmakuInfo().then((value) => initSocket());
danmakuSwitch.listen((p0) {
plPlayerController.isOpenDanmu.value = p0;
});
} }
playerInit(source) async { playerInit(source) async {
@ -87,6 +92,7 @@ class LiveRoomController extends GetxController {
enableHA: true, enableHA: true,
autoplay: true, autoplay: true,
); );
plPlayerController.isOpenDanmu.value = danmakuSwitch.value;
} }
Future queryLiveInfo() async { Future queryLiveInfo() async {
@ -185,6 +191,7 @@ class LiveRoomController extends GetxController {
} else if (liveMsg.first.type == LiveMessageType.join || } else if (liveMsg.first.type == LiveMessageType.join ||
liveMsg.first.type == LiveMessageType.follow) { liveMsg.first.type == LiveMessageType.follow) {
// 每隔一秒依次liveMsg中的每一项赋给activeUserName // 每隔一秒依次liveMsg中的每一项赋给activeUserName
int index = 0; int index = 0;
Timer.periodic(const Duration(seconds: 2), (timer) { Timer.periodic(const Duration(seconds: 2), (timer) {
if (index < liveMsg.length) { if (index < liveMsg.length) {
@ -200,6 +207,7 @@ class LiveRoomController extends GetxController {
timer.cancel(); timer.cancel();
} }
}); });
return; return;
} }
// 过滤出聊天消息 // 过滤出聊天消息
@ -223,7 +231,9 @@ class LiveRoomController extends GetxController {
}).toList(); }).toList();
// 添加到 danmakuController // 添加到 danmakuController
danmakuController?.addItems(danmakuItems); if (danmakuSwitch.value) {
danmakuController?.addItems(danmakuItems);
}
} }
}, },
onErrorCb: (e) { onErrorCb: (e) {

View File

@ -325,7 +325,33 @@ class _LiveRoomPageState extends State<LiveRoomPage>
), ),
child: Row( child: Row(
children: [ children: [
const SizedBox(width: 4), SizedBox(
width: 34,
height: 34,
child: Obx(
() => IconButton(
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
backgroundColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
return Colors.grey.withOpacity(0.1);
}),
),
onPressed: () {
_liveRoomController.danmakuSwitch.value =
!_liveRoomController.danmakuSwitch.value;
},
icon: Icon(
_liveRoomController.danmakuSwitch.value
? Icons.subtitles_outlined
: Icons.subtitles_off_outlined,
size: 19,
color: Colors.white,
),
),
),
),
const SizedBox(width: 8),
Expanded( Expanded(
child: TextField( child: TextField(
controller: _liveRoomController.inputController, controller: _liveRoomController.inputController,
@ -340,11 +366,19 @@ class _LiveRoomPageState extends State<LiveRoomPage>
), ),
), ),
), ),
IconButton( SizedBox(
onPressed: () => _liveRoomController.sendMsg(), width: 34,
icon: const Icon( height: 34,
Icons.send, child: IconButton(
color: Colors.white, style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
),
onPressed: () => _liveRoomController.sendMsg(),
icon: const Icon(
Icons.send,
color: Colors.white,
size: 20,
),
), ),
), ),
], ],
@ -418,56 +452,62 @@ Widget buildMessageListUI(
).createShader(bounds); ).createShader(bounds);
}, },
blendMode: BlendMode.dstIn, blendMode: BlendMode.dstIn,
child: ListView.builder( child: GestureDetector(
controller: scrollController, onTap: () {
itemCount: liveRoomController.messageList.length, // 键盘失去焦点
itemBuilder: (context, index) { FocusScope.of(context).requestFocus(FocusNode());
final LiveMessageModel liveMsgItem = },
liveRoomController.messageList[index]; child: ListView.builder(
return Align( controller: scrollController,
alignment: Alignment.centerLeft, itemCount: liveRoomController.messageList.length,
child: Container( itemBuilder: (context, index) {
decoration: BoxDecoration( final LiveMessageModel liveMsgItem =
color: Colors.grey.withOpacity(0.1), liveRoomController.messageList[index];
borderRadius: const BorderRadius.all(Radius.circular(20)), return Align(
), alignment: Alignment.centerLeft,
margin: EdgeInsets.only( child: Container(
top: index == 0 ? 20.0 : 0.0, decoration: BoxDecoration(
bottom: 6.0, color: Colors.grey.withOpacity(0.1),
left: 14.0, borderRadius: const BorderRadius.all(Radius.circular(20)),
right: 14.0, ),
), margin: EdgeInsets.only(
padding: const EdgeInsets.symmetric( top: index == 0 ? 20.0 : 0.0,
vertical: 3.0, bottom: 6.0,
horizontal: 10.0, left: 14.0,
), right: 14.0,
child: Text.rich( ),
TextSpan( padding: const EdgeInsets.symmetric(
style: const TextStyle(color: Colors.white), vertical: 3.0,
children: [ horizontal: 10.0,
TextSpan( ),
text: '${liveMsgItem.userName}: ', child: Text.rich(
style: TextStyle( TextSpan(
color: Colors.white.withOpacity(0.6), style: const TextStyle(color: Colors.white),
children: [
TextSpan(
text: '${liveMsgItem.userName}: ',
style: TextStyle(
color: Colors.white.withOpacity(0.6),
),
recognizer: TapGestureRecognizer()
..onTap = () {
// 处理点击事件
print('Text clicked');
},
), ),
recognizer: TapGestureRecognizer() TextSpan(
..onTap = () { children: [
// 处理点击事件 ...buildMessageTextSpan(context, liveMsgItem)
print('Text clicked'); ],
}, // text: liveMsgItem.message,
), ),
TextSpan( ],
children: [ ),
...buildMessageTextSpan(context, liveMsgItem)
],
// text: liveMsgItem.message,
),
],
), ),
), ),
), );
); },
}, ),
), ),
), ),
), ),