-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52093d9
commit 3e7d3b2
Showing
5 changed files
with
154 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,131 @@ | ||
# screenshot | ||
|
||
A new Flutter package project. | ||
A simple plugin to capture widgets as Images. | ||
|
||
This plugin wraps your widgets inside [RenderRepaintBoundary](https://docs.flutter.io/flutter/rendering/RenderRepaintBoundary-class.html) | ||
|
||
[Source](https://stackoverflow.com/a/51118088) | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Dart | ||
[package](https://flutter.io/developing-packages/), | ||
a library module containing code that can be shared easily across | ||
multiple Flutter or Dart projects. | ||
This handy plugin can be used to capture any Widget including full screen screenshots & individual widgets like Text(). | ||
|
||
1) Create Instance of Screenshot Controller | ||
|
||
```dart | ||
class _MyHomePageState extends State<MyHomePage> { | ||
int _counter = 0; | ||
File _imageFile; | ||
//Create an instance of ScreenshotController | ||
ScreenshotController screenshotController = ScreenshotController(); | ||
@override | ||
void initState() { | ||
// TODO: implement initState | ||
super.initState(); | ||
} | ||
... | ||
} | ||
``` | ||
2) Wrap the widget that you want to capture inside **Screenshot** Widget. Assign the controller to screenshotController that you have created earlier | ||
|
||
```dart | ||
Screenshot( | ||
controller: screenshotController, | ||
child: Text("This text will be captured as image"), | ||
), | ||
``` | ||
|
||
3) Take the screenshot by calling capture method. This will return a File | ||
|
||
```dart | ||
screenshotController.capture().then((File image) { | ||
//Capture Done | ||
setState(() { | ||
_imageFile = image; | ||
}); | ||
}).catchError((onError) { | ||
print(onError); | ||
}); | ||
``` | ||
|
||
Example: | ||
|
||
```dart | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text(widget.title), | ||
), | ||
body: Container( | ||
child: new Center( | ||
child: new Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
Screenshot( //Screenshot Widget | ||
controller: screenshotController, //asign Controller | ||
//wrap the widgets that you want to capture as image | ||
child: <Widget>[ | ||
Text( | ||
'You have pushed the button this many times:' + | ||
_counter.toString(), | ||
), | ||
FlutterLogo(), | ||
], | ||
), | ||
_imageFile != null ? Image.file(_imageFile) : Container(), | ||
], | ||
), | ||
), | ||
), | ||
floatingActionButton: FloatingActionButton( | ||
onPressed: () { | ||
_incrementCounter(); | ||
_imageFile = null; | ||
screenshotController.capture().then((File image) { | ||
setState(() { | ||
_imageFile = image; | ||
}); | ||
}).catchError((onError){ | ||
print(onError); | ||
}); | ||
}, | ||
tooltip: 'Increment', | ||
child: Icon(Icons.add), | ||
), | ||
); | ||
} | ||
``` | ||
|
||
<img src="assets/screenshot.png" alt="screenshot" width="400"/> | ||
|
||
|
||
By defualt, the captured image will be saved to Application Directory. Custom paths can be set using **path parameter**. Refer [path_provider](https://pub.dartlang.org/packages/path_provider) | ||
```dart | ||
final directory = (await getApplicationDocumentsDirectory ()).path; //from path_provide package | ||
String fileName = DateTime.now().toIso8601String(); | ||
path = '$directory/$fileName.png'; | ||
screenshotController.capture( | ||
path:path //set path where screenshot will be saved | ||
); | ||
``` | ||
|
||
## Note: | ||
Captured image may look pixelated. You can overcome this issue by setting value for **pixelRatio** | ||
|
||
>The pixelRatio describes the scale between the logical pixels and the size of the output image. It is independent of the window.devicePixelRatio for the device, so specifying 1.0 (the default) will give you a 1:1 mapping between logical pixels and the output pixels in the image. | ||
|
||
```dart | ||
screenshotController.capture( | ||
pixelRatio: 1.5 | ||
) | ||
``` | ||
|
||
## Known Bugs | ||
- Image will not be updated if same filename is given multiple times | ||
|
||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.io/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: screenshot | ||
description: A new Flutter Screenshot Package | ||
description: Flutter Screenshot Package (Runtime) | ||
version: 0.0.1 | ||
author: Sachin Ganesh <[email protected]> | ||
homepage: https://github.com/SachinGanesh/screenshot | ||
|