【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),
                  ),
相关推荐
黄林晴2 小时前
Compose Multiplatform 1.10 发布:统一 Preview、Navigation 3、Hot Reload 三箭齐发
android·flutter
Swift社区3 小时前
Flutter 应该按功能拆,还是按技术层拆?
flutter
肠胃炎3 小时前
树形选择器组件封装
前端·flutter
程序员老刘18 小时前
跨平台开发地图:金三银四你准备好了吗? | 2026年3月
flutter·客户端
恋猫de小郭18 小时前
Kotlin 在 2.0 - 2.3 都更新了什么特性,一口气带你看完这两年 Kotlin 更新
android·前端·flutter
左手厨刀右手茼蒿20 小时前
Flutter 三方库 all_lint_rules_community 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、基于全量社区 Lint 规则的工业级静态代码质量与安全审计引擎
flutter·harmonyos·鸿蒙·openharmony·all_lint_rules_community
雷帝木木20 小时前
Flutter for OpenHarmony:Flutter 三方库 cbor 构建 IoT 设备的极致压缩防窃协议(基于标准二进制 JSON 表达格式)
网络·物联网·flutter·http·json·harmonyos·鸿蒙
Zender Han20 小时前
Flutter Bloc / Cubit 最新详解与实战指南(2026版)
android·flutter·ios
王码码203520 小时前
Flutter 三方库 servicestack 的鸿蒙化适配指南 - 实现企业级 Message-based 架构集成、支持强类型 JSON 序列化与跨端服务调用同步
flutter·harmonyos·鸿蒙·openharmony·message-based