Skip to content

Commit

Permalink
Expanded options for Android 12. Thanks Jose for the PR that served a…
Browse files Browse the repository at this point in the history
…s a good starting point.
  • Loading branch information
jonbhanson committed Mar 6, 2022
1 parent 5d323f0 commit dcf9198
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 90 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## [2.1.0] - (2022-Mar-06)
* Expanded options for Android 12. Thanks [Jose](https://github.com/JCQuintas) for the [PR](https://github.com/jonbhanson/flutter_native_splash/pull/300) that served as a good starting point.
## [2.0.5] - (2022-Feb-20)
* Converted to a plugin since there is now platform specific code.
## [2.0.4] - (2022-Feb-15)
Expand Down
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ First, add `flutter_native_splash` as a dependency in your pubspec.yaml file.

```yaml
dependencies:
flutter_native_splash: ^2.0.5
flutter_native_splash: ^2.1.0
```

Don't forget to `flutter pub get`.
Expand Down Expand Up @@ -53,42 +53,52 @@ flutter_native_splash:
# png file and should be sized for 4x pixel density.
#image: assets/splash.png

# This property allows you to specify an image used as branding in the splash screen. It must be
# a png file. Currently, it is only supported for Android and iOS.
# The branding property allows you to specify an image used as branding in the splash screen.
# It must be a png file. Currently, it is only supported for Android < v12 and iOS.
#branding: assets/dart.png

# Specify your branding image for dark mode.
#branding_dark: assets/dart_dark.png

# To position the branding image at the bottom of the screen you can use bottom, bottomRight,
# and bottomLeft. The default values is bottom if not specified or specified something else.
#
# Make sure this content mode value should not be similar to android_gravity value and
# ios_content_mode value.
#branding_mode: bottom

# The color_dark, background_image_dark, and image_dark are parameters that set the background
# The color_dark, background_image_dark, image_dark, branding_dark are parameters that set the background
# and image when the device is in dark mode. If they are not specified, the app will use the
# parameters from above. If the image_dark parameter is specified, color_dark or
# background_image_dark must be specified. color_dark and background_image_dark cannot both be
# set.
#color_dark: "#042a49"
#background_image_dark: "assets/dark-background.png"
#image_dark: assets/splash-invert.png
#branding_dark: assets/dart_dark.png

# Android 12 handles the splash screen differently than previous versions. Please visit
# https://developer.android.com/guide/topics/ui/splash-screen
# Following are Android 12 specific parameter.
android_12:
# The image parameter sets the splash screen icon image. If this parameter is not specified,
# the app's launcher icon will be used instead.
# Please note that the splash screen will be clipped to a circle on the center of the screen.
# App icon with an icon background: This should be 960×960 pixels, and fit within a circle
# 640 pixels in diameter.
# App icon without an icon background: This should be 1152×1152 pixels, and fit within a circle
# 768 pixels in diameter.
#image: assets/android12splash.png

# App icon background color.
#icon_background_color: "#111111"

# The image_dark parameter and icon_background_color_dark set the image and icon background
# color when the device is in dark mode. If they are not specified, the app will use the
# parameters from above.
#image_dark: assets/android12splash-invert.png
#icon_background_color_dark: "#eeeeee"

# The android, ios and web parameters can be used to disable generating a splash screen on a given
# platform.
#android: false
#ios: false
#web: false

# The mode to use for android 12 splash screen. The default behavior is to use the app's icon as
# the splash image, but we can customize that so we use the same asset that was provided to the
# image/image_dark parameter.
# Please note that the splash screen will be clipped to a circle on the center of the screen.
# More info https://developer.android.com/guide/topics/ui/splash-screen#splash_screen_dimensions
#android_12_mode: 'use_image'

# The position of the splash image can be set with android_gravity, ios_content_mode, and
# web_image_mode parameters. All default to center.
#
Expand Down Expand Up @@ -165,9 +175,9 @@ If you find this package useful, you can support it for free by giving it a thum

# Android 12 Support

Android 12 has a [new method](https://developer.android.com/about/versions/12/features/splash-screen) of adding splash screens, which consists of a window background, icon, and the icon background. This package supports setting the background color and gives you the options to use the launcher icon or use the provided splash image with the `android_12_mode` configuration option.
Android 12 has a [new method](https://developer.android.com/about/versions/12/features/splash-screen) of adding splash screens, which consists of a window background, icon, and the icon background.

The package will add a `styles.xml` in `values-v31` and `values-night-v31` resource folders, which will provide Android 12 support while maintaining the legacy splash screen for previous versions of Android.
The package provides Android 12 support while maintaining the legacy splash screen for previous versions of Android.

***PLEASE NOTE:*** The splash screen may not appear when you launch the app from Android Studio. However, it should appear when you launch by clicking on the launch icon in Android.

Expand Down
34 changes: 25 additions & 9 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,44 @@ flutter_native_splash:
# png file and should be sized for 4x pixel density.
image: assets/logo_lockup_flutter_vertical.png

# This property allows you to specify an image used as branding in the splash screen. It must be
# a png file. Currently, it is only supported for Android and iOS.
# The branding property allows you to specify an image used as branding in the splash screen.
# It must be a png file. Currently, it is only supported for Android < v12 and iOS.
#branding: assets/dart.png

# Specify your branding image for dark mode.
#branding_dark: assets/dart_dark.png

# To position the branding image at the bottom of the screen you can use bottom, bottomRight,
# and bottomLeft. The default values is bottom if not specified or specified something else.
#
# Make sure this content mode value should not be similar to android_gravity value and
# ios_content_mode value.
#branding_mode: bottom

# The color_dark, background_image_dark, and image_dark are parameters that set the background
# The color_dark, background_image_dark, image_dark, branding_dark are parameters that set the background
# and image when the device is in dark mode. If they are not specified, the app will use the
# parameters from above. If the image_dark parameter is specified, color_dark or
# background_image_dark must be specified. color_dark and background_image_dark cannot both be
# set.
color_dark: "#042a49"
#background_image_dark: "assets/dark-background.png"
image_dark: assets/logo_lockup_flutter_vertical_wht.png
#branding_dark: assets/dart_dark.png

# Android 12 handles the splash screen differently than previous versions. Please visit
# https://developer.android.com/guide/topics/ui/splash-screen
# Following are Android 12 specific parameter.
android_12:
# The image parameter sets the splash screen icon image. If this parameter is not specified,
# the app's launcher icon will be used instead.
# App icon with an icon background: This should be 960×960 pixels, and fit within a circle
# 640 pixels in diameter.
# App icon without an icon background: This should be 1152×1152 pixels, and fit within a circle
# 768 pixels in diameter.
#image: assets/android12splash.png

# App icon background color.
#icon_background_color: "#eeeeee"

# The image_dark parameter and icon_background_color_dark set the image and icon background
# color when the device is in dark mode. If they are not specified, the app will use the
# parameters from above.
#image_dark: assets/android12splash-invert.png
#icon_background_color_dark: "#111111"

# The android, ios and web parameters can be used to disable generating a splash screen on a given
# platform.
Expand Down
109 changes: 72 additions & 37 deletions lib/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ final List<_AndroidDrawableTemplate> androidSplashImagesDark =
void _createAndroidSplash({
required String? imagePath,
required String? darkImagePath,
String? brandingImagePath,
String? brandingDarkImagePath,
required String? android12ImagePath,
required String? android12DarkImagePath,
required String? brandingImagePath,
required String? brandingDarkImagePath,
required String? color,
required String? darkColor,
required String gravity,
String brandingGravity = 'bottom',
required String brandingGravity,
required bool fullscreen,
required String? backgroundImage,
required String? darkBackgroundImage,
required String? android12Mode,
required String? android12IconBackgroundColor,
required String? darkAndroid12IconBackgroundColor,
}) {
if (imagePath != null) {
_applyImageAndroid(imagePath: imagePath);
Expand All @@ -67,6 +70,19 @@ void _createAndroidSplash({
);
}

//create android 12 image if provided. (otherwise uses launch icon)
if (android12ImagePath != null) {
_applyImageAndroid(
imagePath: android12ImagePath, fileName: 'android12splash.png');
}

if (android12DarkImagePath != null) {
_applyImageAndroid(
imagePath: android12DarkImagePath,
dark: true,
fileName: 'android12splash.png');
}

_createBackground(
colorString: color,
darkColorString: darkColor,
Expand Down Expand Up @@ -128,30 +144,36 @@ void _createAndroidSplash({

print('[Android] Updating styles...');
_applyStylesXml(
fullScreen: fullscreen,
file: _androidV31StylesFile,
template: _androidV31StylesXml,
android12BackgroundColor: color,
android12Mode: android12Mode);
fullScreen: fullscreen,
file: _androidV31StylesFile,
template: _androidV31StylesXml,
android12BackgroundColor: color,
android12ImagePath: android12ImagePath,
android12IconBackgroundColor: android12IconBackgroundColor,
);
if (darkColor != null) {
_applyStylesXml(
fullScreen: fullscreen,
file: _androidV31StylesNightFile,
template: _androidV31StylesNightXml,
android12BackgroundColor: darkColor,
android12Mode: android12Mode);
fullScreen: fullscreen,
file: _androidV31StylesNightFile,
template: _androidV31StylesNightXml,
android12BackgroundColor: darkColor,
android12ImagePath: android12DarkImagePath,
android12IconBackgroundColor: darkAndroid12IconBackgroundColor,
);
}

_applyStylesXml(
fullScreen: fullscreen,
file: _androidStylesFile,
template: _androidStylesXml);
fullScreen: fullscreen,
file: _androidStylesFile,
template: _androidStylesXml,
);

if (darkColor != null || darkBackgroundImage != null) {
_applyStylesXml(
fullScreen: fullscreen,
file: _androidNightStylesFile,
template: _androidStylesNightXml);
fullScreen: fullscreen,
file: _androidNightStylesFile,
template: _androidStylesNightXml,
);
}
}

Expand Down Expand Up @@ -242,12 +264,14 @@ void _applyLaunchBackgroundXml(
}

/// Create or update styles.xml full screen mode setting
void _applyStylesXml(
{required bool fullScreen,
required String file,
required String template,
String? android12BackgroundColor,
String? android12Mode}) {
void _applyStylesXml({
required bool fullScreen,
required String file,
required String template,
String? android12BackgroundColor,
String? android12ImagePath,
String? android12IconBackgroundColor,
}) {
final stylesFile = File(file);
print('[Android] - ' + file);
if (!stylesFile.existsSync()) {
Expand All @@ -258,18 +282,22 @@ void _applyStylesXml(
}

_updateStylesFile(
fullScreen: fullScreen,
stylesFile: stylesFile,
android12BackgroundColor: android12BackgroundColor,
android12Mode: android12Mode);
fullScreen: fullScreen,
stylesFile: stylesFile,
android12BackgroundColor: android12BackgroundColor,
android12ImagePath: android12ImagePath,
android12IconBackgroundColor: android12IconBackgroundColor,
);
}

/// Updates styles.xml adding full screen property
Future<void> _updateStylesFile(
{required bool fullScreen,
required File stylesFile,
required String? android12BackgroundColor,
required String? android12Mode}) async {
Future<void> _updateStylesFile({
required bool fullScreen,
required File stylesFile,
required String? android12BackgroundColor,
required String? android12ImagePath,
required String? android12IconBackgroundColor,
}) async {
final stylesDocument = XmlDocument.parse(stylesFile.readAsStringSync());
final resources = stylesDocument.getElement('resources');
final styles = resources?.findElements('style');
Expand Down Expand Up @@ -310,11 +338,18 @@ Future<void> _updateStylesFile(
value: '#' + android12BackgroundColor);
}

if (android12Mode == 'use_image') {
if (android12ImagePath != null) {
replaceElement(
launchTheme: launchTheme,
name: 'android:windowSplashScreenAnimatedIcon',
value: '@drawable/splash');
value: '@drawable/android12splash');
}

if (android12IconBackgroundColor != null) {
replaceElement(
launchTheme: launchTheme,
name: 'android:windowSplashScreenIconBackgroundColor',
value: '#' + android12IconBackgroundColor);
}

stylesFile.writeAsStringSync(
Expand Down

0 comments on commit dcf9198

Please sign in to comment.