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