提交 727e4860 authored 作者: Sachin Ganesh's avatar Sachin Ganesh

Merge remote-tracking branch 'origin/nullsafety'

## [1.0.0-nullsafety.0] - 10/02/2021
* Add nullsafety
## [0.3.0] - 10/02/2021 ## [0.3.0] - 10/02/2021
* breaking change capture method returns Uint8List instead of File. * breaking change capture method returns Uint8List instead of File.
* support for web and windows * support for web and windows
......
...@@ -11,8 +11,8 @@ version: 1.0.0+1 ...@@ -11,8 +11,8 @@ version: 1.0.0+1
publish_to: none publish_to: none
environment: environment:
sdk: '>=2.8.0 <3.0.0' # sdk: '>=2.8.0 <3.0.0'
# sdk: '>=2.12.0-259.8.beta <3.0.0' sdk: '>=2.12.0-259.8.beta <3.0.0'
dependencies: dependencies:
flutter: flutter:
......
...@@ -16,26 +16,28 @@ import 'dart:ui' as ui; ...@@ -16,26 +16,28 @@ import 'dart:ui' as ui;
/// ///
/// ///
class ScreenshotController { class ScreenshotController {
GlobalKey? _containerKey; late GlobalKey _containerKey;
ScreenshotController() { ScreenshotController() {
_containerKey = GlobalKey(); _containerKey = GlobalKey();
} }
/// Captures image and saves to given path /// Captures image and saves to given path
Future<String> captureAndSave( Future<String?> captureAndSave(
String directory, { String directory, {
String? fileName, String? fileName,
double? pixelRatio, double? pixelRatio,
Duration delay: const Duration(milliseconds: 20), Duration delay = const Duration(milliseconds: 20),
}) async { }) async {
Uint8List content = await capture( Uint8List? content = await capture(
pixelRatio: pixelRatio, pixelRatio: pixelRatio,
delay: delay, delay: delay,
); );
if (content != null) {
PlatformFileManager fileManager = PlatformFileManager();
PlatformFileManager fileManager = PlatformFileManager(); return fileManager.saveFile(content, directory, name: fileName);
}
return fileManager.saveFile(content, directory, name: fileName); return null;
} }
Future<Uint8List> capture({ Future<Uint8List> capture({
...@@ -43,15 +45,15 @@ class ScreenshotController { ...@@ -43,15 +45,15 @@ class ScreenshotController {
Duration delay: const Duration(milliseconds: 20), Duration delay: const Duration(milliseconds: 20),
}) { }) {
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308 //Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
return new Future.delayed(delay, () async { return new Future.delayed(delay ?? Duration(milliseconds: 20), () async {
try { try {
ui.Image image = await captureAsUiImage( ui.Image image = await captureAsUiImage(
delay: Duration.zero, delay: Duration.zero,
pixelRatio: pixelRatio, pixelRatio: pixelRatio,
); );
ByteData byteData = await (image.toByteData( ByteData? byteData =
format: ui.ImageByteFormat.png) as FutureOr<ByteData>); await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List(); Uint8List? pngBytes = byteData?.buffer.asUint8List();
return pngBytes; return pngBytes;
} catch (Exception) { } catch (Exception) {
...@@ -67,12 +69,15 @@ class ScreenshotController { ...@@ -67,12 +69,15 @@ class ScreenshotController {
return new Future.delayed(delay, () async { return new Future.delayed(delay, () async {
try { try {
RenderRepaintBoundary boundary = this RenderRepaintBoundary boundary = this
._containerKey! ._containerKey
.currentContext! .currentContext
.findRenderObject() as RenderRepaintBoundary; ?.findRenderObject() as RenderRepaintBoundary;
pixelRatio = pixelRatio ?? BuildContext? context = _containerKey.currentContext;
MediaQuery.of(_containerKey!.currentContext!).devicePixelRatio; if (pixelRatio == null) {
ui.Image image = await boundary.toImage(pixelRatio: pixelRatio!); if (context != null)
pixelRatio = pixelRatio ?? MediaQuery.of(context).devicePixelRatio;
}
ui.Image image = await boundary.toImage(pixelRatio: pixelRatio ?? 1);
return image; return image;
} catch (Exception) { } catch (Exception) {
throw (Exception); throw (Exception);
...@@ -139,8 +144,8 @@ class Screenshot<T> extends StatefulWidget { ...@@ -139,8 +144,8 @@ class Screenshot<T> extends StatefulWidget {
final ScreenshotController? controller; final ScreenshotController? controller;
const Screenshot({ const Screenshot({
Key? key, Key? key,
this.child, required this.child,
this.controller, required this.controller,
}) : super(key: key); }) : super(key: key);
@override @override
...@@ -150,15 +155,15 @@ class Screenshot<T> extends StatefulWidget { ...@@ -150,15 +155,15 @@ class Screenshot<T> extends StatefulWidget {
} }
class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin { class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
ScreenshotController? _controller; late ScreenshotController _controller;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
if (widget.controller == null) { // if (widget.controller == null) {
_controller = ScreenshotController(); // _controller = ScreenshotController();
} else // } else
_controller = widget.controller; _controller = widget.controller;
} }
// @override // @override
......
...@@ -13,6 +13,5 @@ ...@@ -13,6 +13,5 @@
// file.writeAsBytesSync(fileContent); // file.writeAsBytesSync(fileContent);
// return file.path; // return file.path;
// } // }
// } // }
import 'file_manager.dart'; import 'file_manager.dart';
PlatformFileManager getFileManager() => PlatformFileManager getFileManager() => throw UnimplementedError(
throw UnimplementedError("File Picker is Not Implementd in current platform"); "File Picker is Not Implementd in current platform");
name: screenshot name: screenshot
description: Flutter Screenshot Package (Runtime). Capture any Widget as an image. description: Flutter Screenshot Package (Runtime). Capture any Widget as an image.
version: 0.3.0 version: 1.0.0-nullsafety.0
homepage: https://github.com/SachinGanesh/screenshot homepage: https://github.com/SachinGanesh/screenshot
environment: environment:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论