颤振截图

时间:2021-02-04 08:47:42

标签: flutter screenshot

我正在尝试为我的 Flutter 应用程序截屏并分享功能。我也试过下面的lib

  1. screenshot_share
  2. flutter_screenshot
  3. screenshots

我在所有 3 个库中都面临类似的问题,是: Unhandled Exception: NoSuchMethodError: The method 'findRenderObject' was called on null.

下面是我想截屏的小部件,我使用了flutter_hooksscreenshots,我就是按照这个Medium

class MyPage extends HookWidget {

  @override
  Widget build(BuildContext context) {
    final screenshotController = useMemoized(() => ScreenshotController());

    _takeScreenshotandShare() async {
      screenshotController.capture(delay: Duration(milliseconds: 10), pixelRatio: 2.0).then((File image) async {
        final directory = (await getApplicationDocumentsDirectory()).path;
        Uint8List pngBytes = image.readAsBytesSync();
        File imgFile = new File('$directory/screenshot.png');
        imgFile.writeAsBytes(pngBytes);
        print("File Saved to Gallery");
        await Share.file('Anupam', 'screenshot.png', pngBytes, 'image/png');
      }).catchError((onError) {
        print("_takeScreenshotandShare ${onError}");
      });
    }

    return ...
  }
}

挖掘lib pub-cache,错误来自全局键,它变成了null enter image description here

1 个答案:

答案 0 :(得分:0)

您无需任何插件即可完成:) 代码:-

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static GlobalKey previewContainer = new GlobalKey();
  int _counter = 0;
  int i=0;
  File imgFile;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: RepaintBoundary(
        key: previewContainer,
        child: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Text(
                'You have pushed the button this many times:',
              ),
              new Text(
                '$_counter',
                style: Theme.of(context).textTheme.display1,
              ),
              new RaisedButton(
                onPressed: ()=>Timer(Duration(milliseconds: 10),takeScreenShot),
                child: const Text('Take a Screenshot'),
              ),
              Container(
                height: 400,
                child: imgFile!=null?Image.file(imgFile):Container(),
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
  takeScreenShot() async{
    i++;
    RenderRepaintBoundary boundary = previewContainer.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    final directory = (await getApplicationDocumentsDirectory()).path;
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    setState(() {
      imgFile =new File('$directory/screenshot$i.png');
      imgFile.writeAsBytes(pngBytes);
    });
  }
}