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://yourweb.com/referral?code=TEST123 https://play.google.com/store/apps/details?id=com.erer&referrer=referral_code%3DTEST123 Manifest :  < intent-filter android : autoVerify = "true" >     < action android : name = "android.intent.action.VIEW" />     < category android : name = "android.intent.category.DEFAULT" />     < category android : name = "android.intent.category.BROWSABLE" />     < data         android : host = "yourweb.co"         android : pathPrefix = "/referral"         android : scheme = "https" /> </ intent-filter > 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_p...

Vibe Coding Tools List

  Vibe Coding Tools List Dedicated Vibe Coding Platforms: Google AI Studio / Firebase Studio:  Rapid full-stack application generation and deployment from conversational prompts. Replit Agent:  Cloud-based IDE with "Ghostwriter" AI for instant code generation, debugging, and deployment. Bolt:  Browser-based AI development agent for building full-stack web and mobile apps with natural language. Lovable:  Rapid UI prototyping tool that converts text commands into styled layouts with one-click export/deployment. v0 by Vercel:  Prompt-powered UI builder for generating production-ready React components with Tailwind CSS. Wegic:  Converts visual ideas or Figma designs into working code with prompt-based structure creation. AI-Powered IDEs and Code Editors: Cursor :  An "AI-first" code editor (based on VS Code) offering deep project awareness and multi-file conversational edits. Windsurf (formerly Codeium) :  Agentic AI-native IDE featuring the "Cas...

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.