Home Blog Page 40

How to take screenshots programmatically in flutter

0
Programmatically take screenshot in flutter

Hi Guys, Welcome to Proto Coders Point, In this flutter article will learn how can we programmatically take a screenshot in flutter. Add this feature into your flutter app so that users can take screenshot of the app screen when they want to, just by clicking a button.

Let’s get started – Taking Screenshot in flutter app

To capture our app screen will make use of flutter inBuilt widget i.e. RepaintBoundry.

Using RepaintBoundry widget

RePaintBoundry widget in flutter will capture the view of the child it wrapped with, For Example: if you wrap RepaintBoundry with Container Widget then your app will only capture that widget, suppose if you want to take screenshot of complete app screen then you need to wrap RePaintBoundry to Scaffold widget as done in below complete code.
Then attach a GlobalKey to RepaintBoundry.
Next, by using GlobalKey currentContext we can use .findRenderObject() method helps us in finding current render object.
Next, will use toImage() method to convert render object to image.
then, will convert image into byteData which will again get converted to asUint8List().
Next, will use the Uint8List format to save the screenshot by using ImageGallerySaver package.

Below method take screenshowt and save it.

void _CaptureScreenShot() async{
    //get paint bound of your app screen or the widget which is wrapped with RepaintBoundary.
    RenderRepaintBoundary bound = _key.currentContext!.findRenderObject() as RenderRepaintBoundary;

    if(bound.debugNeedsPaint){
      Timer(Duration(seconds: 1),()=>_CaptureScreenShot());
      return null;
    }

    ui.Image image = await bound.toImage();
    ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);

    // this will save image screenshot in gallery
    if(byteData != null ){
      Uint8List pngBytes = byteData.buffer.asUint8List();
      final resultsave = await ImageGallerySaver.saveImage(Uint8List.fromList(pngBytes),quality: 90,name: 'screenshot-${DateTime.now()}.png');
      print(resultsave);
    }
  }

Programmatically take screenshot in flutter

Complete Code implement to take screenshot in flutter in step

Step 1: Add ImageGallerySaver

In your flutter project, open pubspec.yaml file and under dependencies section add the image gallery saver flutter library.

dependencies:
  image_gallery_saver: '^1.7.1'

Step 2: Add Permission to save image in galery

Android External Storage permission

Open manifest file, and under application tag add:

<application android:requestLegacyExternalStorage="true" .....>

iOS Photo Library usage permission

Navigate to <project>/ios/Runner/info.plist, then between <dict></dict> add below lines

<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app need access to your gallery to save screenshots</string>


Step 3: Complete Code

Refer comment in code for better understanding

main.dart

import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomepage(),
    );
  }
}

class MyHomepage extends StatefulWidget {
  const MyHomepage({Key? key}) : super(key: key);

  @override
  State<MyHomepage> createState() => _MyHomepageState();
}

class _MyHomepageState extends State<MyHomepage> {
  final GlobalKey _key = GlobalKey();

  void _CaptureScreenShot() async{
    //get paint bound of your app screen or the widget which is wrapped with RepaintBoundary.
    RenderRepaintBoundary bound = _key.currentContext!.findRenderObject() as RenderRepaintBoundary;

    if(bound.debugNeedsPaint){
      Timer(Duration(seconds: 1),()=>_CaptureScreenShot());
      return null;
    }

    ui.Image image = await bound.toImage();
    ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);

    // this will save image screenshot in gallery
    if(byteData != null ){
      Uint8List pngBytes = byteData.buffer.asUint8List();
      final resultsave = await ImageGallerySaver.saveImage(Uint8List.fromList(pngBytes),quality: 90,name: 'screenshot-${DateTime.now()}.png');
      print(resultsave);
    }
  }

  @override
  Widget build(BuildContext context) {
    //Here i have wrapped whole app screen scaffold widget, to take full screenshot
    return RepaintBoundary(
      key: _key,
      child: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(color:Colors.blue,width: 220,height: 220,),
              SizedBox(height: 25,),
              ElevatedButton(onPressed: (){
                _CaptureScreenShot(); // Method called to take screenshot on wrapped widget and save it.
              }, child: Text("Capture & Save"))
            ],
          )
        ),
      ),
    );
  }
}

Video Tutorial

How to upgrade node version ubuntu

0
update node version ubuntu
upgrade node version Ubuntu

Hi Guys, Welcome to Proto Coders point. Here is a quick step by step guide to update nodejs in ubuntu OS.

Time needed: 3 minutes

