Sub topic 5
Trimming Images, Assets and Fonts
This chapter assumes that you can run a Flutter app, edit pubspec.yaml, and use basic Dart. The goal is to reduce shipped bytes without lowering the visual quality that users need.
Start with the bytes users actually need
An asset is a file that Flutter packages with your application, such as an image, font, JSON file, or animation. Every declared asset can increase the download size, so review the asset list before changing file formats.
Use explicit paths when practical. A directory entry can include files that are no longer used.
name: size_demo
description: An example Flutter application
publish_to: "none"
environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
flutter:
sdk: flutter
flutter:
assets:
- assets/images/logo.webp
- assets/images/empty-state.webp
fonts:
- family: AppText
fonts:
- asset: assets/fonts/app_text_regular.ttf
- asset: assets/fonts/app_text_bold.ttf
weight: 700Remove old files and their declarations together. A file that remains under a declared asset directory can still be packaged even when no Dart code references it.
Resize and encode images
The largest image mistake is shipping a source image that is much larger than its displayed size. If a banner is displayed at 800 by 400 pixels, a 4000 by 2000 source contains five times as many pixels in each dimension and roughly twenty-five times as many pixels overall.
Choose the format based on the image:
| Image content | Usual choice | Reason |
|---|---|---|
| Photos | WebP or carefully compressed JPEG | Good lossy compression |
| Flat illustrations | WebP, PNG, or SVG | Depends on transparency and complexity |
| Small icons | SVG or a small raster image | Scales well when the SVG is simple |
| Transparency | WebP, PNG, or SVG | Preserve the alpha channel |
Use density variants when an image must remain sharp on different screens. Flutter can select a 2.0x or 3.0x asset automatically, but do not generate variants that are much larger than the devices you support.
cacheWidth and cacheHeight limit the decoded image in memory. They do not reduce the file stored in the application. Resize and compress the source file separately.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: LogoPage()));
}
class LogoPage extends StatelessWidget {
const LogoPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Image(
image: AssetImage('assets/images/logo.webp'),
cacheWidth: 400,
fit: BoxFit.contain,
),
),
);
}
}Trim fonts deliberately
Fonts can occupy several megabytes, especially when they contain many writing systems or unused weights. Declare only the families and weights that the interface uses. A font weight is the thickness variant, such as regular or bold.
If your application supports a limited set of languages, use a legally permitted subset of the font containing those characters. Test every supported language after subsetting. Missing glyphs can silently appear as replacement boxes.
Flutter can remove unused glyphs from supported icon fonts in release builds. Avoid disabling icon tree shaking unless you need dynamically selected icons that the compiler cannot discover. If you do disable it, measure the size increase.
Common errors
- Compressing an image but leaving its dimensions unnecessarily large.
- Assuming
cacheWidthchanges the download size. - Declaring an entire asset directory for convenience.
- Removing font glyphs without testing every supported language.
- Using a complex SVG for a fixed-size illustration when a compressed raster image would be smaller.
Exercise
An image is displayed at 300 pixels wide, but its source is 2400 pixels wide. Which two changes should you consider first?
Show answer
Resize the source near the required display size and encode it with a suitable format. Use cacheWidth only to control decoded memory, not shipped size.