Skip to content

Commit

Permalink
support web and windows platfom
Browse files Browse the repository at this point in the history
  • Loading branch information
ritheshSalyan committed Feb 10, 2021
1 parent 89b8098 commit d03e009
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 75 deletions.
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 8 additions & 18 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// 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());
Expand Down Expand Up @@ -57,6 +58,8 @@ class _MyHomePageState extends State<MyHomePage> {

@override
void initState() {

// if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
super.initState();
}

Expand Down Expand Up @@ -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"),

),
),
),
Expand Down
15 changes: 3 additions & 12 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,34 @@ 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:

# The following line ensures that the Material Icons font is
# 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
Expand Down
84 changes: 44 additions & 40 deletions lib/screenshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -113,3 +116,4 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
);
}
}

8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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:
Expand Down

0 comments on commit d03e009

Please sign in to comment.