パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v3.0.0

screenshot

Flutter スクリーンショットパッケージ(実行時)。任意のウィジェットを画像としてキャプチャします。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
61.7万
いいね数
1560
Pub ポイント
140

パッケージをインストール

flutter pub add screenshot

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

スクリーンショットパッケージは、複数のプラットフォームで実行時に任意のFlutterウィジェットを画像としてキャプチャする機能を提供します。アプリケーション実行中にウィジェットのスクリーンショットをプログラム的に取得でき、デバッグやテスト、視覚コンテンツの共有に役立ちます。このパッケージはAndroid、iOS、Web、macOS、Windows、Linuxをサポートしており、非常に汎用性が高いです。Flutterのレンダリングパイプラインとシームレスに統合され、ウィジェットツリーから画像を生成するためのシンプルなAPIを提供します。動的な画像生成や視覚フィードバックが必要なアプリに最適です。

用途

  • UI の問題のデバッグ
  • 共有可能な画像の生成
  • 自動テスト
  • ビジュアルドキュメンテーション
  • アプリチュートリアル
  • プレビュー機能

主な機能

  • 実行時ウィジェットスクリーンショットのキャプチャ
  • クロスプラットフォーム対応
  • シンプルな API インテグレーション
  • 高品質な画像出力
  • すべての主要プラットフォームをサポートしています
  • 外部の依存関係なし

適している対象

  • 動的スクリーンショットが必要な開発者
  • テストおよび品質保証チーム
  • ビジュアル共有機能を備えたアプリ
  • 教育的またはチュートリアルアプリ
  • UIプロトタイピングツール
  • 複雑なレイアウトのデバッグ

注意事項

  • 大規模なウィジェットへのパフォーマンスの影響
  • 高解像度画像のメモリ使用量
  • モバイルプラットフォームでは適切な権限が必要です
  • リアルタイム動画キャプチャには適していません
  • 静的ウィジェットスナップショットに限定
  • プラットフォーム固有の構成が必要になる場合があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

screenshot

A simple package to capture widgets as Images. Now you can also capture the widgets that are not rendered on the screen!

This package wraps your widgets inside RenderRepaintBoundary

Source

screenshot

  Capture a widget:

screenshot

 Capture an invisible widget (a widget which is not part of the widget tree):

screenshot

Getting Started

This handy package can be used to capture any Widget including full screen screenshots & individual widgets like Text().

  1. Create Instance of Screenshot Controller
class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  Uint8List _imageFile;

  //Create an instance of ScreenshotController
  ScreenshotController screenshotController = ScreenshotController(); 

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  ...
}
  1. Wrap the widget that you want to capture inside Screenshot Widget. Assign the controller to screenshotController that you have created earlier
Screenshot(
    controller: screenshotController,
    child: Text("This text will be captured as image"),
),
  1. Take the screenshot by calling capture method. This will return a Uint8List
screenshotController.capture().then((Uint8List image) {
    //Capture Done
    setState(() {
        _imageFile = image;
    });
}).catchError((onError) {
    print(onError);
});

Capturing Widgets that are not in the widget tree

You can capture invisible widgets by calling captureFromWidget and passing a widget tree to the function

screenshotController
      .captureFromWidget(Container(
          padding: const EdgeInsets.all(30.0),
          decoration: BoxDecoration(
            border:
                Border.all(color: Colors.blueAccent, width: 5.0),
            color: Colors.redAccent,
          ),
          child: Text("This is an invisible widget")))
      .then((capturedImage) {
    // Handle captured image
  });


Capturing a List Widget

You can capture a long Invisible widgets by using captureFromLongWidget function.

var randomItemCount = Random().nextInt(100);

///
/// Step 1: Create Long widget using Column.
///

var myLongWidget = Builder(builder: (context) {
  return Container(
      padding: const EdgeInsets.all(30.0),
      decoration: BoxDecoration(
        border:
            Border.all(color: Colors.blueAccent, width: 5.0),
        color: Colors.redAccent,
      ),
      ///
      /// Note: Do not use Scrolling widget, instead place your children in Column. 
      ///  
      /// Do not use widgets like 'Expanded','Flexible',or 'Spacer'
      ///
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          for (int i = 0; i < randomItemCount; i++)
            Text("Tile Index $i"),
        ],
      ));
});

///
/// Step 2: 
///    Use `captureFromLongWidget` function for taking screenshot.
///
screenshotController
      .captureFromLongWidget(
          InheritedTheme.captureAll(
            context, 
            Material(
              child: myLongWidget,
            ),
          ),
          delay: Duration(milliseconds: 100),
          context: context,


          ///
          /// Additionally you can define constraint for your image.
          ///
          /// constraints: BoxConstraints(
          ///   maxHeight: 1000,
          ///   maxWidth: 1000,
          /// )
      )
      .then((capturedImage) {
    // Handle captured image
  });


Saving images to Specific Location

For this you can use captureAndSave method by passing directory location. By default, the captured image will be saved to Application Directory. Custom paths can be set using path parameter. Refer path_provider

Note

Method captureAndSave is not supported for web.

final directory = (await getApplicationDocumentsDirectory ()).path; //from path_provide package
String fileName = DateTime.now().microsecondsSinceEpoch;
path = '$directory';

screenshotController.captureAndSave(
    path //set path where screenshot will be saved
    fileName:fileName 
);

Saving images to Gallery

If you want to save captured image to Gallery, Please use https://github.com/hui-z/image_gallery_saver Example app uses the same to save screenshots to gallery.


Sharing Captured Images

await _screenshotController.capture(delay: const Duration(milliseconds: 10)).then((Uint8List image) async {
      if (image != null) {
        final directory = await getApplicationDocumentsDirectory();
        final imagePath = await File('${directory.path}/image.png').create();
        await imagePath.writeAsBytes(image);

        /// Share Plugin
        await Share.shareFiles([imagePath.path]);
      }
    });

Note:

Captured image may look pixelated. You can overcome this issue by setting value for pixelRatio

The pixelRatio describes the scale between the logical pixels and the size of the output image. It is independent of the window.devicePixelRatio for the device, so specifying 1.0 (the default) will give you a 1:1 mapping between logical pixels and the output pixels in the image.

double pixelRatio = MediaQuery.of(context).devicePixelRatio;

screenshotController.capture(
    pixelRatio: pixelRatio //1.5
)

Sometimes rastergraphics like images may not be captured by the plugin with default configurations. The issue is discussed here.

...screenshot is taken before the GPU thread is done rasterizing the frame 
so the screenshot of the previous frame is taken, which is wrong.

The solution is to add a small delay before capturing.

screenshotController.capture(delay: Duration(milliseconds: 10))

Known Issues

  • Platform Views are not supported. (Example: Google Maps, Camera etc)issue

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト