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

support web and windows platfom

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