From 237ce2989519e1e9ccd1867812b185cfe1fb4893 Mon Sep 17 00:00:00 2001 From: ritheshSalyan <rithesh199.rr@gmail.com> Date: Wed, 10 Feb 2021 14:35:40 +0530 Subject: [PATCH] add new method captureAndSave --- example/lib/main.dart | 34 ++++------ lib/screenshot.dart | 23 +++++-- .../file_manager/file_manager.dart | 11 ++++ .../file_manager/file_manager_io.dart | 17 +++++ .../file_manager/file_manager_mobile.dart | 18 +++++ .../file_manager/file_manager_stub.dart | 4 ++ .../file_manager/non_io.dart | 18 +++++ pubspec.lock | 8 --- pubspec.yaml | 65 +++++++++---------- 9 files changed, 130 insertions(+), 68 deletions(-) create mode 100644 lib/src/platform_specific/file_manager/file_manager.dart create mode 100644 lib/src/platform_specific/file_manager/file_manager_io.dart create mode 100644 lib/src/platform_specific/file_manager/file_manager_mobile.dart create mode 100644 lib/src/platform_specific/file_manager/file_manager_stub.dart create mode 100644 lib/src/platform_specific/file_manager/non_io.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index c280602..122877c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,4 +1,3 @@ - import 'dart:typed_data'; import 'package:flutter/material.dart'; @@ -58,8 +57,7 @@ class _MyHomePageState extends State<MyHomePage> { @override void initState() { - - // if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); + // if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); super.initState(); } @@ -92,8 +90,7 @@ class _MyHomePageState extends State<MyHomePage> { child: new Center( child: Screenshot( controller: screenshotController, - child:Text("HEllo"), - + child: Text("HEllo"), ), ), ), @@ -104,28 +101,21 @@ class _MyHomePageState extends State<MyHomePage> { screenshotController .capture(delay: Duration(milliseconds: 10)) .then((Uint8List image) async { - //print("Capture Done"); - // setState(() { - _imageFile = image; - showDialog(context: context, + _imageFile = image; + showDialog( + context: context, builder: (context) => Scaffold( appBar: AppBar( title: Text("CAPURED SCREENSHOT"), ), body: Center( - child:Column( - children: [ - - _imageFile != null ? Image.memory(_imageFile) : Container(), - ], - ) - ), - ) , - ); - // }); - // final result = - // await ImageGallerySaver.save(image.readAsBytesSync()); - print("File Saved to Gallery"); + child: Column( + children: [ + _imageFile != null ? Image.memory(_imageFile) : Container(), + ], + )), + ), + ); }).catchError((onError) { print(onError); }); diff --git a/lib/screenshot.dart b/lib/screenshot.dart index eec2f00..89bbc68 100644 --- a/lib/screenshot.dart +++ b/lib/screenshot.dart @@ -3,7 +3,7 @@ library screenshot; // import 'dart:io'; import 'dart:async'; import 'dart:typed_data'; - +import 'src/platform_specific/file_manager/file_manager.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; @@ -21,8 +21,24 @@ class ScreenshotController { _containerKey = GlobalKey(); } +/// Captures image and saves to given path + Future<String> captureAndSave( + String directory, { + String fileName, + double pixelRatio, + Duration delay: const Duration(milliseconds: 20), + }) async { + Uint8List content = await capture( + pixelRatio: pixelRatio, + delay: delay, + ); + + PlatformFileManager fileManager = PlatformFileManager(); + + return fileManager.saveFile(content, directory, name: fileName); + } + Future<Uint8List> capture({ - String path = "", double pixelRatio, Duration delay: const Duration(milliseconds: 20), }) { @@ -73,7 +89,7 @@ class Screenshot<T> extends StatefulWidget { this.child, this.controller, }) : super(key: key); - + @override State<Screenshot> createState() { return new ScreenshotState(); @@ -116,4 +132,3 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin { ); } } - diff --git a/lib/src/platform_specific/file_manager/file_manager.dart b/lib/src/platform_specific/file_manager/file_manager.dart new file mode 100644 index 0000000..bc815de --- /dev/null +++ b/lib/src/platform_specific/file_manager/file_manager.dart @@ -0,0 +1,11 @@ +import 'dart:typed_data'; + +// import 'file_manager_mobile.dart'; +import 'file_manager_stub.dart' + if (dart.library.io) "file_manager_io.dart" + if (dart.library.html) "non_io.dart"; + +abstract class PlatformFileManager { + factory PlatformFileManager() => getFileManager(); + Future<String> saveFile(Uint8List fileContent, String path, {String name}); +} diff --git a/lib/src/platform_specific/file_manager/file_manager_io.dart b/lib/src/platform_specific/file_manager/file_manager_io.dart new file mode 100644 index 0000000..1d3cf1a --- /dev/null +++ b/lib/src/platform_specific/file_manager/file_manager_io.dart @@ -0,0 +1,17 @@ +import 'dart:io'; +import 'dart:typed_data'; + +import 'file_manager.dart'; + +PlatformFileManager getFileManager() => PlatformFilePickerWindows(); + +class PlatformFilePickerWindows implements PlatformFileManager { + @override + Future<String> saveFile(Uint8List fileContent, String path, + {String name}) async { + name = name ?? "${DateTime.now().microsecondsSinceEpoch}.png"; + File file = await File("$path/$name").create(recursive: true); + file.writeAsBytesSync(fileContent); + return file.path; + } +} diff --git a/lib/src/platform_specific/file_manager/file_manager_mobile.dart b/lib/src/platform_specific/file_manager/file_manager_mobile.dart new file mode 100644 index 0000000..acadf6b --- /dev/null +++ b/lib/src/platform_specific/file_manager/file_manager_mobile.dart @@ -0,0 +1,18 @@ +// import 'dart:io'; +// import 'dart:typed_data'; + +// import 'file_manager.dart'; + +// PlatformFileManager getFilePicker() => PlatformFilePickerMobile(); + +// class PlatformFilePickerMobile with PlatformFileManager { +// @override +// Future<String> saveFile(Uint8List fileContent, String path, {String name}) async{ +// name = name??"${DateTime.now().toIso8601String()}.png"; +// File file = File("$path/$name"); +// file.writeAsBytesSync(fileContent); +// return file.path; +// } + + +// } diff --git a/lib/src/platform_specific/file_manager/file_manager_stub.dart b/lib/src/platform_specific/file_manager/file_manager_stub.dart new file mode 100644 index 0000000..3b2fb33 --- /dev/null +++ b/lib/src/platform_specific/file_manager/file_manager_stub.dart @@ -0,0 +1,4 @@ +import 'file_manager.dart'; + +PlatformFileManager getFileManager() => + throw UnimplementedError("File Picker is Not Implementd in current platform"); diff --git a/lib/src/platform_specific/file_manager/non_io.dart b/lib/src/platform_specific/file_manager/non_io.dart new file mode 100644 index 0000000..f2d7a64 --- /dev/null +++ b/lib/src/platform_specific/file_manager/non_io.dart @@ -0,0 +1,18 @@ +// import 'dart:html'; +import 'dart:typed_data'; + +import 'file_manager.dart'; + +PlatformFileManager getFileManager() => PlatformFileManagerWeb(); + +class PlatformFileManagerWeb implements PlatformFileManager { + @override + Future<String> saveFile(Uint8List fileContent, String path, + {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); + // file.writeAsBytesSync(fileContent); + // return file.path; + } +} diff --git a/pubspec.lock b/pubspec.lock index 14f1ac3..8426fcf 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -81,13 +81,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0-nullsafety.3" - path_provider: - dependency: "direct main" - description: - name: path_provider - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.0" sky_engine: dependency: transitive description: flutter @@ -151,4 +144,3 @@ packages: version: "2.1.0-nullsafety.5" sdks: dart: ">=2.12.0-0.0 <3.0.0" - flutter: ">=0.1.4" diff --git a/pubspec.yaml b/pubspec.yaml index aafa77d..ce00243 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,12 +6,11 @@ homepage: https://github.com/SachinGanesh/screenshot environment: sdk: '>=2.8.0 <3.0.0' # sdk: '>=2.12.0-259.8.beta <3.0.0' - + # analyzer: # enable-experiment: # - non-nullable dependencies: - path_provider: ^1.1.0 flutter: sdk: flutter @@ -21,37 +20,35 @@ dev_dependencies: # For information on the generic Dart part of this file, see the # following page: https://www.dartlang.org/tools/pub/pubspec - # The following section is specific to Flutter. -flutter: - - # To add assets to your package, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - # - # For details regarding assets in packages, see - # https://flutter.io/assets-and-images/#from-packages - # - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.io/assets-and-images/#resolution-aware. +flutter: null - # To add custom fonts to your package, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts in packages, see - # https://flutter.io/custom-fonts/#from-packages +# To add assets to your package, add an assets section, like this: +# assets: +# - images/a_dot_burr.jpeg +# - images/a_dot_ham.jpeg +# +# For details regarding assets in packages, see +# https://flutter.io/assets-and-images/#from-packages +# +# An image asset can refer to one or more resolution-specific "variants", see +# https://flutter.io/assets-and-images/#resolution-aware. +# To add custom fonts to your package, add a fonts section here, +# in this "flutter" section. Each entry in this list should have a +# "family" key with the font family name, and a "fonts" key with a +# list giving the asset and other descriptors for the font. For +# example: +# fonts: +# - family: Schyler +# fonts: +# - asset: fonts/Schyler-Regular.ttf +# - asset: fonts/Schyler-Italic.ttf +# style: italic +# - family: Trajan Pro +# fonts: +# - asset: fonts/TrajanPro.ttf +# - asset: fonts/TrajanPro_Bold.ttf +# weight: 700 +# +# For details regarding fonts in packages, see +# https://flutter.io/custom-fonts/#from-packages -- 2.17.1