【Flutter】 视频视频源横向、竖向问题

【Flutter】 视频视频源横向、竖向问题

源代码

复制代码
Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 212.h,
            child: Stack(
              children: [
                Container(
                  color: Color(0x0F000000),
                  constraints: const BoxConstraints.expand(),
                  child: Obx(() => controller.callStatusMap[cameraId].value ==
                      VideoCallStatus.SPEAKING
                      ? Obx(() => Transform(
                      alignment: Alignment.center,
                      transform: controller.cameraList[index].rxFlip.value
                          ? Matrix4.diagonal3Values(-1, 1, 1)
                          : Matrix4.identity(),
                      child: Transform.rotate(
                        angle: controller
                            .cameraList[index].rxAngle.value >
                            0.0
                            ? (180 *
                            pi /
                            (controller.cameraList[index].angle ?? 1))
                            : 0,
                        child: RTCVideoView(
                            controller.getRender(cameraId),
                            objectFit: RTCVideoViewObjectFit
                                .RTCVideoViewObjectFitCover,
                            mirror: true,
                            placeholderBuilder: ((ctx) => Container(
                                color: Colors.black))),
                      )))
                      : readerToStart(cameraId, index)),
                ),
                Obx(() => Visibility(
                    visible: controller.recordingStatusMap[cameraId]?.value ??
                        false,
                    child: Positioned(
                      top: 8.h,
                      left: 0,
                      right: 0,
                      child: buildRecordTimer(index),
                    ))),
              ],
            ),
          ),

现象

垂直会超出父控件。

原因分析

Transform.rotate变化时不会通知父组件,导致RTCVideoView直接超出了父组件的布局范围。

修复

关键:

1、RotatedBox替代Transform.rotate;

2、视频控件需要主动判断视频是横向还是竖向的状态:objectFit: isVertical?RTCVideoViewObjectFit.RTCVideoViewObjectFitContain:RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,

复制代码
Container(
                    color: const Color(0x0F000000),
                    width: double.infinity,
                    height: videoHeight,
                    alignment: Alignment.center,
                    child: status == VideoCallStatus.SPEAKING
                        ? LayoutBuilder(
                      builder: (context, constraints) {
                        final w = constraints.maxWidth;
                        final h = constraints.maxHeight;

                        // ✅ 自动根据方向切换布局逻辑
                        final view = RTCVideoView(
                          controller.getRender(cameraId),
                          objectFit: isVertical?RTCVideoViewObjectFit.RTCVideoViewObjectFitContain:RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
                          mirror: true,
                          placeholderBuilder: (ctx) =>
                              Container(color: Colors.black),
                        );

                        return Transform(
                          alignment: Alignment.center,
                          transform: camera.rxFlip.value
                              ? Matrix4.diagonal3Values(-1, 1, 1)
                              : Matrix4.identity(),
                          child: RotatedBox(
                            quarterTurns: isVertical ? 1 : 0, // ✅ 用 RotatedBox 解决溢出问题
                            child: FittedBox(
                              fit: isVertical
                                  ? BoxFit.fitHeight // 竖屏填满高度
                                  : BoxFit.fitWidth, // 横屏填满宽度
                              alignment: Alignment.center,
                              child: SizedBox(
                                width: w,
                                height: h,
                                child: view,
                              ),
                            ),
                          ),
                        );
                      },
                    )
                        : readerToStart(cameraId, index),
                  ),
相关推荐
浮芷.4 小时前
Flutter 框架跨平台鸿蒙开发 - 家庭健康监测云应用
科技·flutter·华为·harmonyos·鸿蒙
世人万千丶4 小时前
Flutter 框架跨平台鸿蒙开发 - 宠物语言翻译器应用
学习·flutter·华为·开源·harmonyos·鸿蒙
见山是山-见水是水4 小时前
Flutter 框架跨平台鸿蒙开发 - NPC模拟器
flutter·华为·harmonyos
提子拌饭1334 小时前
番茄时间管理:鸿蒙Flutter 实现的高效时间管理工具
android·flutter·华为·架构·开源·harmonyos·鸿蒙
浮芷.5 小时前
Flutter 框架跨平台鸿蒙开发 - 科技预言应用
科技·flutter·华为·harmonyos
李李李勃谦5 小时前
Flutter 框架跨平台鸿蒙开发 - 小众景点发现
flutter·华为·harmonyos
见山是山-见水是水5 小时前
Flutter 框架跨平台鸿蒙开发 - 邻里互助服务平台
flutter·华为·harmonyos
提子拌饭1335 小时前
3D 旋转卡片:鸿蒙Flutter 实现的交互式 3D 卡片效果
flutter·华为·harmonyos·鸿蒙
提子拌饭1335 小时前
星芒便签:鸿蒙Flutter框架 实现的美观便签应用
flutter·华为·架构·开源·harmonyos·鸿蒙
一直在想名5 小时前
Flutter 框架跨平台鸿蒙开发 - 亲子故事共创
flutter·华为·harmonyos