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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ flutter_native_splash:
# https://developer.android.com/guide/topics/manifest/activity-element#screen
#android_screen_orientation: sensorLandscape

# By specifying the used minimum SDK version of Android, flutter_native_splash might be able to
# merge/remove certain styles and drawables.
#android_min_sdk: 24 # default 16

# hide notif bar on android. ios already hides it by default.
# Has no effect in web since web has no notification bar.
fullscreen: true # default false
Expand Down
168 changes: 105 additions & 63 deletions lib/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,33 @@ void _createAndroidSplash({
required String? screenOrientation,
String? android12BrandingImagePath,
String? android12DarkBrandingImagePath,
int? androidMinSdk,
}) {
androidMinSdk ??= 16;
final androidDrawableFolder =
_flavorHelper.getAndroidDrawableFolder(androidMinSdk);
final androidNightDrawableFolder =
_flavorHelper.getAndroidNightDrawableFolder(androidMinSdk);
final androidLaunchBackgroundFile =
_flavorHelper.getAndroidLaunchBackgroundFile(androidMinSdk);
final androidLaunchDarkBackgroundFile =
_flavorHelper.getAndroidLaunchDarkBackgroundFile(androidMinSdk);
final androidStylesFile = _flavorHelper.getAndroidStylesFile(androidMinSdk);
final androidNightStylesFile =
_flavorHelper.getAndroidNightStylesFile(androidMinSdk);
final androidV31StylesFile =
_flavorHelper.getAndroidV31StylesFile(androidMinSdk);
final androidV31NightStylesFile =
_flavorHelper.getAndroidV31NightStylesFile(androidMinSdk);
final androidV21DrawableFolder =
_flavorHelper.getAndroidV21DrawableFolder(androidMinSdk);
final androidV21LaunchBackgroundFile =
_flavorHelper.getAndroidV21LaunchBackgroundFile(androidMinSdk);
final androidV21NightDrawableFolder =
_flavorHelper.getAndroidV21NightDrawableFolder(androidMinSdk);
final androidV21LaunchDarkBackgroundFile =
_flavorHelper.getAndroidV21LaunchDarkBackgroundFile(androidMinSdk);

_applyImageAndroid(imagePath: imagePath);

_applyImageAndroid(imagePath: darkImagePath, dark: true);
Expand Down Expand Up @@ -109,65 +135,73 @@ void _createAndroidSplash({
fileName: 'android12branding.png',
);

_createBackground(
colorString: color,
darkColorString: darkColor,
darkBackgroundImageSource: darkBackgroundImage,
backgroundImageSource: backgroundImage,
darkBackgroundImageDestination:
'${_flavorHelper.androidNightDrawableFolder}background.png',
backgroundImageDestination:
'${_flavorHelper.androidDrawableFolder}background.png',
);
if (androidNightDrawableFolder != null && androidDrawableFolder != null) {
_createBackground(
colorString: color,
darkColorString: darkColor,
darkBackgroundImageSource: darkBackgroundImage,
backgroundImageSource: backgroundImage,
darkBackgroundImageDestination:
'${androidNightDrawableFolder}background.png',
backgroundImageDestination: '${androidDrawableFolder}background.png',
);
}

_createBackground(
colorString: color,
darkColorString: darkColor,
darkBackgroundImageSource: darkBackgroundImage,
backgroundImageSource: backgroundImage,
darkBackgroundImageDestination:
'${_flavorHelper.androidNightV21DrawableFolder}background.png',
backgroundImageDestination:
'${_flavorHelper.androidV21DrawableFolder}background.png',
);
if (androidV21NightDrawableFolder != null &&
androidV21DrawableFolder != null) {
_createBackground(
colorString: color,
darkColorString: darkColor,
darkBackgroundImageSource: darkBackgroundImage,
backgroundImageSource: backgroundImage,
darkBackgroundImageDestination:
'${androidV21NightDrawableFolder}background.png',
backgroundImageDestination: '${androidV21DrawableFolder}background.png',
);
}

// ignore_for_file: avoid_print
print('[Android] Updating launch background(s) with splash image path...');

_applyLaunchBackgroundXml(
gravity: gravity,
launchBackgroundFilePath: _flavorHelper.androidLaunchBackgroundFile,
showImage: imagePath != null,
showBranding: brandingImagePath != null,
brandingGravity: brandingGravity,
brandingBottomPadding: brandingBottomPadding,
);
if (androidLaunchBackgroundFile != null) {
_applyLaunchBackgroundXml(
gravity: gravity,
launchBackgroundFilePath: androidLaunchBackgroundFile,
showImage: imagePath != null,
showBranding: brandingImagePath != null,
brandingGravity: brandingGravity,
brandingBottomPadding: brandingBottomPadding,
);
}

if (darkColor != null || darkBackgroundImage != null) {
if (androidLaunchDarkBackgroundFile != null &&
(darkColor != null || darkBackgroundImage != null)) {
_applyLaunchBackgroundXml(
gravity: gravity,
launchBackgroundFilePath: _flavorHelper.androidLaunchDarkBackgroundFile,
launchBackgroundFilePath: androidLaunchDarkBackgroundFile,
showImage: imagePath != null,
showBranding: brandingImagePath != null,
brandingGravity: brandingGravity,
brandingBottomPadding: brandingBottomPadding,
);
}

if (Directory(_flavorHelper.androidV21DrawableFolder).existsSync()) {
if (androidV21DrawableFolder != null &&
androidV21LaunchBackgroundFile != null &&
Directory(androidV21DrawableFolder).existsSync()) {
_applyLaunchBackgroundXml(
gravity: gravity,
launchBackgroundFilePath: _flavorHelper.androidV21LaunchBackgroundFile,
launchBackgroundFilePath: androidV21LaunchBackgroundFile,
showImage: imagePath != null,
showBranding: brandingImagePath != null,
brandingGravity: brandingGravity,
brandingBottomPadding: brandingBottomPadding,
);
if (darkColor != null || darkBackgroundImage != null) {
if (androidV21LaunchDarkBackgroundFile != null &&
(darkColor != null || darkBackgroundImage != null)) {
_applyLaunchBackgroundXml(
gravity: gravity,
launchBackgroundFilePath:
_flavorHelper.androidV21LaunchDarkBackgroundFile,
launchBackgroundFilePath: androidV21LaunchDarkBackgroundFile,
showImage: imagePath != null,
showBranding: brandingImagePath != null,
brandingGravity: brandingGravity,
Expand All @@ -177,37 +211,45 @@ void _createAndroidSplash({
}

print('[Android] Updating styles...');
_applyStylesXml(
fullScreen: fullscreen,
file: _flavorHelper.androidV31StylesFile,
template: _androidV31StylesXml,
android12BackgroundColor: android12BackgroundColor,
android12ImagePath: android12ImagePath,
android12IconBackgroundColor: android12IconBackgroundColor,
android12BrandingImagePath: android12BrandingImagePath,
);
if (androidV31StylesFile != null) {
_applyStylesXml(
fullScreen: fullscreen,
file: androidV31StylesFile,
template: _androidV31StylesXml,
android12BackgroundColor: android12BackgroundColor,
android12ImagePath: android12ImagePath,
android12IconBackgroundColor: android12IconBackgroundColor,
android12BrandingImagePath: android12BrandingImagePath,
);
}

_applyStylesXml(
fullScreen: fullscreen,
file: _flavorHelper.androidV31StylesNightFile,
template: _androidV31StylesNightXml,
android12BackgroundColor: android12DarkBackgroundColor,
android12ImagePath: android12DarkImagePath,
android12IconBackgroundColor: darkAndroid12IconBackgroundColor,
android12BrandingImagePath: android12DarkBrandingImagePath,
);
if (androidV31NightStylesFile != null) {
_applyStylesXml(
fullScreen: fullscreen,
file: androidV31NightStylesFile,
template: _androidV31StylesNightXml,
android12BackgroundColor: android12DarkBackgroundColor,
android12ImagePath: android12DarkImagePath,
android12IconBackgroundColor: darkAndroid12IconBackgroundColor,
android12BrandingImagePath: android12DarkBrandingImagePath,
);
}

_applyStylesXml(
fullScreen: fullscreen,
file: _flavorHelper.androidStylesFile,
template: _androidStylesXml,
);
if (androidStylesFile != null) {
_applyStylesXml(
fullScreen: fullscreen,
file: androidStylesFile,
template: _androidStylesXml,
);
}

_applyStylesXml(
fullScreen: fullscreen,
file: _flavorHelper.androidNightStylesFile,
template: _androidStylesNightXml,
);
if (androidNightStylesFile != null) {
_applyStylesXml(
fullScreen: fullscreen,
file: androidNightStylesFile,
template: _androidStylesNightXml,
);
}

_applyOrientation(orientation: screenOrientation);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/cli_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void _createSplashByConfig(Map<String, dynamic> config) {
}
final String? androidScreenOrientation =
config[_Parameter.androidScreenOrientation] as String?;
final int? androidMinSdk = config[_Parameter.androidMinSdk] as int?;
final brandingGravity =
config[_Parameter.brandingGravity] as String? ?? 'bottom';
final bool fullscreen = config[_Parameter.fullscreen] as bool? ?? false;
Expand Down Expand Up @@ -173,6 +174,7 @@ void _createSplashByConfig(Map<String, dynamic> config) {
brandingGravity: brandingGravity,
fullscreen: fullscreen,
screenOrientation: androidScreenOrientation,
androidMinSdk: androidMinSdk,
android12ImagePath: android12Image,
android12DarkImagePath: android12DarkImage ?? android12Image,
android12BackgroundColor: android12Color,
Expand Down Expand Up @@ -420,6 +422,7 @@ String? parseColor(dynamic color) {
class _Parameter {
static const android = 'android';
static const android12Section = 'android_12';
static const androidMinSdk = 'android_min_sdk';
static const androidScreenOrientation = 'android_screen_orientation';
static const backgroundImage = 'background_image';
static const backgroundImageAndroid = 'background_image_android';
Expand Down Expand Up @@ -470,6 +473,7 @@ class _Parameter {
static List<String> all = [
android,
android12Section,
androidMinSdk,
androidScreenOrientation,
backgroundImage,
backgroundImageAndroid,
Expand Down
54 changes: 54 additions & 0 deletions lib/flavor_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,60 @@ class _FlavorHelper {
return '${androidNightV21DrawableFolder}launch_background.xml';
}

String? getAndroidDrawableFolder(int minSdk) {
return minSdk < 21 ? androidDrawableFolder : null;
}

String? getAndroidNightDrawableFolder(int minSdk) {
return minSdk < 21 ? androidNightDrawableFolder : null;
}

String? getAndroidLaunchBackgroundFile(int minSdk) {
return minSdk < 21 ? androidLaunchBackgroundFile : null;
}

String? getAndroidLaunchDarkBackgroundFile(int minSdk) {
return minSdk < 21 ? androidLaunchDarkBackgroundFile : null;
}

String? getAndroidStylesFile(int minSdk) {
return minSdk < 31 ? androidStylesFile : null;
}

String? getAndroidNightStylesFile(int minSdk) {
return minSdk < 31 ? androidNightStylesFile : null;
}

String? getAndroidV31StylesFile(int minSdk) {
return minSdk < 31 ? androidV31StylesFile : androidStylesFile;
}

String? getAndroidV31NightStylesFile(int minSdk) {
return minSdk < 31 ? androidV31StylesNightFile : androidNightStylesFile;
}

String? getAndroidV21DrawableFolder(int minSdk) {
return minSdk < 21 ? androidV21DrawableFolder : androidDrawableFolder;
}

String? getAndroidV21LaunchBackgroundFile(int minSdk) {
return minSdk < 21
? androidV21LaunchBackgroundFile
: androidLaunchBackgroundFile;
}

String? getAndroidV21NightDrawableFolder(int minSdk) {
return minSdk < 21
? androidNightV21DrawableFolder
: androidNightDrawableFolder;
}

String? getAndroidV21LaunchDarkBackgroundFile(int minSdk) {
return minSdk < 21
? androidV21LaunchDarkBackgroundFile
: androidLaunchDarkBackgroundFile;
}

String get androidManifestFile {
return 'android/app/src/main/AndroidManifest.xml';
}
Expand Down