【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),
                  ),
相关推荐
FungLeo6 分钟前
Flutter 带 TTL 的多级缓存设计:内存+磁盘+网络三层实战
网络·flutter·缓存·性能优化
FungLeo1 小时前
Flutter 可复用公共组件库设计与落地:AppDialog/BottomSheet 等实战
flutter
Fungleo10699716 小时前
Flutter/Android Release 包连不上网?AndroidManifest INTERNET 权限排查实录
flutter
GitLqr18 小时前
Flutter 实战:为你的 App 增加桌面快捷方式 (Quick Actions)
flutter·app·全栈
FungLeo19 小时前
Flutter 把第三方 UI 库渐进迁回 Material 3:组件映射总表 + 四批次替换实战
flutter·ui
GitLqr2 天前
iOS 27 强制要求 UISceneDelegate:UIKit 和 Flutter 开发者该如何应对?
flutter·ios·全栈
蜡台2 天前
Flutter HTTP 请求完整详解
网络协议·flutter·http·dart
9765033352 天前
iOS 上架/审核 4.3a Cocos 2026最新方案解读
flutter·ios·swift·cocos2d·ios开发
FungLeo3 天前
Flutter 接入 Alice 调试浮窗:一个顶层 final 抢跑,把 release 网络整没了
网络·flutter
恋猫de小郭3 天前
Flutter hit,一个可以灵活控制溢出点击的第三方包
android·前端·flutter