| | |
| | | import 'dart:io'; |
| | | import 'dart:typed_data'; |
| | | import 'dart:ui'; |
| | | |
| | | import 'package:flutter/cupertino.dart'; |
| | | import 'package:flutter/material.dart'; |
| | | import 'package:flutter/rendering.dart'; |
| | | import 'package:path_provider/path_provider.dart'; |
| | | |
| | | |
| | | ///对widget截图 |
| | | |
| | | |
| | | class CaptureWidget extends StatelessWidget { |
| | | //截图组件 |
| | | GlobalKey rootWidgetKey = GlobalKey(); |
| | | final Widget widget; |
| | | final CaptureController captureController; |
| | | |
| | | CaptureWidget(this.widget); |
| | | CaptureWidget({required this.widget, required this.captureController}) { |
| | | captureController.setGlobalKey(rootWidgetKey); |
| | | } |
| | | |
| | | @override |
| | | Widget build(BuildContext context) { |
| | | return RepaintBoundary(key: rootWidgetKey, child: widget); |
| | | } |
| | | |
| | | Future<Uint8List?> capturePng() async { |
| | | } |
| | | |
| | | class CaptureController { |
| | | |
| | | GlobalKey? _globalKey; |
| | | |
| | | setGlobalKey(GlobalKey globalKey) { |
| | | _globalKey = globalKey; |
| | | } |
| | | |
| | | |
| | | |
| | | Future<File?> capturePng(String path) async { |
| | | try { |
| | | RenderRepaintBoundary? boundary = rootWidgetKey.currentContext! |
| | | 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(); |
| | | return pngBytes; |
| | | File(path).writeAsBytesSync(pngBytes); |
| | | return File(path); |
| | | } catch (e) {} |
| | | return null; |
| | | } |
| | | |
| | | } |