diff --git a/CHANGELOG.md b/CHANGELOG.md index 95ec871..dc36b45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/README.md b/README.md index 3049286..d23a27f 100644 --- a/README.md +++ b/README.md @@ -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`. @@ -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. diff --git a/example/pubspec.yaml b/example/pubspec.yaml index c7bf87b..901f13d 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -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. diff --git a/lib/android.dart b/lib/android.dart index cf74176..28fa9af 100644 --- a/lib/android.dart +++ b/lib/android.dart @@ -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, @@ -60,6 +62,7 @@ void _createAndroidSplash({ required String? darkBackgroundImage, required String? android12IconBackgroundColor, required String? darkAndroid12IconBackgroundColor, + required String? screenOrientation, }) { if (imagePath != null) { _applyImageAndroid(imagePath: imagePath); @@ -164,7 +167,7 @@ void _createAndroidSplash({ fullScreen: fullscreen, file: _flavorHelper.androidV31StylesFile, template: _androidV31StylesXml, - android12BackgroundColor: color, + android12BackgroundColor: android12BackgroundColor, android12ImagePath: android12ImagePath, android12IconBackgroundColor: android12IconBackgroundColor, android12BrandingImagePath: brandingImagePath, @@ -175,7 +178,7 @@ void _createAndroidSplash({ fullScreen: fullscreen, file: _flavorHelper.androidV31StylesNightFile, template: _androidV31StylesNightXml, - android12BackgroundColor: darkColor, + android12BackgroundColor: android12DarkBackgroundColor, android12ImagePath: android12DarkImagePath, android12IconBackgroundColor: darkAndroid12IconBackgroundColor, android12BrandingImagePath: brandingDarkImagePath, @@ -195,6 +198,8 @@ void _createAndroidSplash({ template: _androidStylesNightXml, ); } + + _applyOrientation(orientation: screenOrientation); } /// Create splash screen as drawables for multiple screens (dpi) @@ -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; + }, + ), + ); +} diff --git a/lib/cli_commands.dart b/lib/cli_commands.dart index 2b98017..6123d1c 100644 --- a/lib/cli_commands.dart +++ b/lib/cli_commands.dart @@ -73,6 +73,8 @@ void createSplashByConfig(Map 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 = @@ -97,8 +99,8 @@ void createSplashByConfig(Map 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) { @@ -119,6 +121,9 @@ void createSplashByConfig(Map config) { gravity: gravity, brandingGravity: brandingGravity, fullscreen: fullscreen, + android12DarkBackgroundColor: android12DarkColor, + android12BackgroundColor: android12Color, + screenOrientation: androidScreenOrientation, ); } else { print('Android folder not found, skipping Android splash update...'); diff --git a/lib/flavor_helper.dart b/lib/flavor_helper.dart index 18141a2..ed3705a 100644 --- a/lib/flavor_helper.dart +++ b/lib/flavor_helper.dart @@ -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; diff --git a/pubspec.yaml b/pubspec.yaml index 465761c..c19821b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: