提交 237ce298 authored 作者: ritheshSalyan's avatar ritheshSalyan

add new method captureAndSave

上级 980871be
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
...@@ -58,8 +57,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -58,8 +57,7 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
void initState() { void initState() {
// if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
// if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
super.initState(); super.initState();
} }
...@@ -92,8 +90,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -92,8 +90,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: new Center( child: new Center(
child: Screenshot( child: Screenshot(
controller: screenshotController, controller: screenshotController,
child:Text("HEllo"), child: Text("HEllo"),
), ),
), ),
), ),
...@@ -104,28 +101,21 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -104,28 +101,21 @@ class _MyHomePageState extends State<MyHomePage> {
screenshotController screenshotController
.capture(delay: Duration(milliseconds: 10)) .capture(delay: Duration(milliseconds: 10))
.then((Uint8List image) async { .then((Uint8List image) async {
//print("Capture Done"); _imageFile = image;
// setState(() { showDialog(
_imageFile = image; context: context,
showDialog(context: context,
builder: (context) => Scaffold( builder: (context) => Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text("CAPURED SCREENSHOT"), title: Text("CAPURED SCREENSHOT"),
), ),
body: Center( body: Center(
child:Column( child: Column(
children: [ children: [
_imageFile != null ? Image.memory(_imageFile) : Container(),
_imageFile != null ? Image.memory(_imageFile) : Container(), ],
], )),
) ),
), );
) ,
);
// });
// final result =
// await ImageGallerySaver.save(image.readAsBytesSync());
print("File Saved to Gallery");
}).catchError((onError) { }).catchError((onError) {
print(onError); print(onError);
}); });
......
...@@ -3,7 +3,7 @@ library screenshot; ...@@ -3,7 +3,7 @@ library screenshot;
// import 'dart:io'; // import 'dart:io';
import 'dart:async'; import 'dart:async';
import 'dart:typed_data'; import 'dart:typed_data';
import 'src/platform_specific/file_manager/file_manager.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
...@@ -21,8 +21,24 @@ class ScreenshotController { ...@@ -21,8 +21,24 @@ class ScreenshotController {
_containerKey = GlobalKey(); _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({ Future<Uint8List> capture({
String path = "",
double pixelRatio, double pixelRatio,
Duration delay: const Duration(milliseconds: 20), Duration delay: const Duration(milliseconds: 20),
}) { }) {
...@@ -73,7 +89,7 @@ class Screenshot<T> extends StatefulWidget { ...@@ -73,7 +89,7 @@ class Screenshot<T> extends StatefulWidget {
this.child, this.child,
this.controller, this.controller,
}) : super(key: key); }) : super(key: key);
@override @override
State<Screenshot> createState() { State<Screenshot> createState() {
return new ScreenshotState(); return new ScreenshotState();
...@@ -116,4 +132,3 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin { ...@@ -116,4 +132,3 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
); );
} }
} }
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});
}
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;
}
}
// 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;
// }
// }
import 'file_manager.dart';
PlatformFileManager getFileManager() =>
throw UnimplementedError("File Picker is Not Implementd in current platform");
// 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;
}
}
...@@ -81,13 +81,6 @@ packages: ...@@ -81,13 +81,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.3" 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: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
...@@ -151,4 +144,3 @@ packages: ...@@ -151,4 +144,3 @@ packages:
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-0.0 <3.0.0"
flutter: ">=0.1.4"
...@@ -6,12 +6,11 @@ homepage: https://github.com/SachinGanesh/screenshot ...@@ -6,12 +6,11 @@ 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:
# - non-nullable # - non-nullable
dependencies: dependencies:
path_provider: ^1.1.0
flutter: flutter:
sdk: flutter sdk: flutter
...@@ -21,37 +20,35 @@ dev_dependencies: ...@@ -21,37 +20,35 @@ dev_dependencies:
# For information on the generic Dart part of this file, see the # For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec # following page: https://www.dartlang.org/tools/pub/pubspec
# The following section is specific to Flutter. # The following section is specific to Flutter.
flutter: flutter: null
# 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, # To add assets to your package, add an assets section, like this:
# in this "flutter" section. Each entry in this list should have a # assets:
# "family" key with the font family name, and a "fonts" key with a # - images/a_dot_burr.jpeg
# list giving the asset and other descriptors for the font. For # - images/a_dot_ham.jpeg
# example: #
# fonts: # For details regarding assets in packages, see
# - family: Schyler # https://flutter.io/assets-and-images/#from-packages
# fonts: #
# - asset: fonts/Schyler-Regular.ttf # An image asset can refer to one or more resolution-specific "variants", see
# - asset: fonts/Schyler-Italic.ttf # https://flutter.io/assets-and-images/#resolution-aware.
# style: italic # To add custom fonts to your package, add a fonts section here,
# - family: Trajan Pro # in this "flutter" section. Each entry in this list should have a
# fonts: # "family" key with the font family name, and a "fonts" key with a
# - asset: fonts/TrajanPro.ttf # list giving the asset and other descriptors for the font. For
# - asset: fonts/TrajanPro_Bold.ttf # example:
# weight: 700 # fonts:
# # - family: Schyler
# For details regarding fonts in packages, see # fonts:
# https://flutter.io/custom-fonts/#from-packages # - 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
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论