diff --git a/.gitignore b/.gitignore index 45ce92b464..0e8c2f9fbf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,117 @@ -# Files and directories created by pub -.packages +# Miscellaneous +*.class +*.lock +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# Visual Studio Code related +.classpath +.project +.settings/ +.vscode/ + +# Flutter repo-specific +/bin/cache/ +/bin/internal/bootstrap.bat +/bin/internal/bootstrap.sh +/bin/mingit/ +/dev/benchmarks/mega_gallery/ +/dev/bots/.recipe_deps +/dev/bots/android_tools/ +/dev/devicelab/ABresults*.json +/dev/docs/doc/ +/dev/docs/flutter.docs.zip +/dev/docs/lib/ +/dev/docs/pubspec.yaml +/dev/integration_tests/**/xcuserdata +/dev/integration_tests/**/Pods +/packages/flutter/coverage/ +version +analysis_benchmark.json + +# packages file containing multi-root paths +.packages.generated + +# Flutter/Dart/Pub related +**/doc/api/ .dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +**/generated_plugin_registrant.dart +.packages +.pub-cache/ .pub/ -.idea/ build/ -# Remove the following pattern if you wish to check in your lock file -pubspec.lock +flutter_*.png +linked_*.ds +unlinked.ds +unlinked_spec.ds + +# Android related +**/android/**/gradle-wrapper.jar +**/android/.gradle +**/android/captures/ +**/android/gradlew +**/android/gradlew.bat +**/android/local.properties +**/android/**/GeneratedPluginRegistrant.java +**/android/key.properties +*.jks + +# iOS/XCode related +**/ios/**/*.mode1v3 +**/ios/**/*.mode2v3 +**/ios/**/*.moved-aside +**/ios/**/*.pbxuser +**/ios/**/*.perspectivev3 +**/ios/**/*sync/ +**/ios/**/.sconsign.dblite +**/ios/**/.tags* +**/ios/**/.vagrant/ +**/ios/**/DerivedData/ +**/ios/**/Icon? +**/ios/**/Pods/ +**/ios/**/.symlinks/ +**/ios/**/profile +**/ios/**/xcuserdata +**/ios/.generated/ +**/ios/Flutter/.last_build_id +**/ios/Flutter/App.framework +**/ios/Flutter/Flutter.framework +**/ios/Flutter/Flutter.podspec +**/ios/Flutter/Generated.xcconfig +**/ios/Flutter/app.flx +**/ios/Flutter/app.zip +**/ios/Flutter/flutter_assets/ +**/ios/Flutter/flutter_export_environment.sh +**/ios/ServiceDefinitions.json +**/ios/Runner/GeneratedPluginRegistrant.* + +# macOS +**/macos/Flutter/GeneratedPluginRegistrant.swift + +# Coverage +coverage/ + +# Symbols +app.*.symbols -# Directory created by dartdoc -doc/api/ +# Exceptions to above rules. +!**/ios/**/default.mode1v3 +!**/ios/**/default.mode2v3 +!**/ios/**/default.pbxuser +!**/ios/**/default.perspectivev3 +!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages +!/dev/ci/**/Gemfile.lock \ No newline at end of file diff --git a/example/android/local.properties b/example/android/local.properties new file mode 100644 index 0000000000..06dcee3290 --- /dev/null +++ b/example/android/local.properties @@ -0,0 +1,3 @@ +sdk.dir=C:\\Users\\yanco\\AppData\\Local\\Android\\Sdk +flutter.sdk=C:\\flutter +flutter.buildMode=debug \ No newline at end of file diff --git a/example/default_example/pubspec.yaml b/example/default_example/pubspec.yaml index bbb09bca4b..c7a873471f 100644 --- a/example/default_example/pubspec.yaml +++ b/example/default_example/pubspec.yaml @@ -21,6 +21,7 @@ flutter_icons: ios: true # can specify file name here e.g. "My-Launcher-Icon" adaptive_icon_background: "assets/images/christmas-background.png" # only available for Android 8.0 devices and above adaptive_icon_foreground: "assets/images/icon-foreground-432x432.png" # only available for Android 8.0 devices and above + adaptive_icon_foreground_scale_factor: 1.0 # Test adaptive icon scaling dev_dependencies: flutter_test: diff --git a/lib/android.dart b/lib/android.dart index 5025a6ee48..1ec888b92b 100644 --- a/lib/android.dart +++ b/lib/android.dart @@ -1,9 +1,11 @@ import 'dart:io'; +import 'dart:math'; + +import 'package:flutter_launcher_icons/constants.dart' as constants; +import 'package:flutter_launcher_icons/custom_exceptions.dart'; import 'package:flutter_launcher_icons/utils.dart'; import 'package:flutter_launcher_icons/xml_templates.dart' as xml_template; import 'package:image/image.dart'; -import 'package:flutter_launcher_icons/custom_exceptions.dart'; -import 'package:flutter_launcher_icons/constants.dart' as constants; class AndroidIconTemplate { AndroidIconTemplate({required this.size, required this.directoryName}); @@ -68,24 +70,71 @@ bool isAndroidIconNameCorrectFormat(String iconName) { return true; } +/// Rescales image by creating a different sized canvas and copying image centered onto the new canvas +Image rescaleImage(Image image, double scaleFactor, {int fillColor = 0}) { + printStatus('Rescaling icon by $scaleFactor'); + final int scaledDimension = + (max(image.width, image.height) * 1 / scaleFactor).floor(); + if (image.width != image.height) { + printStatus( + 'Foreground image is not square! Scaled canvas will be made square ' + 'to meet Adaptive Icon Specifications'); + } + final Image scaledImage = Image(scaledDimension, scaledDimension); + scaledImage.fill(fillColor); + copyInto( + scaledImage, + image, + dstX: ((scaledDimension - image.width) / 2).floor(), + dstY: ((scaledDimension - image.height) / 2).floor(), + ); + return scaledImage; +} + void createAdaptiveIcons( Map flutterLauncherIconsConfig, String? flavor) { printStatus('Creating adaptive icons Android'); - // Retrieve the necessary Flutter Launcher Icons configuration from the pubspec.yaml file + // Retrieve the necessary Flutter Launcher Icons configuration from the yaml file final String backgroundConfig = flutterLauncherIconsConfig['adaptive_icon_background']; final String foregroundImagePath = flutterLauncherIconsConfig['adaptive_icon_foreground']; - final Image? foregroundImage = decodeImageFile(foregroundImagePath); - if (foregroundImage == null) { - return; - } + final Image? foregroundImage = + decodeImage(File(foregroundImagePath).readAsBytesSync()); + final double? foregroundScaleFactor = + flutterLauncherIconsConfig['adaptive_icon_foreground_scale_factor']; + final String? foregroundScaleFillColor = + flutterLauncherIconsConfig['adaptive_icon_foreground_scale_fill_color']; - // Create adaptive icon foreground images - for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) { - overwriteExistingIcons(androidIcon, foregroundImage, - constants.androidAdaptiveForegroundFileName, flavor); + final bool rescale = foregroundScaleFactor != null && + foregroundScaleFactor > 0 && + foregroundImage != null; + + // Scales the foreground image prior to converting to icon. This is mainly for scaling down to match adaptive icon spec + if (rescale) { + Image rescaledImage; + + int _getColorFromHex(String hexColor) { + //Converts hex string to int + hexColor = hexColor.toUpperCase().replaceAll('#', ''); + return int.parse(hexColor, radix: 16); + } + + rescaledImage = rescaleImage(foregroundImage, foregroundScaleFactor, + fillColor: foregroundScaleFillColor != null && + foregroundScaleFillColor.isNotEmpty + ? _getColorFromHex(foregroundScaleFillColor) + : 0); + + // Create adaptive icon foreground images + for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) { + overwriteExistingIcons( + androidIcon, + rescale ? rescaledImage : foregroundImage, + constants.androidAdaptiveForegroundFileName, + flavor); + } } // Create adaptive icon background diff --git a/lib/ios.dart b/lib/ios.dart index c051792d94..d60f50fd1f 100644 --- a/lib/ios.dart +++ b/lib/ios.dart @@ -1,8 +1,9 @@ import 'dart:convert'; import 'dart:io'; + +import 'package:flutter_launcher_icons/constants.dart'; import 'package:flutter_launcher_icons/utils.dart'; import 'package:image/image.dart'; -import 'package:flutter_launcher_icons/constants.dart'; /// File to handle the creation of icons for iOS platform class IosIconTemplate { diff --git a/lib/main.dart b/lib/main.dart index 1edea61726..a4bbd4e0ea 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,12 +1,12 @@ import 'dart:io'; import 'package:args/args.dart'; -import 'package:path/path.dart' as path; -import 'package:yaml/yaml.dart'; import 'package:flutter_launcher_icons/android.dart' as android_launcher_icons; -import 'package:flutter_launcher_icons/ios.dart' as ios_launcher_icons; import 'package:flutter_launcher_icons/constants.dart'; import 'package:flutter_launcher_icons/custom_exceptions.dart'; +import 'package:flutter_launcher_icons/ios.dart' as ios_launcher_icons; +import 'package:path/path.dart' as path; +import 'package:yaml/yaml.dart'; const String fileOption = 'file'; const String helpFlag = 'help';