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

support nullsafety

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