Flutter, how to take full screenshot - flutter

i am trying to find a way to take a screenshot of entire screen. Flutter library is only allowing me to make a screenshot of a widget, but i want a screenshot of entire android.
I did find this link:
How to programmatically take a screenshot on Android?
and this is resolwing my problem, but i tried to use that java code as it is in documentation:
https://flutter.dev/docs/development/platform-integration/platform-channels?tab=android-channel-java-tab
sadly i am not able to run that code, maybie im making some kind of mistakes? i find a error, while i whas following docs instruction, after that program whas working (not exacly, java whas not able to find my battery, but code whas compiling), and then i copy first answer from that stack link and i tried to pase it in "MainActivity" folder, sadly program whas not working, my "old" java code whas comipilng, from the docs, not the new one.
If you know the solution how to make a photo of entire android screen using some kind of buttor in flutter, or how to implement code from that link into my MainActivity.java folder, please help

Try this package native_screenshot
https://pub.dev/packages/native_screenshot
You can simply run this function to take the screenshot of your phone:
Future<void> _capturePng() async {
String path = await NativeScreenshot.takeScreenshot();
print(path);
}
You can learn more about this package here
It work fine with Android and IOS
Hope it will be useful

You can use screenshot plugin:https://pub.dev/packages/screenshot to take a widget screenshot.
A screenshot is a simple plugin to capture widgets as Images. This plugin wraps your widgets inside RenderRepaintBoundary
Use this package as a library
Add this to your package's pubspec.yaml file:
dependencies:
screenshot: ^0.2.0
Now in your Dart code, Import it:
import 'package:screenshot/screenshot.dart';
This handy plugin can be used to capture any Widget including full-screen screenshots & individual widgets like Text().
Create Instance of Screenshot Controller
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();
}
...
}
Wrap the widget that you want to capture inside Screenshot Widget. Assign the controller to screenshotController that you have created earlier
Screenshot(
controller: screenshotController,
child: Text("This text will be captured as image"),
),
Take the screenshot by calling the capture method. This will return a File
screenshotController.capture().then((File image) {
//Capture Done
setState(() {
_imageFile = image;
});
}).catchError((onError) {
print(onError);
});
Duplicate: How to take a screenshot of the current widget - Flutter

Related

Flutter precacheImage not working for AssetImage in Android/iOS when support multiple dimensions

I have been following posts of people trying to use precacheImage to avoid flickering when loading AssetImage first time. Some key code below:
late Image logo;
#override
void initState() {
super.initState();
logo = Image.asset("path_to_logo");
}
#override
void didChangeDependencies() {
precacheImage(logo.image, context);
super.didChangeDependencies();
}
Then later logo is used as a child widget:
Widget build(BuildContext context) {
...
logo
...
}
I also tried to use precacheImage before current page loads in main.dart but that didn't do any difference. The image is empty for a brief while when first time loaded and then all content is shifted to correct position once the asset image loads.
Using sdk: '>=2.19.0-168.0.dev <3.0.0'
Any ideas what could be wrong?
EDIT:
I found out that since I have support for multiple device pixel ratios, the precaching caches only 1x dimensions because I am using the generic path, but the device can be e.g. using 3x and ImageCache also seems to have some internal scale property when it caches images.
This ticket might be about the issue I am facing: https://github.com/flutter/flutter/issues/3905

How can I know when a google font have been loaded completely in flutter for web?

I'm currently writing a webpage with flutter. In that, I'm using GoogleFonts package and I would like to know when the font has been loaded completely if there's a way to do that.
The reason why I want to know that is below.
I'm wrapping the google fonts text with the AutoSizeText package(https://pub.dev/packages/auto_size_text) and it does not work properly when the first time you open the page because the AutoSizeText optimizes the font size for the default font, not the google font, since the google font has not been loaded yet. Consequently, the google font sometimes overflows the bounds. My workaround for it is to force reload the widget after a few seconds by doing something like this but it does not feel right.
#override
void initState() {
super.initState();
waitForSomeMoment();
}
void waitForSomeMoment() async {
await Future.delayed(
Duration(milliseconds: 1000),
() {
setState(() {});
},
);
}
I don't think there's a way to know that. But instead, you can bundle font files in your assets. Check out the end of the google_fonts page to learn how.

Flutter widget vs Flutter driver