update node version

  1. use n module

    To upgrade node version in ubuntu will make use of n module from npm package.

  2. Install n module

    Firstly install n module using npm run below command.
    sudo npm cache clean -f
    sudo npm install -g n
    sudo n stable
    …so now we have successfully install n module of npm package.

  3. update node version ubuntu to latest

    Now simply run below command, to upgrade nodejs and to update npm version.
    sudo n latest
    Note: this will update your nodejs and npm version to latest version(not to stable current version).

  4. Now check nodejs version

    run below command
    node --version
    npm --version

  5. Optional – update node to specific version

    curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
    sudo apt-get install --reinstall nodejs-legacy # fix /usr/bin/node


If node --version showing you old version still then simply, close and re-open new terminal and run node --version to check nodejs version updated or no.

Flutter Cascade Operator (..) cascade notation in dart

0
flutter dart cascade opetator notation
cascade operator in dart language

Estimated reading time: 1 minute

The Cascade operator notation in flutter dart (..) is two dots, using cascade in dart we can make a sequence of operation using same object & we can even call a function & easily access field or parameter value. By using dart cascade notation (..) we can keep dart code neat & clean as it removes unnecessary temporary variable creation to store data.

Video Tutorial on Flutter Cascade Operator

Cascade Operator in dart example code

In below example We have created a class by name Person which has 2 variable and 1 method to print on console:

  • int? age;
  • String? name;
  • printDetails()

class person

class Person{
  int? age;
  String? name;
  
  void printDetails(){
    print("$name age is $age");
  }
}

In Below Code,

Line No: 3 – > Create an object of Person as p1.

void main(){
  
  Person p1 = Person(); // object creation

  p1..name = "Rajat Palankar"..age = 26 ..printDetails(); 
  // using cascade, initialize the value and call print function
  
}

Then, In line No: 5 -> using object, initialize the value to class variable and call printDetails method of Person Class.

Output : Rajat Palankar age is 26

How to show git brand name on terminal

0
How to show git branch name in Terminal Ubuntu

Time needed: 2 minutes

In this Article will learn how to show git branch name with color in terminal (ubuntu)

  1. Open bashrc file

    Firstly open bashrc file by below cmd.
    gedit ~/.bashrc

  2. Comment old color prompt

    In ~/.bashrc file, you will find a code starting with if[“$color_prompt = yes”] and end’s with fi, just comment this like as shown in below screenshot for reference.ubuntu git bashrc edit

  3. Add below line of code

    parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
       }
       
       if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
       else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
       fi


    Reference Screenshot
    show git branch name on terminal

  4. Final done

    Here is how your terminal shows branch name when you are into git repository project folder, In below screenshot as you can see we can easily get to know on which github branch we are working on(No need to keep on checking git branch -a).how to show git branch name in prompt terminal ubuntu

Steps to install RabbitMQ on ubuntu

0
how to install rabbitmq on ubuntu
install rabbitmq

Hi Guys, Welcome to Proto Coders Point. In this article let’s learn how to install RabbitMQ on ubuntu server.

What is RabbitMQ

RabbitMQ is a message queuing protocol, In simple words it is a software that works as message broker.

Eg: Can we use while build microservices envirnoment.

Install rabbitmq ubuntu

Below are step by step commands to install rabbitmq on ubuntu 20.04.

Step 1: Install curl, gnupg, apt-transport-https

sudo apt-get install curl gnupg apt-transport-https -y
install curl gnupg apt-transport-https

Step 2: Download RabbitMQ signing key

curl -1sLf "https://keys.openpgp.org/vks/v1/by-fingerprint/0A9AF2115F4687BD29803A206B73A36E6026DFCA" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/com.rabbitmq.team.gpg > /dev/null

Step 3: Download cloudsmith Erlang repo

curl -1sLf https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/gpg.E495BB49CC4BBE5B.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg > /dev/null

Step 4: Download RabbitMQ repository

curl -1sLf https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/gpg.9F4587F226208342.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg > /dev/null

Step 5: Add apt repositories rabbitmq.list

sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF

tee in ubuntu is a command line interpreter used to input and output data in a file.

The about command will create a file by name rabbitmq.list, In this list will add below lines of cmd’s

## Provides modern Erlang/OTP releases
##
deb [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main

## Provides RabbitMQ
##
deb [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
EOF

Example

add apt repositories in rabbitmq.list

Step 6: ubuntu apt-get update

sudo apt-get update -y

Step 7: Install ErLang on ubuntu

sudo apt-get install -y erlang-base \
                        erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
                        erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
                        erlang-runtime-tools erlang-snmp erlang-ssl \
                        erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl

Step 8: Finally Install RabbitMQ

sudo apt-get install rabbitmq-server -y --fix-missing

Step 9: RabbitMQ check status

After successful installing rabbitMQ on ubuntu by following above step, To check is rabbitMQ is working or no use below command to check rabbitmq status.

systemctl status rabbitmq-server
check rabbitmq server status