Skip to content

Commit

Permalink
Corrected Android 12 color overriding color parameter. Closes #365. A…
Browse files Browse the repository at this point in the history
…dd support for setting screen orientation in Android. Closes #344.
  • Loading branch information
jonbhanson committed May 29, 2022
1 parent 4f36d95 commit 4b0b56f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [2.2.2] - (2022-May-29)
* Corrected Android 12 color overriding color parameter. Closes [#365](https://github.com/jonbhanson/flutter_native_splash/issues/365).
* Add support for setting screen orientation in Android. Closes [#344](https://github.com/jonbhanson/flutter_native_splash/issues/344).

## [2.2.1] - (2022-May-22)
* Updated dependencies. Closes [#358](https://github.com/jonbhanson/flutter_native_splash/issues/358).
* Added Android 12 background color support. Closes [#357](https://github.com/jonbhanson/flutter_native_splash/issues/357).
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ First, add `flutter_native_splash` as a dependency in your pubspec.yaml file.

```yaml
dependencies:
flutter_native_splash: ^2.2.1
flutter_native_splash: ^2.2.2
```

Don't forget to `flutter pub get`.
Expand Down Expand Up @@ -119,6 +119,11 @@ flutter_native_splash:
# web_image_mode can be one of the following modes: center, contain, stretch, and cover.
#web_image_mode: center

# The screen orientation can be set in Android with the android_screen_orientation parameter.
# Valid parameters can be found here:
# https://developer.android.com/guide/topics/manifest/activity-element#screen
#android_screen_orientation: sensorLandscape

# To hide the notification bar, use the fullscreen parameter. Has no effect in web since web
# has no notification bar. Defaults to false.
# NOTE: Unlike Android, iOS will not automatically show the notification bar when the app loads.
Expand Down
5 changes: 5 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ flutter_native_splash:
# web_image_mode can be one of the following modes: center, contain, stretch, and cover.
#web_image_mode: center

# The screen orientation can be set in Android with the android_screen_orientation parameter.
# Valid parameters can be found here:
# https://developer.android.com/guide/topics/manifest/activity-element#screen
#android_screen_orientation: sensorLandscape

# To hide the notification bar, use the fullscreen parameter. Has no affect in web since web
# has no notification bar. Defaults to false.
# NOTE: Unlike Android, iOS will not automatically show the notification bar when the app loads.
Expand Down
44 changes: 42 additions & 2 deletions lib/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ void _createAndroidSplash({
required String? darkImagePath,
required String? android12ImagePath,
required String? android12DarkImagePath,
required String? android12BackgroundColor,
required String? android12DarkBackgroundColor,
required String? brandingImagePath,
required String? brandingDarkImagePath,
required String? color,
Expand All @@ -60,6 +62,7 @@ void _createAndroidSplash({
required String? darkBackgroundImage,
required String? android12IconBackgroundColor,
required String? darkAndroid12IconBackgroundColor,
required String? screenOrientation,
}) {
if (imagePath != null) {
_applyImageAndroid(imagePath: imagePath);
Expand Down Expand Up @@ -164,7 +167,7 @@ void _createAndroidSplash({
fullScreen: fullscreen,
file: _flavorHelper.androidV31StylesFile,
template: _androidV31StylesXml,
android12BackgroundColor: color,
android12BackgroundColor: android12BackgroundColor,
android12ImagePath: android12ImagePath,
android12IconBackgroundColor: android12IconBackgroundColor,
android12BrandingImagePath: brandingImagePath,
Expand All @@ -175,7 +178,7 @@ void _createAndroidSplash({
fullScreen: fullscreen,
file: _flavorHelper.androidV31StylesNightFile,
template: _androidV31StylesNightXml,
android12BackgroundColor: darkColor,
android12BackgroundColor: android12DarkBackgroundColor,
android12ImagePath: android12DarkImagePath,
android12IconBackgroundColor: darkAndroid12IconBackgroundColor,
android12BrandingImagePath: brandingDarkImagePath,
Expand All @@ -195,6 +198,8 @@ void _createAndroidSplash({
template: _androidStylesNightXml,
);
}

_applyOrientation(orientation: screenOrientation);
}

/// Create splash screen as drawables for multiple screens (dpi)
Expand Down Expand Up @@ -451,3 +456,38 @@ void removeElement({required XmlElement launchTheme, required String name}) {
),
);
}

void _applyOrientation({required String? orientation}) {
final manifestFile = File(_flavorHelper.androidManifestFile);
final manifestDocument = XmlDocument.parse(manifestFile.readAsStringSync());

final manifestRoot = manifestDocument.getElement('manifest');
final application = manifestRoot?.getElement('application');
final activity = application?.getElement('activity');
const String attribute = 'android:screenOrientation';
if (orientation == null) {
if (activity?.attributes.any((p0) => p0.name.toString() == attribute) ??
false) {
activity?.removeAttribute(attribute);
} else {
return;
}
} else {
activity?.setAttribute(attribute, orientation);
}
manifestFile.writeAsStringSync(
manifestDocument.toXmlString(
pretty: true,
indent: ' ',
indentAttribute: (XmlAttribute xmlAttribute) {
// Try to preserve AndroidManifest.XML formatting:
if (xmlAttribute.name.toString() == "xmlns:android") return false;
if (xmlAttribute.value == "android.intent.action.MAIN") return false;
if (xmlAttribute.value == "android.intent.category.LAUNCHER") {
return false;
}
return true;
},
),
);
}
9 changes: 7 additions & 2 deletions lib/cli_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ void createSplashByConfig(Map<String, dynamic> config) {
if (config['android_gravity'] != null) {
gravity = config['android_gravity'] as String;
}
final String? androidScreenOrientation =
config['android_screen_orientation'] as String?;
final brandingGravity = config['branding_mode'] as String? ?? 'bottom';
final bool fullscreen = config['fullscreen'] as bool? ?? false;
final String iosContentMode =
Expand All @@ -97,8 +99,8 @@ void createSplashByConfig(Map<String, dynamic> config) {
darkAndroid12IconBackgroundColor =
parseColor(android12Config['icon_background_color_dark']) ??
android12IconBackgroundColor;
android12Color = parseColor(android12Config['color']);
android12DarkColor = parseColor(android12Config['dark_color']);
android12Color = parseColor(android12Config['color']) ?? color;
android12DarkColor = parseColor(android12Config['dark_color']) ?? darkColor;
}

if (!config.containsKey('android') || config['android'] as bool) {
Expand All @@ -119,6 +121,9 @@ void createSplashByConfig(Map<String, dynamic> config) {
gravity: gravity,
brandingGravity: brandingGravity,
fullscreen: fullscreen,
android12DarkBackgroundColor: android12DarkColor,
android12BackgroundColor: android12Color,
screenOrientation: androidScreenOrientation,
);
} else {
print('Android folder not found, skipping Android splash update...');
Expand Down
4 changes: 4 additions & 0 deletions lib/flavor_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class _FlavorHelper {
return '${androidNightV21DrawableFolder}launch_background.xml';
}

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

// iOS related values
late String? _iOSFlavorName;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_native_splash
description: Customize Flutter's default white native splash screen with
background color and splash image. Supports dark mode, full screen, and more.
version: 2.2.1
version: 2.2.2
homepage: https://github.com/jonbhanson/flutter_native_splash

environment:
Expand Down

0 comments on commit 4b0b56f

Please sign in to comment.