build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
final controller = TCICController.instance;
// 实现小视频布局的内容
// 纯音视频课,不管多少人都依次排列即可,老师和学生端一致、
return Obx(() {
final streamInfoList = controller.getTrtcStreamInfoObs().trtcStreamsInfo;
final memberCount = controller.getMembersInfoObs().memberIds.length;
final onStageMemberIds =
controller
.getMembersInfoObs()
.memberIds
.where(
(memberId) =>
controller
.getMembersInfoObs()
.membersInfo[memberId]
?.stage ==
1,
)
.toList();
final teacherId =
controller.getClassInfoObs().getClassInfo().roomInfo.teacherId;
final selfId = controller.getConfig().userId;
final teacherPlaceholder = 'teacherId';
// 重新排序:老师排第一,自己排第二,其余用户顺序不变
onStageMemberIds.sort((a, b) {
// 获取用户角色信息
final aMemberInfo = controller.getMembersInfoObs().membersInfo[a];
final bMemberInfo = controller.getMembersInfoObs().membersInfo[b];
// 如果a是老师,a排在前面
if (aMemberInfo?.role == RoleEnum.teacher.index) return -1;
// 如果b是老师,b排在前面
if (bMemberInfo?.role == RoleEnum.teacher.index) return 1;
// 如果a是助教,且b不是老师,a排在前面
if (aMemberInfo?.role == RoleEnum.assistant.index &&
bMemberInfo?.role != RoleEnum.teacher.index)
return -1;
// 如果b是助教,且a不是老师,b排在前面
if (bMemberInfo?.role == RoleEnum.assistant.index &&
aMemberInfo?.role != RoleEnum.teacher.index)
return 1;
// 如果a是自己,且b不是老师和助教,a排在前面
if (a == selfId &&
bMemberInfo?.role != RoleEnum.teacher.index &&
bMemberInfo?.role != RoleEnum.assistant.index)
return -1;
// 如果b是自己,且a不是老师和助教,b排在前面
if (b == selfId &&
aMemberInfo?.role != RoleEnum.teacher.index &&
aMemberInfo?.role != RoleEnum.assistant.index)
return 1;
// 其他情况保持原有顺序
return 0;
});
// 计算上台人数(stage=1)
int onStageCount = onStageMemberIds.length;
bool isTeacherInRoom = false;
for (var memberId in onStageMemberIds) {
if (memberId == teacherId) {
isTeacherInRoom = true;
break;
}
}
if (!isTeacherInRoom && selfId != teacherId) {
onStageCount++;
onStageMemberIds.insert(0, teacherPlaceholder);
}
// 获取当前页的数据
TCICLog.info(
'SmallAVClassLayout memberCount: $memberCount, 上台人数: $onStageCount streamInfoList ${streamInfoList.length} ${streamInfoList.map((e) => e.userId).toList()} onStageCount $onStageCount teacherId $teacherId',
actionModule: ActionModule.smallAVClassLayout.name,
actionName: ActionName.buildContent.name,
);
if (onStageCount == 0) {
return Container(
color: const Color(0xFF1A1E2E),
child: Center(
child: BaseText(
StringEnum.noUserJoined,
style: TextStyle(color: Colors.white),
),
),
);
}
final cacheExtent = TCICConst.cacheExtent;
if (onStageMemberIds.length == 1 &&
onStageMemberIds[0] == controller.getConfig().userId) {
// 只有老师在台上,渲染全屏16/9视频
return SafeArea(
child: Align(
alignment: Alignment.center,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height, // 最大高度
),
child: Center(
child: AspectRatio(
aspectRatio: 16 / 9,
child: VideoWrapComponent(
userId: controller.getConfig().userId,
streamType: LiveStreamType.main,
),
),
),
),
),
);
}
return SafeArea(
child: Align(
alignment: Alignment.center,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height * (2 / 3), // 最大高度
),
child: ListView.builder(
scrollDirection: Axis.horizontal,
cacheExtent: cacheExtent,
itemBuilder: (c, i) {
final memberId = onStageMemberIds[i];
return Padding(
padding: const EdgeInsets.fromLTRB(8, 4, 8, 4),
child: Container(
decoration: BoxDecoration(
color: const Color(0xFF282828),
borderRadius: BorderRadius.circular(4),
),
child: AspectRatio(
aspectRatio: 16 / 9,
child:
memberId == teacherPlaceholder
? Center(
child: BaseText(
StringEnum.teacherComing,
style: TextStyle(
color: Colors.white,
fontSize: 12,
),
),
)
: VideoWrapComponent(
userId: memberId,
streamType: LiveStreamType.main,
),
),
),
);
},
itemCount: onStageCount,
),
),
),
);
});
}