【问题标题】:How do I crop a widget, or cut out a square portion of a widget of that particular size?如何裁剪小部件,或切出特定大小的小部件的正方形部分?
【发布时间】:2019-03-24 18:54:12
【问题描述】:

我想裁剪 CameraPreview 小部件,以便只获得我想要裁剪它的确切大小和位置。

目前我可以使用ClipRect 对其进行剪辑, 但是我在小部件被剪掉的地方周围出现了这个黑色区域,我想删除它(请参阅下面的替换图)

假设我们有一个这样的小部件

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

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

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

谁能帮我裁剪一个小部件?

【问题讨论】:

    标签: dart flutter flutter-layout


    【解决方案1】:

    没关系,我自己设法解决了, 感觉 Flutter 框架以神秘的方式工作,直到我弄清楚了这一点

    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),
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
    

    【讨论】:

    • RectClipper 有什么作用?它是否将偏移设置到小部件中以进行剪辑?
    【解决方案2】:

    试试这个

    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)
          )
        )
      )
    );
    

    【讨论】:

    • 我试过了,但这是用于中心裁剪,我想在我想要的确切位置和大小处裁剪小部件。
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多