【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),
                  ),
相关推荐
ljt27249606611 天前
Flutter笔记--envied
前端·笔记·flutter
加农炮手Jinx1 天前
Flutter for OpenHarmony 实战:flutter_animate 声明式动画让 UI 灵动如原生
flutter·ui·华为·harmonyos·鸿蒙
里欧跑得慢1 天前
Flutter 像素级还原:设计稿到代码的视觉无损转换技巧
前端·css·flutter·web
里欧跑得慢1 天前
Flutter 三方库 function_tree — 鸿蒙应用开发中的动态数学公式解析与计算神器,实现鸿蒙深度适配下的复杂函数逻辑运行时求值实战(适配鸿蒙 HarmonyOS Next ohos)
android·前端·安全·flutter·华为·harmonyos
吃西瓜的年年2 天前
vue3原理思维导图
vue.js·笔记·flutter
GitLqr2 天前
从代码复用走向生态:Flutter Dart Package 实战全指南
flutter·全栈·dart
SoaringHeart2 天前
Flutter 进阶:NCanvasImageLoader 让 Canvas 也能画网络图
前端·flutter
磐链科技3 天前
钱包开发中的跨平台架构:Flutter与Rust构建高性能移动端钱包
flutter·架构·rust
iFlyCai3 天前
深入理解Flutter:StatefulWidget生命周期全解析(四)
前端·javascript·flutter