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

add new method captureAndSave

上级 980871be
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);
});
......
......@@ -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 {
);
}
}
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:
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"
......@@ -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
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论