提交 de95fbdc authored 作者: ritheshSalyan's avatar ritheshSalyan

support nullsafety

上级 be886c89
......@@ -31,7 +31,7 @@ class MyApp extends StatelessWidget {
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
MyHomePage({Key? key, this.title = ""}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
......@@ -50,7 +50,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Uint8List _imageFile;
Uint8List? _imageFile;
//Create an instance of ScreenshotController
ScreenshotController screenshotController = ScreenshotController();
......@@ -100,22 +100,24 @@ class _MyHomePageState extends State<MyHomePage> {
_imageFile = null;
screenshotController
.capture(delay: Duration(milliseconds: 10))
.then((Uint8List image) async {
.then((Uint8List? image) async {
_imageFile = image;
showDialog(
context: context,
builder: (context) => Scaffold(
appBar: AppBar(
title: Text("CAPURED SCREENSHOT"),
if (image != null) {
showDialog(
context: context,
builder: (context) => Scaffold(
appBar: AppBar(
title: Text("CAPURED SCREENSHOT"),
),
body: Center(
child: Column(
children: [
if (image != null) Image.memory(image),
],
)),
),
body: Center(
child: Column(
children: [
_imageFile != null ? Image.memory(_imageFile) : Container(),
],
)),
),
);
);
}
}).catchError((onError) {
print(onError);
});
......
......@@ -10,8 +10,8 @@ description: A new Flutter project.
version: 1.0.0+1
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,42 +16,44 @@ 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),
String? fileName,
double? pixelRatio,
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({
double pixelRatio,
Duration delay: const Duration(milliseconds: 20),
Future<Uint8List?> capture({
double? pixelRatio,
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 =
ByteData? byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
Uint8List? pngBytes = byteData?.buffer.asUint8List();
return pngBytes;
} catch (Exception) {
......@@ -61,18 +63,20 @@ class ScreenshotController {
}
Future<ui.Image> captureAsUiImage(
{double pixelRatio: 1,
Duration delay: const Duration(milliseconds: 20)}) {
{double? pixelRatio, Duration delay: const Duration(milliseconds: 20)}) {
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
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);
?.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);
......@@ -85,9 +89,9 @@ class Screenshot<T> extends StatefulWidget {
final Widget child;
final ScreenshotController controller;
const Screenshot({
Key key,
this.child,
this.controller,
Key? key,
required this.child,
required this.controller,
}) : super(key: key);
@override
......@@ -97,14 +101,15 @@ class Screenshot<T> extends StatefulWidget {
}
class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
late
ScreenshotController _controller;
@override
void initState() {
super.initState();
if (widget.controller == null) {
_controller = ScreenshotController();
} else
// if (widget.controller == null) {
// _controller = ScreenshotController();
// } else
_controller = widget.controller;
}
......
......@@ -7,5 +7,5 @@ import 'file_manager_stub.dart'
abstract class PlatformFileManager {
factory PlatformFileManager() => getFileManager();
Future<String> saveFile(Uint8List fileContent, String path, {String name});
Future<String> saveFile(Uint8List fileContent, String path, {String? name});
}
......@@ -8,7 +8,7 @@ PlatformFileManager getFileManager() => PlatformFilePickerWindows();
class PlatformFilePickerWindows implements PlatformFileManager {
@override
Future<String> saveFile(Uint8List fileContent, String path,
{String name}) async {
{String? name}) async {
name = name ?? "${DateTime.now().microsecondsSinceEpoch}.png";
File file = await File("$path/$name").create(recursive: true);
file.writeAsBytesSync(fileContent);
......
......@@ -8,7 +8,7 @@ PlatformFileManager getFileManager() => PlatformFileManagerWeb();
class PlatformFileManagerWeb implements PlatformFileManager {
@override
Future<String> saveFile(Uint8List fileContent, String path,
{String name}) async {
{String? name}) async {
throw UnsupportedError("File cannot be saved in current platform");
// name = name ?? "${DateTime.now().microsecondsSinceEpoch}.png";
// File file = await File("$path/$name").create(recursive: true);
......
......@@ -143,4 +143,4 @@ packages:
source: hosted
version: "2.1.0-nullsafety.5"
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
dart: ">=2.12.0-259.8.beta <3.0.0"
......@@ -4,8 +4,8 @@ version: 0.3.0
homepage: https://github.com/SachinGanesh/screenshot
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'
# analyzer:
# enable-experiment:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论