Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theFlutter 3.41 blog post!

Transforming assets at build time

How to set up automatic transformation of images (and other assets) in your Flutter app.

You can configure your project to automatically transform assets at build time using compatible Dart packages.

Specifying asset transformations

#

In thepubspec.yaml file, list the assets to be transformed and the associated transformer package.

yaml
flutter:assets:-path:assets/logo.svgtransformers:-package:vector_graphics_compiler

With this configuration,assets/logo.svg is transformed by thevector_graphics_compiler package as it is copied to the build output. This package precompiles SVG files into an optimized binary files that can be displayed using thevector_graphics package, like so:

dart
import'package:vector_graphics/vector_graphics.dart';constWidgetlogo=VectorGraphic(loader:AssetBytesLoader('assets/logo.svg'));

Passing arguments to asset transformers

#

To pass a string of arguments to an asset transformer, also specify that in the pubspec:

yaml
flutter:assets:-path:assets/logo.svgtransformers:-package:vector_graphics_compilerargs:['--tessellate','--font-size=14']

Chaining asset transformers

#

Asset transformers can be chained and are applied in the order they are declared. Consider the following example using imaginary packages:

yaml
flutter:assets:-path:assets/bird.pngtransformers:-package:grayscale_filter-package:png_optimizer

Here,bird.png is transformed by thegrayscale_filter package. The output is then transformed by thepng_optimizer package before being bundled into the built app.

Writing asset transformer packages

#

An asset transformer is a Dartcommand-line app that is invoked withdart run with at least two arguments:--input, which contains the path to the file to transform and--output, which is the location where the transformer code must write its output to.

If the transformer finishes with a non-zero exit code, the application build fails with error message explaining that transformation of the asset failed. Anything written to thestderr stream of the process by the transformer is included in the error message.

During the invocation of the transformer, theFLUTTER_BUILD_MODE environment variable will be set to the CLI name of the build mode being used. For example, if you run your app withflutter run -d macos --release, thenFLUTTER_BUILD_MODE will be set torelease.

Sample

#

For a sample Flutter project that uses asset transformation and includes a custom Dart package that is used as a transformer, check out theasset_transformers project in the Flutter samples repo.

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.38.6. Page last updated on 2025-09-22.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp