Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ foreground of the Android 13+ themed icon. For more information see [Android Ada
- `web`: Add web related configs
- `generate`: Specifies whether to generate icons for this platform or not
- `image_path`: Path to web icon.png
- `image_path_favicon`: The location of the icon image file used to generate the favicon (optional - if not defined then falls back to using the web image_path then the global image_path)
- `background_color`: Updates *background_color* in `web/manifest.json`
- `theme_color`: Updates *theme_color* in `web/manifest.json`

Expand Down
1 change: 1 addition & 0 deletions bin/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ flutter_launcher_icons:
web:
generate: true
image_path: "path/to/image.png"
# image_path_favicon: "assets/icon/icon-favicon.png"
background_color: "#hexcode"
theme_color: "#hexcode"

Expand Down
5 changes: 5 additions & 0 deletions lib/config/web_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class WebConfig {
@JsonKey(name: 'image_path')
final String? imagePath;

/// Override image path for favicon
@JsonKey(name: 'image_path_favicon')
final String? imagePathFavicon;

/// manifest.json's background_color
@JsonKey(name: 'background_color')
final String? backgroundColor;
Expand All @@ -27,6 +31,7 @@ class WebConfig {
const WebConfig({
this.generate = false,
this.imagePath,
this.imagePathFavicon,
this.backgroundColor,
this.themeColor,
});
Expand Down
4 changes: 4 additions & 0 deletions lib/config/web_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions lib/web/web_icon_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class WebIconGenerator extends IconGenerator {
context.webConfig!.imagePath ?? context.config.imagePath!,
);

// load and decode the image file
context.logger
.verbose('Decoding and loading image file at $imgFilePath...');
final imgFile = utils.decodeImageFile(imgFilePath);
Expand All @@ -61,9 +62,31 @@ class WebIconGenerator extends IconGenerator {
throw FileNotFoundException(imgFilePath);
}

// resolve the favicon image path and file, which is either one explicitly
// provided or the same as the image file loaded above
late final String faviconImgFilePath;
late final Image faviconImgFile;
final faviconImgFileOverride = context.webConfig!.imagePathFavicon;
if (faviconImgFileOverride != null) {
// favicon override was specified, construct the full path and decode
faviconImgFilePath =
path.join(context.prefixPath, faviconImgFileOverride);
final imgFile = utils.decodeImageFile(faviconImgFilePath);
if (imgFile == null) {
context.logger
.error('Image File not found at give path $faviconImgFilePath...');
throw FileNotFoundException(faviconImgFilePath);
}
faviconImgFile = imgFile;
} else {
// no favicon override, use the fallback image file
faviconImgFilePath = imgFilePath;
faviconImgFile = imgFile;
}

// generate favicon in web/favicon.png
context.logger.verbose('Generating favicon from $imgFilePath...');
_generateFavicon(imgFile);
context.logger.verbose('Generating favicon from $faviconImgFilePath...');
_generateFavicon(faviconImgFile);

// generate icons in web/icons/
context.logger.verbose('Generating icons from $imgFilePath...');
Expand Down
3 changes: 3 additions & 0 deletions test/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void main() {
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
'image_path_favicon': null,
'background_color': '#0175C2',
'theme_color': '#0175C2',
}),
Expand Down Expand Up @@ -155,6 +156,7 @@ void main() {
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
'image_path_favicon': 'app_icon.png',
'background_color': '#0175C2',
'theme_color': '#0175C2',
}),
Expand Down Expand Up @@ -238,6 +240,7 @@ void main() {
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
'image_path_favicon': null,
'background_color': '#0175C2',
'theme_color': '#0175C2',
}),
Expand Down
1 change: 1 addition & 0 deletions test/templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ flutter_launcher_icons:
web:
generate: true
image_path: "app_icon.png" # filepath
image_path_favicon: "app_icon.png" # filepath
background_color: "#0175C2" # hex_color
theme_color: "#0175C2" # hex_color
apple_mobile_web_app_title: "demo"
Expand Down
1 change: 1 addition & 0 deletions test/web/web_icon_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void main() {
d.file('flutter_launcher_icons.yaml', templates.fliWebConfig),
d.file('pubspec.yaml', templates.pubspecTemplate),
d.file('app_icon.png', imageFile.readAsBytesSync()),
d.file('app_icon_favicon.png', imageFile.readAsBytesSync()),
]).create();
prefixPath = path.join(d.sandbox, 'fli_test');
config = Config.loadConfigFromPath(
Expand Down