首页 > 解决方案 > 颤振截图

问题描述

我试图为我的颤振应用程序做截图和分享功能。我也试过下面的lib

  1. 截图分享
  2. flutter_screenshot
  3. 截图

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

下面是我想要截屏的 Widget,我使用了 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 在此处输入图像描述

标签: flutterscreenshot

解决方案


你可以在没有任何插件的情况下做到这一点:) 代码:-

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

推荐阅读