Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example for correct sized screenshots #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions device_preview/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:device_preview_example/screenShotProcessing.dart';

void main() {
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;

runApp(DevicePreview(
onScreenshot: onScreenshot,
builder: (context) => ExampleApp(),
));
}
Expand Down
70 changes: 70 additions & 0 deletions device_preview/example/lib/screenShotProcessing.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'dart:convert';
import 'package:device_preview/device_preview.dart';
import 'package:http/http.dart' as http;
import 'package:image/image.dart';

const String host = 'https://file.io/?expires=1';

Future<String> onScreenshot(DeviceScreenshot screenshot) async {
var shot = decodeImage(screenshot.bytes);

var size = screenshot.device.portrait.size;
var acpect = screenshot.device.portrait.size.aspectRatio;
var ratio = screenshot.device.portrait.devicePixelRatio;

if (shot.width > shot.height) {
size = screenshot.device.landscape.size;
acpect = screenshot.device.landscape.size.aspectRatio;
ratio = screenshot.device.landscape.devicePixelRatio;
}

final width = (size.width * ratio).round();
final heigth = (size.height * ratio).round();

final image = Image(width, heigth);

shot = copyResize(
dropShadow(shot, 0, 0, 0),
height: acpect >= 0.5 ? heigth : null,
width: acpect <= 0.5 ? width : null,
);

image.fill(getColor(255, 255, 255));

final x = ((image.width - shot.width) / 2).round();
final y = ((image.height - shot.height) / 2).round();

copyInto(
image,
shot,
dstX: x,
dstY: y,
);

var request = http.MultipartRequest('POST', Uri.parse(host));
request.files.add(
http.MultipartFile.fromBytes(
'file',
encodeJpg(image),
filename: 'screenshot.jpg',
),
);

final response = await request.send();

if (response.statusCode != 200) {
throw Error();
}

final bodyString = await response.stream.transform(utf8.decoder).join();
final body = jsonDecode(bodyString);

if (!body['success']) {
throw Error();
}

final link = body['link'];
print('[DevicePreview] Screenshot available here : $link');

return 'Your screenshot is available here : $link';
}
1 change: 1 addition & 0 deletions device_preview/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
meta: ^1.1.8
shared_preferences: ^0.5.6+3
shared_preferences_macos: ^0.0.1+6
image: ^2.1.12

# This is a temporary hack to enable Windows to work. It is not recommended
# since this package may change at any time in ways that break.
Expand Down