Skip to content

Commit

Permalink
Fixed remove command which was leaving splash images in place.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Hanson committed Feb 9, 2021
1 parent c10d697 commit 691bcbc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 59 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.11] - (2021-Feb-09)

* Fixed `remove` command which was leaving splash images in place.

## [0.2.10] - (2021-Feb-08)

* Replaced `android_fullscreen` with `fullscreen` parameter, adding iOS support. closes [#75](https://github.com/jonbhanson/flutter_native_splash/issues/75), closes [#65](https://github.com/jonbhanson/flutter_native_splash/issues/65).
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ First, add `flutter_native_splash` as a dev dependency in your pubspec.yaml file

```yaml
dev_dependencies:
flutter_native_splash: ^0.2.10
flutter_native_splash: ^0.2.11
```
Don't forget to `flutter pub get`.
Expand All @@ -37,7 +37,7 @@ flutter_native_splash:
# Optional parameters are listed below. To enable a parameter, uncomment the line by removing
# the leading # character.
# The image parameter allows you to specifiy an image used in the splash screen. It must be a
# The image parameter allows you to specify an image used in the splash screen. It must be a
# png file.
#image: assets/splash.png
Expand Down Expand Up @@ -93,7 +93,7 @@ When the package finishes running your splash screen is ready.

# Recommendations
## Secondary splash screen:
The native splash screen is displayed while the native app loads the Flutter framework. Once Flutter loads, there may still be resources that need to be loaded before your app is ready. For this reason, you should consider implimenting a Flutter splash screen that is displayed while these resources load. Here is a code example of a secondary Flutter splash screen, or use a package from [pub.dev](https://pub.dev).
The native splash screen is displayed while the native app loads the Flutter framework. Once Flutter loads, there may still be resources that need to be loaded before your app is ready. For this reason, you should consider implementing a Flutter splash screen that is displayed while these resources load. Here is a code example of a secondary Flutter splash screen, or use a package from [pub.dev](https://pub.dev).

```dart
class MyApp extends StatelessWidget {
Expand Down
76 changes: 40 additions & 36 deletions lib/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ void _createAndroidSplash(
}

await _applyLaunchBackgroundXml(
imagePath: imagePath,
gravity: gravity,
launchBackgroundFilePath: _androidLaunchBackgroundFile);
gravity: gravity,
launchBackgroundFilePath: _androidLaunchBackgroundFile,
showImage: imagePath.isNotEmpty,
);
await _applyColor(color: color, colorFile: _androidColorsFile);
await _overwriteLaunchBackgroundWithNewSplashColor(
color: color, launchBackgroundFilePath: _androidLaunchBackgroundFile);

if (darkColor.isNotEmpty) {
await _applyLaunchBackgroundXml(
imagePath: darkImagePath,
gravity: gravity,
launchBackgroundFilePath: _androidLaunchDarkBackgroundFile);
gravity: gravity,
launchBackgroundFilePath: _androidLaunchDarkBackgroundFile,
showImage: imagePath.isNotEmpty,
);
await _applyColor(color: darkColor, colorFile: _androidColorsDarkFile);
await _overwriteLaunchBackgroundWithNewSplashColor(
color: color,
Expand All @@ -62,17 +64,19 @@ void _createAndroidSplash(

if (await Directory(_androidV21DrawableFolder).exists()) {
await _applyLaunchBackgroundXml(
imagePath: imagePath,
gravity: gravity,
launchBackgroundFilePath: _androidV21LaunchBackgroundFile);
gravity: gravity,
launchBackgroundFilePath: _androidV21LaunchBackgroundFile,
showImage: imagePath.isNotEmpty,
);
await _overwriteLaunchBackgroundWithNewSplashColor(
color: color,
launchBackgroundFilePath: _androidV21LaunchBackgroundFile);
if (darkColor.isNotEmpty) {
await _applyLaunchBackgroundXml(
imagePath: darkImagePath,
gravity: gravity,
launchBackgroundFilePath: _androidV21LaunchDarkBackgroundFile);
gravity: gravity,
launchBackgroundFilePath: _androidV21LaunchDarkBackgroundFile,
showImage: imagePath.isNotEmpty,
);
await _overwriteLaunchBackgroundWithNewSplashColor(
color: color,
launchBackgroundFilePath: _androidV21LaunchDarkBackgroundFile);
Expand Down Expand Up @@ -118,17 +122,22 @@ void _saveImageAndroid({_AndroidDrawableTemplate template, Image image}) {
}

/// Create or update launch_background.xml adding splash image path
Future _applyLaunchBackgroundXml(
{String imagePath, String gravity, String launchBackgroundFilePath}) {
Future _applyLaunchBackgroundXml({
String gravity,
String launchBackgroundFilePath,
bool showImage,
}) {
final launchBackgroundFile = File(launchBackgroundFilePath);

if (launchBackgroundFile.existsSync()) {
if (imagePath.isNotEmpty) {
if (launchBackgroundFile.existsSync()) {
print('[Android] Updating ' +
launchBackgroundFilePath +
' with splash image path');
return _updateLaunchBackgroundFileWithImagePath(
launchBackgroundFilePath: launchBackgroundFilePath, gravity: gravity);
launchBackgroundFilePath: launchBackgroundFilePath,
gravity: gravity,
showImage: showImage);
}

return Future.value(false);
Expand All @@ -140,15 +149,15 @@ Future _applyLaunchBackgroundXml(
launchBackgroundFilePath +
' file and adding it to your Android project');
return _createLaunchBackgroundFileWithImagePath(
imagePath: imagePath,
gravity: gravity,
launchBackgroundFilePath: launchBackgroundFilePath);
launchBackgroundFilePath: launchBackgroundFilePath,
showImage: showImage);
}
}

/// Updates launch_background.xml adding splash image path
Future _updateLaunchBackgroundFileWithImagePath(
{String launchBackgroundFilePath, String gravity}) async {
{String launchBackgroundFilePath, String gravity, bool showImage}) async {
final launchBackgroundFile = File(launchBackgroundFilePath);
var launchBackgroundDocument;
if (launchBackgroundFile.existsSync()) {
Expand Down Expand Up @@ -178,32 +187,27 @@ Future _updateLaunchBackgroundFileWithImagePath(
});
removeNodes.forEach(items.remove);

var splashItem =
XmlDocument.parse(_androidLaunchBackgroundItemXml).rootElement.copy();
splashItem.getElement('bitmap').setAttribute('android:gravity', gravity);
items.add(splashItem);
if (showImage) {
var splashItem =
XmlDocument.parse(_androidLaunchBackgroundItemXml).rootElement.copy();
splashItem.getElement('bitmap').setAttribute('android:gravity', gravity);
items.add(splashItem);
}
launchBackgroundFile.writeAsStringSync(
launchBackgroundDocument.toXmlString(pretty: true, indent: ' '));
}

/// Creates launch_background.xml with splash image path
Future _createLaunchBackgroundFileWithImagePath(
{String imagePath, String gravity, String launchBackgroundFilePath}) async {
{String gravity, String launchBackgroundFilePath, bool showImage}) async {
var file = await File(launchBackgroundFilePath).create(recursive: true);
var fileContent = XmlDocument.parse(_androidLaunchBackgroundXml);

if (imagePath.isEmpty) {
fileContent.getElement('layer-list').children.removeLast();
} else {
var layerlist = fileContent.getElement('layer-list');

layerlist.children.whereType<XmlElement>().forEach((element) {
var bitmap = element.getElement('bitmap');
if (bitmap != null) {
bitmap.setAttribute('android:gravity', gravity);
return true;
}
});
if (showImage) {
var splashItem =
XmlDocument.parse(_androidLaunchBackgroundItemXml).rootElement.copy();
splashItem.getElement('bitmap').setAttribute('android:gravity', gravity);
fileContent.getElement('layer-list').children.add(splashItem);
}
return await file
.writeAsString(fileContent.toXmlString(pretty: true, indent: ' '));
Expand Down
49 changes: 32 additions & 17 deletions lib/ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,50 @@ void _createiOSSplash({
}) async {
if (imagePath.isNotEmpty) {
await _applyImageiOS(imagePath: imagePath);
} else {
final splashImage = Image(1, 1);
_iOSSplashImages.forEach((template) async {
await File(_iOSAssetsLaunchImageFolder + template.fileName)
.create(recursive: true)
.then((File file) {
file.writeAsBytesSync(encodePng(splashImage));
});
});
}

if (darkImagePath.isNotEmpty) {
await _applyImageiOS(imagePath: darkImagePath, dark: true);
} else {
_iOSSplashImagesDark.forEach((template) {
final file = File(_iOSAssetsLaunchImageFolder + template.fileName);
if (file.existsSync()) file.deleteSync();
});
}

await File(_iOSAssetsLaunchImageFolder + 'Contents.json')
.create(recursive: true)
.then((File file) {
file.writeAsStringSync(
darkImagePath.isNotEmpty ? _iOSContentsJsonDark : _iOSContentsJson);
});

await _applyLaunchScreenStoryboard(
imagePath: imagePath, iosContentMode: iosContentMode);
await _createBackgroundColor(
colorString: color,
darkColorString: darkColor,
dark: darkColor.isNotEmpty);
await _createBackgroundColor(colorString: color, darkColorString: darkColor);
await _applyInfoPList(plistFiles: plistFiles, fullscreen: fullscreen);
}

/// Create splash screen images for original size, @2x and @3x
void _applyImageiOS({String imagePath, bool dark = false}) {
print('[iOS] Creating ' + (dark ? 'dark mode ' : '') + 'splash images');

final file = File(imagePath);

if (!file.existsSync()) {
if (!File(imagePath).existsSync()) {
throw _NoImageFileFoundException('The file $imagePath was not found.');
}

final image = decodeImage(File(imagePath).readAsBytesSync());

for (var template in dark ? _iOSSplashImagesDark : _iOSSplashImages) {
_saveImageiOS(template: template, image: image);
}
File(_iOSAssetsLaunchImageFolder + 'Contents.json')
.create(recursive: true)
.then((File file) {
file.writeAsStringSync(dark ? _iOSContentsJsonDark : _iOSContentsJson);
});
}

/// Saves splash screen image to the project
Expand Down Expand Up @@ -195,7 +205,7 @@ Future _createLaunchScreenStoryboard(
}

Future<void> _createBackgroundColor(
{String colorString, String darkColorString, bool dark}) async {
{String colorString, String darkColorString}) async {
var background = Image(1, 1);
var redChannel = int.parse(colorString.substring(0, 2), radix: 16);
var greenChannel = int.parse(colorString.substring(2, 4), radix: 16);
Expand All @@ -215,13 +225,18 @@ Future<void> _createBackgroundColor(
await File(_iOSAssetsLaunchImageBackgroundFolder + 'darkbackground.png')
.create(recursive: true)
.then((File file) => file.writeAsBytesSync(encodePng(background)));
} else {
final file =
File(_iOSAssetsLaunchImageBackgroundFolder + 'darkbackground.png');
if (file.existsSync()) file.deleteSync();
}

return File(_iOSAssetsLaunchImageBackgroundFolder + 'Contents.json')
.create(recursive: true)
.then((File file) {
file.writeAsStringSync(
dark ? _iOSLaunchBackgroundDarkJson : _iOSLaunchBackgroundJson);
file.writeAsStringSync(darkColorString.isNotEmpty
? _iOSLaunchBackgroundDarkJson
: _iOSLaunchBackgroundJson);
});
}

Expand Down
2 changes: 0 additions & 2 deletions lib/templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const String _androidLaunchBackgroundXml = '''
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/splash_color" />
$_androidLaunchBackgroundItemXml
</layer-list>
''';

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: Generates native code to customize Flutter's default white native splash screen with background color and splash image. Supports dark mode, full screen, and more.
version: 0.2.10
version: 0.2.11
homepage: https://github.com/jonbhanson/flutter_native_splash

environment:
Expand Down

0 comments on commit 691bcbc

Please sign in to comment.