如何裁剪窗口小部件,或剪切特定大小的窗口小部件的正方形部分?

问题描述 投票:0回答:2

我想裁剪一个CameraPreview小部件,这样我只能得到我想切割它的确切尺寸和位置。

目前我可以使用ClipRect剪辑它,但是我得到了这个黑色区域,其中小部件被剪掉了我要删除的部分(请参阅下面的替换图片)

让我们说我们有这样的小部件

 --------------                       
|88888888888888|                               
|88888888888888|                   
|88888888888888|                     
|88888888888888|                      
|88888888888888|                     
|88888888888888|                     
 --------------  

我需要裁剪小部件,(不是剪辑)

 --------------                       
|              |                               
|              |                    
|         888  |                      -----
|         888  |                     | 888 | 
|         888  |                     | 888 | 
|              |                     | 888 |
 --------------                       -----
     CLIPPING                        CROPPING

任何人都可以帮我修剪小部件吗?

dart flutter flutter-layout
2个回答
0
投票

试试这个

final Size size = controller.value.size;
return ClipRect(
  child: OverflowBox(
    maxWidth: double.infinity,
    maxHeight: double.infinity,
    alignment: Alignment.center,
    child: FittedBox(
      fit: BoxFit.cover,
      alignment: Alignment.center,
      child: new Container(
        width: size.width,
        height: size.height,
        child: CameraPreview(controller)
      )
    )
  )
);

0
投票

没关系,我设法自己解决了它,感觉像颤抖的框架以神秘的方式工作,直到我想出来

return Container( // just a parent
      child: Align( // important
        alignment: Alignment.center,
        child: Container( // just a parent
          width: some_width,  
          height: some_height,  
          child: SizedBox(
            width: width,  // final width of cropped portion
            height: width,  // final height of cropped portion
            child: OverflowBox(
              alignment: Alignment(-1,-1), // gives you top left portion of the size above, (1,1) gives bottom right, right direction is positive x, downward direction is positive y, see about Alignment on flutter docs for more details 
              maxWidth: double.infinity,
              maxHeight: double.infinity,
              child: Container(
                width: width,
                height: width,
                child: ClipRect(
                  clipper: RectClipper(i, width / 4),// this is a custom clipper i made of type CustomClipper<Rect>
                  child: CameraPreview(controller),
                ),
              ),
            ),
          ),
        ),
      ),
    );
© www.soinside.com 2019 - 2024. All rights reserved.