How can you “center crop” a Widget in Flutter?

前端 未结 4 649
自闭症患者
自闭症患者 2020-12-29 00:00

I have a VideoPlayer widget that needs to be fullscreen and also fit the aspect ratio of the source video. In order to achieve that, I\'ll need to chop off either the top/bo

4条回答
  •  忘掉有多难
    2020-12-29 00:18

    Finally solved it. There were a few missing pieces:

    1. I needed an OverflowBox with no restrictions so that my child could grow as large as needed.
    2. I needed the child of FittedBox to actually enforce a size to stop the layout system from crashing.
    3. Now that my widget will paint outside of its bounds, we also want to put a ClipRect in there to stop this from happening.
    final Size size = controller.value.size;
    return new ClipRect(
      child: new OverflowBox(
        maxWidth: double.infinity,
        maxHeight: double.infinity,
        alignment: Alignment.center,
        child: new FittedBox(
          fit: BoxFit.cover,
          alignment: Alignment.center,
          child: new Container(
            width: size.width,
            height: size.height,
            child: new VideoPlayer(controller)
          )
        )
      )
    );
    

提交回复
热议问题