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

support web and windows platfom

上级 89b80987
......@@ -35,7 +35,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.screenshot.example"
minSdkVersion 16
minSdkVersion 17
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
......
// import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:screenshot/screenshot.dart';
// import 'package:webview_flutter/webview_flutter.dart';
// import 'package:image_gallery_saver/image_gallery_saver.dart';
void main() => runApp(MyApp());
......@@ -57,6 +58,8 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
// if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
super.initState();
}
......@@ -87,23 +90,10 @@ class _MyHomePageState extends State<MyHomePage> {
),
body: Container(
child: new Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Screenshot(
controller: screenshotController,
child: Column(
children: <Widget>[
Text(
'You have pushed the button this many times:' +
_counter.toString(),
),
FlutterLogo(),
],
),
),
_imageFile != null ? Image.memory(_imageFile) : Container(),
],
child: Screenshot(
controller: screenshotController,
child:Text("HEllo"),
),
),
),
......
......@@ -14,24 +14,19 @@ environment:
# sdk: '>=2.12.0-259.8.beta <3.0.0'
dependencies:
image_gallery_saver: ^1.1.0
screenshot:
path: ../
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
screenshot:
path: ../
cupertino_icons: ^0.1.2
image_gallery_saver: ^1.1.0
dev_dependencies:
flutter_test:
sdk: flutter
# 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:
......@@ -39,18 +34,14 @@ flutter:
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.io/assets-and-images/#from-packages
# To add custom fonts to your application, 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
......
......@@ -10,6 +10,11 @@ import 'package:flutter/widgets.dart';
// import 'package:path_provider/path_provider.dart';
import 'dart:ui' as ui;
///
///
///Cannot capture Platformview due to issue https://github.com/flutter/flutter/issues/25306
///
///
class ScreenshotController {
GlobalKey _containerKey;
ScreenshotController() {
......@@ -19,58 +24,56 @@ class ScreenshotController {
Future<Uint8List> capture({
String path = "",
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
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);
ui.Image image = await captureAsUiImage(
delay: Duration.zero,
pixelRatio: pixelRatio,
);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
// if (path == "") {
// final directory = (await getApplicationDocumentsDirectory()).path;
// String fileName = DateTime.now().toIso8601String();
// path = '$directory/$fileName.png';
// }
// File imgFile = new File(path);
// await imgFile.writeAsBytes(pngBytes).then((onValue) {});
return pngBytes;
} catch (Exception) {
throw (Exception);
}
});
}
Future<ui.Image> captureAsUiImage({
double pixelRatio: 1,
Duration delay: const Duration(milliseconds: 20)
}) {
Future<ui.Image> captureAsUiImage(
{double pixelRatio: 1,
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();
return await boundary.toImage(pixelRatio: pixelRatio);
RenderRepaintBoundary boundary = this
._containerKey
.currentContext
.findRenderObject() as RenderRepaintBoundary;
pixelRatio = pixelRatio ??
MediaQuery.of(_containerKey.currentContext).devicePixelRatio;
ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
return image;
} catch (Exception) {
throw (Exception);
}
});
}
}
class Screenshot<T> extends StatefulWidget {
final Widget child;
final ScreenshotController controller;
final GlobalKey containerKey;
const Screenshot({Key key, this.child, this.controller, this.containerKey})
: super(key: key);
const Screenshot({
Key key,
this.child,
this.controller,
}) : super(key: key);
@override
State<Screenshot> createState() {
return new ScreenshotState();
......@@ -89,21 +92,21 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
_controller = widget.controller;
}
@override
void didUpdateWidget(Screenshot oldWidget) {
super.didUpdateWidget(oldWidget);
// @override
// void didUpdateWidget(Screenshot oldWidget) {
// // super.didUpdateWidget(oldWidget);
if (widget.controller != oldWidget.controller) {
widget.controller._containerKey = oldWidget.controller._containerKey;
if (oldWidget.controller != null && widget.controller == null)
_controller._containerKey = oldWidget.controller._containerKey;
if (widget.controller != null) {
if (oldWidget.controller == null) {
_controller = null;
}
}
}
}
// // if (widget.controller != oldWidget.controller) {
// // widget.controller._containerKey = oldWidget.controller._containerKey;
// // if (oldWidget.controller != null && widget.controller == null)
// // _controller._containerKey = oldWidget.controller._containerKey;
// // if (widget.controller != null) {
// // if (oldWidget.controller == null) {
// // _controller = null;
// // }
// // }
// // }
// }
@override
Widget build(BuildContext context) {
......@@ -113,3 +116,4 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
);
}
}
name: screenshot
description: Flutter Screenshot Package (Runtime). Capture any Widget as an image.
version: 0.2.0
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'
analyzer:
enable-experiment:
- non-nullable
# analyzer:
# enable-experiment:
# - non-nullable
dependencies:
path_provider: ^1.1.0
flutter:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论