admin
2021-12-03 f092e392700f68cdbfc545c9801f530d19fd39fa
lib/ui/widget/capture.dart
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';
@@ -5,7 +6,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
///对widget截图
@@ -23,30 +23,25 @@
  Widget build(BuildContext context) {
    return RepaintBoundary(key: rootWidgetKey, child: widget);
  }
}
class CaptureController {
  GlobalKey? _globalKey;
  setGlobalKey(GlobalKey globalKey) {
    _globalKey = globalKey;
  }
  Future<File?> capturePng(String path) async {
  Future<String> capturePng() async {
    try {
      RenderRepaintBoundary? boundary = _globalKey!.currentContext!
          .findRenderObject() as RenderRepaintBoundary;
      var image = await boundary.toImage(pixelRatio: 3.0);
      ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
      Uint8List pngBytes = byteData!.buffer.asUint8List();
      File(path).writeAsBytesSync(pngBytes);
      return File(path);
      String str = base64.encode(pngBytes);
      return str;
    } catch (e) {}
    return null;
    return "";
  }
}