native_screenshot 0.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 85

native_screenshot #

This plugin aims to be a simple one that implements taking screenshot natively to capture scenes like camera preview or AR views. This cannot be done easily in plain Flutter, at least I cannot make it work using RenderRepaintBoundary and similar techniques.

The main difference with another packages is that they shows a share dialog. This plugin saves the image and returns the path to it. On Android also launch an updating request (internally) to reload the media library.

Instalation #

Add

native_screenshot: ^0.0.3

to your pubspec.yaml file.

Android #

You must add

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

to your AndroidManifest.xml inside android/src/main/ directory.

iOS #

If don't add

<key>NSPhotoLibraryAddUsageDescription</key>
<string>Take pretty screenshots and save it to the PhotoLibrary.</string>

to your info.plist file inside ios/Runner directory, the application will crash.

Use #

Import the library:

import 'package:native_screenshot/native_screenshot.dart';

and take a screenshot:

String path = await NativeScreenshot.takeScreenshot()

In error case the function returns null and the screenshot path if success.

Acknowledgments #

This is based on screenshot_share_image and capture_and_share packages. Thanks to @toonztudio for pointing me out replying in Github.

0.0.4 - 15.05.2020 #

  • Homepage, Repository and Issuetracker updated to GitLab ones.

Android #

  • Added some log messages to make easier to track errors.
  • Class names minified/obfuscated by Android release mode were preventing get the correct class of the needed view for take the screenshot. Fixed comparing classes instead names. ivanjpg/native_screenshot#2

0.0.3 #

  • Homepage, Repository and Issuetracker added to pubspec.yaml.

iOS #

  • Code improvement.
  • The function now takes the screenshot, save it to the application directory and then it adds to the PhotoLibrary. Now it needs NSPhotoLibraryAddUsageDescription key into info.plist file.

0.0.2 #

  • Description added to pubspec.yaml.
  • Documentation API done.
  • Added licence.
  • README.md updated

Android #

  • Unified the code to condesate the plugin initialization and use for the v1 and v2 Flutter's plugin engine.
  • Now the screenshot is written to a folder with the application name on it, if fails then goes to external storage.
  • Tried Canvas and PixelCopy approaches, both throws a black image, still uses getBitmap() method.

0.0.1 #

iOS #

  • Basic functionionality done.

Android #

  • Tries to request permission if possible and considers PixelCopy (API >= 26) that still not works; getBitmap() works, so this is used for now.

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:native_screenshot/native_screenshot.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _scaffoldKey = GlobalKey<ScaffoldState>();

  Widget _imgHolder;

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

    _imgHolder = Center(
      child: Icon(Icons.image),
    );
  } // initState()

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
          title: Text('NativeScreenshot Example'),
        ),
        bottomNavigationBar: ButtonBar(
          alignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('Press to capture screenshot'),
              onPressed: () async {
                String path = await NativeScreenshot.takeScreenshot();

                debugPrint('Screenshot taken, path: $path');

                if( path == null || path.isEmpty ) {
                  _scaffoldKey.currentState.showSnackBar(
                    SnackBar(
                      content: Text('Error taking the screenshot :('),
                      backgroundColor: Colors.red,
                    )
                  ); // showSnackBar()

                  return;
                } // if error

                _scaffoldKey.currentState.showSnackBar(
                  SnackBar(
                    content: Text('The screenshot has been saved to: $path')
                  )
                ); // showSnackBar()

                File imgFile = File(path);
                _imgHolder = Image.file(imgFile);

                setState(() {});
              },
            )
          ],
        ),
        body: Container(
          constraints: BoxConstraints.expand(),
          child: _imgHolder,
        ),
      ),
    );
  } // build()
} // _MyAppState

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  native_screenshot: ^0.0.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:native_screenshot/native_screenshot.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
75
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
85
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:native_screenshot/native_screenshot.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:native_screenshot/native_screenshot.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:native_screenshot/native_screenshot.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:native_screenshot/native_screenshot.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [native_screenshot] that is in a package requiring null.

Health suggestions

Format lib/native_screenshot.dart.

Run flutter format to format lib/native_screenshot.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test