Take Screenshot Of Widget And Convert To PDF file – Flutter

Taking a screenshot in android was easy.How to do we do it flutter with only specific widget.Everything in flutter needs package for implementing our features.So we will need 3 packages here to take screenshot of widget in image format and convert image to pdf.

In this tutorial,We will learn to take screenshot of widget and convert it to pdf format.We will require these packages from pub.dev.

1.screenshot :^0.2.0 -> To take screenshot of widget.

2.permission_handler:^5.0.1+1 -> To handler permission of storage both android and ios.

3.pdf:^1.11.2 -> To convert screenshot image to pdf.

1.Create sample widget to take screenshot of.

Let’s make widget to get screenshot of.Here we will be building a container with a icon in it.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Screenshot Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Screenshot Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text(widget.title),
      ),
      body: Center(
        child: Icon(Icons.print,size: 40,),
      ),
    );
  }
}

2.Check permissions for storage to save image.

Lets check permission before we take screenshot

void onCheckPermission() async{
  var status = await Permission.storage.status;
  if(status.isDenied || status.isUndetermined){
    if(await Permission.storage.isPermanentlyDenied){
      openAppSettings();
    }else{
      var status1 = await Permission.storage.request();
      if(status1.isGranted){
        takeScreenshot();
      }
    }

  }else{
    takeScreenshot();
  }
}

3.Take screenshot by passing widget.