Skip to main content

Full Screenshot in Webview flutter

 import 'dart:convert';

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:path_provider/path_provider.dart';

class InAppWebViewExampleScreen extends StatefulWidget {
@override
_InAppWebViewExampleScreenState createState() =>
new _InAppWebViewExampleScreenState();
}

class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
late InAppWebViewController webView;
late Uint8List screenshotBytes;

@override
void initState() {
super.initState();
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("InAppWebView")),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialUrlRequest:
URLRequest(url: WebUri("https://github.com/flutter")),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, WebUri? url) {},
onLoadStop: (InAppWebViewController controller, WebUri? url) async {
try {
var canvasHeight = await controller.evaluateJavascript(
source:
"Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);");
await controller.setOptions(
options: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
),
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
),
));
screenshotBytes = (await controller.takeScreenshot(
screenshotConfiguration: ScreenshotConfiguration(
compressFormat: CompressFormat.PNG,
//androidHeight: int.parse(canvasHeight.toString())
)))!;
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: SingleChildScrollView(
child: Image.memory(screenshotBytes),
),
);
},
);
} catch (e) {
print("Screenshot error: $e");
}
},
))
])));
}
}


class FullPageScreenshot extends StatefulWidget {
@override
_FullPageScreenshotState createState() => _FullPageScreenshotState();
}

class _FullPageScreenshotState extends State<FullPageScreenshot> {
late InAppWebViewController webViewController;

Future<void> _takeFullPageScreenshot() async {
try {
// Take screenshot of visible area only
Uint8List? imageBytes = await webViewController.takeScreenshot(
screenshotConfiguration: ScreenshotConfiguration(
compressFormat: CompressFormat.PNG,
),
);

if (imageBytes != null) {
final directory = await getApplicationDocumentsDirectory();
File imgFile = File('${directory.path}/screenshot.png');
await imgFile.writeAsBytes(imageBytes);
print('Screenshot saved to ${imgFile.path}');
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Screenshot saved!')));
}
} catch (e) {
print("Screenshot error: $e");
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Failed to take screenshot')));
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Full Page Screenshot'),
actions: [
IconButton(
icon: Icon(Icons.camera),
onPressed: _takeFullPageScreenshot,
),
],
),
body: InAppWebView(
initialUrlRequest: URLRequest(url: WebUri('https://upsoftech.com/biodata/public/templates/temp.html')),
onWebViewCreated: (controller) {
webViewController = controller;
},
// Add more configuration if needed
),
);
}
}

Comments

Popular posts from this blog

Referral & Deep linking

  Plugins :  app_links: android_play_install_referrer: Link to share :  https://play.google.com/store/apps/details?id=com.erer&referrer=referral_code%3DTEST123 Flutter Code :  import 'package:app_links/app_links.dart' ; import 'package:android_play_install_referrer/android_play_install_referrer.dart' ; import 'package:flutter/foundation.dart' ; import 'package:shared_preferences/shared_preferences.dart' ; class DeepLinkService {   static const String _referralKey = 'referral_code' ;   static final AppLinks _appLinks = AppLinks ();   static Future < void > initDeepLinks () async {     debugPrint ( 'Initializing deep links...' );     // 1️⃣ Handle real-time deep/link stream     try {       final Uri ? initialUri = await _appLinks . getInitialAppLink ();       if ( initialUri != null ) {         debugPrint ( 'Initial deep link: $initialUri'...

API security best practices

 API with a focus on security best practices : Key Security Practices Included: Input Validation and Sanitization : All inputs are validated and sanitized to prevent SQL injection and other attacks. Prepared Statements : All database queries use prepared statements to avoid SQL injection. Password Hashing : Passwords are hashed using password_hash() and verified using password_verify() . Token-Based Authentication : JSON Web Tokens (JWTs) are used for secure API authentication. Error Hiding : Error details are logged but not exposed to users in production. Strict Content-Type Header : Ensures only JSON payloads are processed. Rate Limiting and Throttling : Optional mechanisms to prevent abuse. Validation for IDs : Integer inputs (like user_id or exam_id ) are explicitly validated.

Native api call flutter in terminated state

Calling Native Android Code from Flutter (Step-by-Step for Beginners) When you need to run platform-specific features (like background services, Bluetooth, or sensors) that Flutter doesn’t handle out of the box, you can use MethodChannels to talk to native Android or iOS code. In this tutorial, we’ll: Create a MethodChannel in Flutter. Set up a foreground service in Android. Trigger that service from Flutter—even when the app is closed. 1. What Is a MethodChannel? Think of a MethodChannel as a telephone line between Flutter (Dart) and the platform’s native code (Java/Kotlin for Android, Swift/Objective-C for iOS). Flutter side: “📞 Hey Android, please start the service!” Android side: “✅ Got it, I’m starting it now.” 2. Create the Channel in Flutter Create a new file: lib/native_service.dart import 'package:flutter/services.dart' ; class NativeService {   // ✅ A generic channel name you can reuse in any project   static const _channel = MethodChanne...