【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),
                  ),
相关推荐
xmdy58661 小时前
Flutter+开源鸿蒙全域智慧泊车调度管理平台 Day4 订单全流程闭环+支付核验+会员权益+个人中心开发
flutter·开源·harmonyos
W蘭2 小时前
Flutter从入门到实战-01-Dart语言基础
flutter
xuankuxiaoyao2 小时前
Vue.js 插槽、作用域插槽、商品、阶段案例
android·vue.js·flutter
恋猫de小郭2 小时前
终于,Flutter 修复 Android 中文字体异常,但是很草台,不知怎么吐槽
android·前端·flutter
xmdy58662 小时前
Flutter + 开源鸿蒙跨端实战|基于空间地理信息的城市全域智慧泊车调度与多维运维管理平台 Day3
flutter·华为·开源
UnicornDev3 小时前
【Flutter x HarmonyOS 6】魔方计时APP——挑战页面的UI设计
flutter·ui·华为·harmonyos·鸿蒙
张风捷特烈3 小时前
状态管理大乱斗#08 | MobX 源码评析 - 透明魔法
android·前端·flutter
西西学代码5 小时前
Flutter---RichText(混合文本样式)
flutter
西西学代码6 小时前
Flutter---PageView
flutter
wuxianda103020 小时前
uniapp项目上架苹果商店4.3a被拒,3天极速解决方案2026.5.8
前端·人工智能·flutter·uni-app·ios上架·苹果上架·苹果4.3a