I'm writing tests for a mobile App written in Flutter.
I followed this Flutter Cookbook on testing Flutter apps, to learn how to write widget and integration tests.
This tutorial works perfectly, but I'm still stuck with my own Integration tests.
To simplify, let's assume I have an app, containing only a TextField:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyAppp',
home: Scaffold(
body: Center(child: TextField()),
),
);
}
}
I want to write a test for this app. For instance, I want to test the following scenario:
Open the app
Check that the TextField is empty
Select the TextField
Enter "hello, world!"
Check that TextField contains "hello, world!"
I wrote the following Widget test, which works fine:
void main() {
testWidgets('TextField behavior', (WidgetTester tester) async {
// Create app
await tester.pumpWidget(MyApp());
// Find TextField, check there is 1
final textFieldFinder = find.byType(TextField);
expect(textFieldFinder, findsOneWidget);
// Retrieve TextField Widget from Finder
TextField textField = tester.widget(textFieldFinder);
// Check TextField is empty
expect(textField.controller.text, equals(""));
// Enter text
await tester.enterText(textFieldFinder, "hello, world!");
// Check TextField contains text
expect(textField.controller.text, equals("hello, world!"));
});
}
This test passes, but I wanted to write an Integration test, doing more or less the same, to be able to test it on a real device.
Indeed if this Widget test passes, it will probably pass on all device. But in my app I have more complex widgets and interactions between them, I want to be able to launch tests on both Android and iOS.
I tried to write integration tests using Flutter driver, but I did not find what I wanted in the documentation and examples.
How can I check Widget properties, to verify that my App behaves as expected?
I wrote the following sample:
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
void main() {
group('TextField', () {
final textFieldFinder = find.byType('TextField');
FlutterDriver driver;
setUpAll(() async {
driver = await FlutterDriver.connect();
});
tearDownAll(() async {
if (driver != null) {
driver.close();
}
});
test('TextField behavior', () async {
// ??? how to check that the TextField is empty
await driver.tap(textFieldFinder);
await driver.enterText("hello, world!");
// ??? how to check that the TextField contains hello, world!
});
});
}
The Flutter Cookbook tutorial explains how to retrieve an object by its Text, this would help to check if the Text is present, but for instance is it possible to check that a Container color is red?
Where is the limit between Widget and Integration tests?
Writing Widget tests is pretty straighforward, but I didn't find many examples or documentation about how to write more complexe integration tests using Flutter driver.
If anyone is interested:
To be able to test what I want (not only the presence of widgets, but also theirs states, their properties,...) test driver was not enough for me.
What I did in my project, is to use the flutter_test to write widget tests and check the properties I want.
To test it on a real device (Android or iOS), I used the integration_test package (previously e2e package) available here, which adapts flutter_test results to a format compatible with flutter drive.
To use integration_test, add this line in the main of your widget tests:
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
Then create a file (e.g. integration_test.dart) in your test_driver folder with this content:
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();
Then you can launch these driver test by running:
flutter drive \
--driver=test_driver/integration_test.dart \
--target=test/widget_test/widget_test.dart
With your widgets tests wrote in the file test/widget_test/widget_test.dart.
It really helped me, since some widgets did not have the same behavior on Android and iOS.
Today I use both flutter_test (launched on real device with integration_test) and real flutter_driver tests:
I write widget tests to check a single widget, or a single page,
I use flutter driver to write more sophisticated scenarios to test the whole application.
I think there's a difference between flutter widget and flutter driver. In that, flutter widgets is for widget tests and flutter driver is for integration tests. I'm not exactly sure what that means on a practical standpoint but I think integration test are easier to run tests on application interactions that would be made by a user since integration tests run on an actual device as compared to widget tests. Will update if I find more information on this.

Render to video - Flutter

Description :
I'm working on slide show app. Where I can import a video , add on it sound + text (subtitles and stuff) Then render it & save it locally or share it on social media.
I've managed doing the first part with this package.
But I have 3 problems :
First: However I'm just putting text widgets overlaid onto the video player.
Second : How can I input a certain sound on the video?
Third : After all that , How can I render the output video which I can save it locally or share it?
I think you could theoretically make it seem like you are combining video and audio based on how you render them, however to do the third it looks like you would need to get into video editing. Doing it from scratch could be quite a task, and I know in java there is ffmeg but not sure about dart. Here is another stackoverflow discussion that has more on the matter
Video Editor in Flutter using dart
There is now a high-level render package that heavily optimizes the approach of repaint boundary capturing.
Wrap you widget with Renderwidget:
import 'package:render/render.dart';
final controller = RenderController();
#override
Widget build(BuildContext context) {
return Render(
controller: controller,
child: Container(),
);
}
And then capture the motion with the controller:
final result = await renderController.captureMotion(
duration,
format: Format.gif,
);
final file = result.output;

Flutter Web Save Image

I was working on a flutter web app where I need to save an image created using canvas.I tried FileSaver.js library but it wasn't successful for me.
Index.html main.dart
I created simple package image_downloader_web that can easily download image from web.
final WebImageDownloader _webImageDownloader = WebImageDownloader();
const _url = "https://picsum.photos/200";
Future<void> _downloadImage() async {
await _webImageDownloader.downloadImageFromWeb(_url);
}