Skip to content

Commit

Permalink
Handle color parameter that are passed as integers. Fixes #103
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Hanson committed Jan 13, 2021
1 parent 4898182 commit 62db330
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.2.5] - (2021-Jan-13)

* Handle color parameter that are passed as integers. Fixes [#103](https://github.com/henriquearthur/flutter_native_splash/issues/103)

## [0.2.4] - (2021-Jan-12)

* Update code that adds fullscreen mode to Android so that it selects the right style (LaunchTheme) in styles.xml. This should resolve [#39](https://github.com/henriquearthur/flutter_native_splash/issues/39), [#54](https://github.com/henriquearthur/flutter_native_splash/issues/54), [#67](https://github.com/henriquearthur/flutter_native_splash/issues/67), [#92](https://github.com/henriquearthur/flutter_native_splash/issues/92), [#112](https://github.com/henriquearthur/flutter_native_splash/issues/112), and [#117](https://github.com/henriquearthur/flutter_native_splash/issues/117). Removed code that modifies MainActivity as it is not longer needed since Flutter embedding V2 uses two styles in styles.xml so full screen is set independently in the style.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ First, add `flutter_native_splash` as a [dev dependency in your pubspec.yaml fil

```yaml
dev_dependencies:
flutter_native_splash: ^0.2.4
flutter_native_splash: ^0.2.5
```
Don't forget to `flutter pub get`.
Expand Down
5 changes: 1 addition & 4 deletions lib/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ Future _createLaunchBackgroundFileWithImagePath(
void _applyColor(color, {bool dark = false}) {
final colorsXml = File(dark ? _androidColorsDarkFile : _androidColorsFile);

if (!color.contains('#')) {
color = '#' + color;
}

color = '#' + color;
if (colorsXml.existsSync()) {
print('[Android] Updating ' +
(dark ? 'dark mode ' : '') +
Expand Down
23 changes: 8 additions & 15 deletions lib/ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void _createiOSSplash(String imagePath, String darkImagePath, String color,
await _applyImageiOS(darkImagePath, dark: true);
}

await _applyLaunchScreenStoryboard(imagePath, color);
await _applyLaunchScreenStoryboard(imagePath);
await _createBackgroundColor(color, darkColor, darkColor.isNotEmpty);
await _applyInfoPList();
await _applyAppDelegate();
Expand Down Expand Up @@ -79,27 +79,22 @@ void _saveImageiOS(_IosLaunchImageTemplate template, Image image) {
}

/// Update LaunchScreen.storyboard adding width, height and color
Future _applyLaunchScreenStoryboard(String imagePath, String color) {
if (!color.contains('#')) {
color = '#' + color;
}

Future _applyLaunchScreenStoryboard(String imagePath) {
final file = File(_iOSLaunchScreenStoryboardFile);

if (file.existsSync()) {
print(
'[iOS] Updating LaunchScreen.storyboard with width, height and color');
return _updateLaunchScreenStoryboard(imagePath, color);
print('[iOS] Updating LaunchScreen.storyboard with width, and height');
return _updateLaunchScreenStoryboard(imagePath);
} else {
print('[iOS] No LaunchScreen.storyboard file found in your iOS project');
print(
'[iOS] Creating LaunchScreen.storyboard file and adding it to your iOS project');
return _createLaunchScreenStoryboard(imagePath, color);
return _createLaunchScreenStoryboard(imagePath);
}
}

/// Updates LaunchScreen.storyboard adding splash image path
Future _updateLaunchScreenStoryboard(String imagePath, String color) async {
Future _updateLaunchScreenStoryboard(String imagePath) async {
final file = File(_iOSLaunchScreenStoryboardFile);
final lines = await file.readAsLines();

Expand Down Expand Up @@ -189,17 +184,16 @@ Future _updateLaunchScreenStoryboard(String imagePath, String color) async {
}

/// Creates LaunchScreen.storyboard with splash image path
Future _createLaunchScreenStoryboard(String imagePath, String color) async {
Future _createLaunchScreenStoryboard(String imagePath) async {
var file = await File(_iOSLaunchScreenStoryboardFile).create(recursive: true);
await file.writeAsString(_iOSLaunchScreenStoryboardContent);

return _updateLaunchScreenStoryboard(imagePath, color);
return _updateLaunchScreenStoryboard(imagePath);
}

Future<void> _createBackgroundColor(
String colorString, String darkColorString, bool dark) async {
var background = Image(1, 1);
colorString = colorString.replaceFirst('#', '');
var redChannel = int.parse(colorString.substring(0, 2), radix: 16);
var greenChannel = int.parse(colorString.substring(2, 4), radix: 16);
var blueChannel = int.parse(colorString.substring(4, 6), radix: 16);
Expand All @@ -210,7 +204,6 @@ Future<void> _createBackgroundColor(
.then((File file) => file.writeAsBytesSync(encodePng(background)));

if (darkColorString.isNotEmpty) {
darkColorString = darkColorString.replaceFirst('#', '');
redChannel = int.parse(darkColorString.substring(0, 2), radix: 16);
greenChannel = int.parse(darkColorString.substring(2, 4), radix: 16);
blueChannel = int.parse(darkColorString.substring(4, 6), radix: 16);
Expand Down
16 changes: 14 additions & 2 deletions lib/supported_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Future<void> tryCreateSplash() async {
Future<void> tryCreateSplashByConfig(Map<String, dynamic> config) async {
String image = config['image'] ?? '';
String darkImage = config['image_dark'] ?? '';
var color = config['color'].toString();
var darkColor = config['color_dark']?.toString() ?? '';
var color = parseColor(config['color']);
var darkColor = parseColor(config['color_dark']) ?? '';
bool fill = config['fill'] ?? false;
bool androidDisableFullscreen = config['android_disable_fullscreen'] ?? false;

Expand All @@ -45,6 +45,18 @@ Future<void> tryCreateSplashByConfig(Map<String, dynamic> config) async {
}
}

String parseColor(var color) {
if (color is int) color = color.toString().padLeft(6, '0');

if (color is String) {
color = color.replaceAll('#', '').replaceAll(' ', '');
if (color.length == 6) return color;
}
if (color == null) return null;

throw Exception('Invalid color value');
}

/// Get config from `pubspec.yaml` or `flutter_native_splash.yaml`
Map<String, dynamic> _getConfig() {
// if `flutter_native_splash.yaml` exists use it as config file, otherwise use `pubspec.yaml`
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_native_splash
description: Automatically generates native code for adding splash screens in Android and iOS. Customize with specific platform, background color and splash image.
version: 0.2.4
version: 0.2.5
homepage: https://github.com/henriquearthur/flutter_native_splash

environment:
Expand Down

0 comments on commit 62db330

Please sign in to comment.