dart 如何在抖动中截取屏幕外的widget?

fzwojiic  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(75)

我正在使用RepaintBoundary对当前widget进行截图,该widget是一个listView。但它只捕获当时屏幕上可见的内容。

RepaintBoundary(
                key: src,
                child: ListView(padding: EdgeInsets.only(left: 10.0),
                  scrollDirection: Axis.horizontal,
                  children: <Widget>[
                    Align(
                        alignment: Alignment(-0.8, -0.2),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: listLabel(orientation),
                        )
                    ),

                    Padding(padding: EdgeInsets.all(5.0)),

                    Align(
                        alignment: FractionalOffset(0.3, 0.5),
                        child: Container(
                            height: orientation == Orientation.portrait? 430.0: 430.0*0.7,
                            decoration: BoxDecoration(
                                border: Border(left: BorderSide(color: Colors.black))
                            ),
                            //width: 300.0,
                            child:
                            Wrap(
                              direction: Axis.vertical,
                              //runSpacing: 10.0,
                              children: colWidget(orientation),
                            )
                        )
                    ),
                    Padding(padding: EdgeInsets.all(5.0)),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: listLabel(orientation),
                    )
                  ],
                ),
              );

字符串
截图功能:

Future screenshot() async {
    RenderRepaintBoundary boundary = src.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    print(pngBytes);
    final directory = (await getExternalStorageDirectory()).path;
File imgFile =new File('$directory/layout2.pdf');
imgFile.writeAsBytes(pngBytes);
  }


有没有什么方法,这样我就可以捕获整个listView,即,不仅是在屏幕上不可见的内容,但可滚动的内容也.或者,如果整个小部件太大,适合在一张图片,它可以在多个图像中捕获.

tuwxkamq

tuwxkamq1#

我实现了这个问题的解决方案使用这个包:Screenshot,这需要整个小部件的屏幕截图.这很容易和简单,按照PubDev或GitHub上的步骤,你可以让它工作.
OBS:要拍摄小部件的完整屏幕截图,请确保您的小部件完全可滚动,而不仅仅是其中的一部分。
(In我的情况下,我有一个ListView内的容器,并包不采取所有ListView的截图,因为我有很多itens上,所以我有 Package 我的容器内SingleChildScrollView和添加 NeverScrollableScrollPhysics 物理在ListView和它的工作!:D). Screenshot of my screen
x1c 0d1x的数据
More details in this issue

iszxjhcz

iszxjhcz2#

这让我很好奇这是否可行,所以我做了一个快速的模型来展示它确实有效。但是请注意,这样做实际上是故意破坏flutter优化的东西,所以你真的不应该在你绝对需要的地方使用它。
代码如下:

import 'dart:math';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() => runApp(MyApp());

class UiImagePainter extends CustomPainter {
  final ui.Image image;

  UiImagePainter(this.image);

  @override
  void paint(ui.Canvas canvas, ui.Size size) {
    // simple aspect fit for the image
    var hr = size.height / image.height;
    var wr = size.width / image.width;

    double ratio;
    double translateX;
    double translateY;
    if (hr < wr) {
      ratio = hr;
      translateX = (size.width - (ratio * image.width)) / 2;
      translateY = 0.0;
    } else {
      ratio = wr;
      translateX = 0.0;
      translateY = (size.height - (ratio * image.height)) / 2;
    }

    canvas.translate(translateX, translateY);
    canvas.scale(ratio, ratio);
    canvas.drawImage(image, new Offset(0.0, 0.0), new Paint());
  }

  @override
  bool shouldRepaint(UiImagePainter other) {
    return other.image != image;
  }
}

class UiImageDrawer extends StatelessWidget {
  final ui.Image image;

  const UiImageDrawer({Key key, this.image}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size.infinite,
      painter: UiImagePainter(image),
    );
  }
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  GlobalKey<OverRepaintBoundaryState> globalKey = GlobalKey();

  ui.Image image;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: image == null
            ? Capturer(
                overRepaintKey: globalKey,
              )
            : UiImageDrawer(image: image),
        floatingActionButton: image == null
            ? FloatingActionButton(
                child: Icon(Icons.camera),
                onPressed: () async {
                  var renderObject = globalKey.currentContext.findRenderObject();

                  RenderRepaintBoundary boundary = renderObject;
                  ui.Image captureImage = await boundary.toImage();
                  setState(() => image = captureImage);
                },
              )
            : FloatingActionButton(
                onPressed: () => setState(() => image = null),
                child: Icon(Icons.remove),
              ),
      ),
    );
  }
}

class Capturer extends StatelessWidget {
  static final Random random = Random();

  final GlobalKey<OverRepaintBoundaryState> overRepaintKey;

  const Capturer({Key key, this.overRepaintKey}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: OverRepaintBoundary(
        key: overRepaintKey,
        child: RepaintBoundary(
          child: Column(
            children: List.generate(
              30,
              (i) => Container(
                    color: Color.fromRGBO(random.nextInt(256), random.nextInt(256), random.nextInt(256), 1.0),
                    height: 100,
                  ),
            ),
          ),
        ),
      ),
    );
  }
}

class OverRepaintBoundary extends StatefulWidget {
  final Widget child;

  const OverRepaintBoundary({Key key, this.child}) : super(key: key);

  @override
  OverRepaintBoundaryState createState() => OverRepaintBoundaryState();
}

class OverRepaintBoundaryState extends State<OverRepaintBoundary> {
  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

字符串
它所做的是制作一个滚动视图来封装列表(列),并确保repaintBoundary围绕列。对于使用列表的代码,它不可能捕获所有的子级,因为列表本身本质上就是一个repaintBoundary。
特别要注意'overRepaintKey'和OverRepaintBoundary。你可以通过迭代渲染子对象来避免使用它,但它使它变得更容易。

lp0sw83n

lp0sw83n3#

有一个简单的方法,你需要 Package SingleChildScrollViewWidget到RepaintBoundary。只需 Package 你的可滚动部件(或他的父亲)与SingleChildScrollView

SingleChildScrollView(
  child: RepaintBoundary(
     key: _globalKey

   )
)

字符串

bweufnob

bweufnob4#

我得到了这个解决方案采取水平截图使用:-“截图”酒吧libraby和使用此代码进行截图:-

body: Screenshot(
    controller: _screenshotController,
    child: SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: RepaintBoundary(
        key: _screenshotKey,
        child: SizedBox(
          width: 810.0, // Set a fixed width or use a sized container
          child: ListView.builder(
            //physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemCount: reportData.length,
            itemBuilder: (context, reportIndex) {

字符串

相关问题