forked from videosdk-live/videosdk-hls-flutter-sdk-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
participant_list.dart
142 lines (130 loc) · 4.7 KB
/
participant_list.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
import 'package:videosdk_hls_flutter_example/constants/colors.dart';
import 'package:videosdk_hls_flutter_example/widgets/speaker/participant_list/participant_list_item.dart';
class ParticipantList extends StatefulWidget {
final Room meeting;
const ParticipantList({Key? key, required this.meeting}) : super(key: key);
@override
State<ParticipantList> createState() => _ParticipantListState();
}
class _ParticipantListState extends State<ParticipantList> {
Map<String, Participant> _participants = {};
List<String> raisedHandParticipant = [];
@override
void initState() {
_participants.putIfAbsent(widget.meeting.localParticipant.id,
() => widget.meeting.localParticipant);
_participants.addAll(widget.meeting.participants);
addMeetingListeners(widget.meeting);
addRaiseHandListener(widget.meeting);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: secondaryColor,
appBar: AppBar(
backgroundColor: secondaryColor,
flexibleSpace: Align(
alignment: Alignment.centerLeft,
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Text(
"Participants (${widget.meeting.participants.length + 1})",
style: const TextStyle(
fontWeight: FontWeight.w700, fontSize: 18),
),
),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () => Navigator.pop(context),
),
],
),
),
automaticallyImplyLeading: false,
elevation: 0,
),
body: SafeArea(
child: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _participants.values.length,
itemBuilder: (context, index) => ParticipantListItem(
participant: _participants.values.elementAt(index),
isRaisedHand: raisedHandParticipant
.contains(_participants.values.elementAt(index).id),
onMoreOptionsSelected: (value) {
if (value == "Remove from Co-host" ||
value == "Add as a Co-host") {
log("Selected Change Mode ${_participants.values.elementAt(index).id}");
widget.meeting.pubSub.publish(
"CHANGE_MODE_${_participants.values.elementAt(index).id}",
jsonEncode({
"mode":
_participants.values.elementAt(index).mode ==
Mode.CONFERENCE
? Mode.VIEWER.name
: Mode.CONFERENCE.name
}));
} else if (value == "Remove Participant") {
log("Selected remove participnt");
_participants.values.elementAt(index).remove();
}
},
),
),
),
],
),
),
);
}
void addMeetingListeners(Room meeting) {
meeting.on(Events.participantJoined, (participant) {
if (mounted) {
final newParticipants = _participants;
newParticipants[participant.id] = participant;
setState(() => _participants = newParticipants);
}
});
meeting.on(Events.participantModeChanged, (data) {
if (mounted) {
final newParticipants = _participants;
newParticipants[data['participantId']]?.mode =
meeting.participants[data['participantId']]!.mode;
setState(() => _participants = newParticipants);
}
});
meeting.on(Events.participantLeft, (participantId) {
if (mounted) {
final newParticipants = _participants;
newParticipants.remove(participantId);
setState(() => _participants = newParticipants);
}
});
}
void addRaiseHandListener(Room meeting) {
meeting.pubSub.subscribe("RAISE_HAND", (message) {
List<String> participants = raisedHandParticipant;
setState(() {
participants.add(message.senderId);
});
Timer(Duration(seconds: 5), () {
List<String> participants = raisedHandParticipant;
setState(() {
participants.remove(message.senderId);
});
});
});
}
}