setupSplashScreen function
Setting up the splash screen using the details provided in pubspec.yaml file under splash_master.
Implementation
Future<void> setupSplashScreen(YamlMap splashData) async {
final splashKeys = splashData.keys.map((e) => e.toString()).toSet();
final unsupportedTopLevelKeys = splashKeys
.where((key) => !YamlKeys.supportedYamlKeys.contains(key))
.toList();
final hasAndroid12Section =
splashKeys.contains(YamlKeys.android12AndAboveKey);
/// Checking keys in the `splash_master` section in `pubspec.yaml` file is proper or not.
if (unsupportedTopLevelKeys.isNotEmpty) {
log(
'Unsupported key(s) in splash_master: '
'${unsupportedTopLevelKeys.join(', ')}. '
'Supported keys are: ${YamlKeys.supportedYamlKeys.join(', ')}',
);
return;
}
final android12AndAboveRaw = splashData[YamlKeys.android12AndAboveKey];
final android12AndAbove = hasAndroid12Section && android12AndAboveRaw == null
? (loadYaml('{}') as YamlMap)
: android12AndAboveRaw;
if (android12AndAbove != null && android12AndAbove is! YamlMap) {
log('Please check the android_12_and_above configuration. All parameters must be nested under the android_12_and_above key.');
return;
}
final iosContentMode =
_tryParseIosContentMode(splashData[YamlKeys.iosContentModeKey]);
final iosBackgroundContentMode = _tryParseIosContentMode(
splashData[YamlKeys.iosBackgroundContentMode],
);
if (android12AndAbove != null) {
final android12Keys =
android12AndAbove.keys.map((e) => e.toString()).toSet();
final unsupportedAndroid12Keys = android12Keys
.where(
(key) => !YamlKeys.supportedAndroid12AndAboveYamlKeys.contains(key),
)
.toList();
if (unsupportedAndroid12Keys.isNotEmpty) {
log(
'Unsupported key(s) in android_12_and_above: '
'${unsupportedAndroid12Keys.join(', ')}. '
'Supported keys are: '
'${YamlKeys.supportedAndroid12AndAboveYamlKeys.join(', ')}',
);
return;
}
}
/// Checking if provided android gravity is valid or not
if (splashData[YamlKeys.androidGravityKey] != null &&
!(AndroidGravity.values.any(
(element) =>
element ==
AndroidGravity.fromString(splashData[YamlKeys.androidGravityKey]),
))) {
log('Please check the android_gravity');
return;
}
/// Checking if provided content mode is valid or not
if (splashData[YamlKeys.iosContentModeKey] != null &&
!IosContentMode.values.any(
(element) => element == iosContentMode,
)) {
log('Please check the ios_content_mode');
return;
}
/// Checking if provided background content mode is valid or not
if (splashData[YamlKeys.iosBackgroundContentMode] != null &&
!IosContentMode.values.any(
(element) => element == iosBackgroundContentMode,
)) {
log('Please check the ios_background_content_mode');
return;
}
if (!_validateImageExtensionIfProvided(
splashData[YamlKeys.imageKey],
keyName: YamlKeys.imageKey,
)) {
return;
}
if (!_validateImageExtensionIfProvided(
splashData[YamlKeys.imageDarkKey],
keyName: YamlKeys.imageDarkKey,
)) {
return;
}
if (!_validateImageExtensionIfProvided(
splashData[YamlKeys.backgroundImage],
keyName: YamlKeys.backgroundImage,
)) {
return;
}
if (!_validateImageExtensionIfProvided(
splashData[YamlKeys.backgroundImageDarkKey],
keyName: YamlKeys.backgroundImageDarkKey,
)) {
return;
}
if (android12AndAbove != null) {
if (!_validateImageExtensionIfProvided(
android12AndAbove[YamlKeys.imageKey],
keyName: '${YamlKeys.android12AndAboveKey}.${YamlKeys.imageKey}',
)) {
return;
}
if (!_validateImageExtensionIfProvided(
android12AndAbove[YamlKeys.imageDarkKey],
keyName: '${YamlKeys.android12AndAboveKey}.${YamlKeys.imageDarkKey}',
)) {
return;
}
if (!_validateImageExtensionIfProvided(
android12AndAbove[YamlKeys.brandingImageKey],
keyName: '${YamlKeys.android12AndAboveKey}.${YamlKeys.brandingImageKey}',
)) {
return;
}
if (!_validateImageExtensionIfProvided(
android12AndAbove[YamlKeys.brandingImageDarkKey],
keyName:
'${YamlKeys.android12AndAboveKey}.${YamlKeys.brandingImageDarkKey}',
)) {
return;
}
}
if (splashData[YamlKeys.androidBackgroundGravity] != null &&
!(AndroidGravity.values.any(
(element) =>
element ==
AndroidGravity.fromString(
splashData[YamlKeys.androidBackgroundGravity]),
))) {
log('Please check the android_background_image_gravity');
return;
}
try {
await applySplash(
imageSource: splashData[YamlKeys.imageKey],
color: splashData[YamlKeys.colorKey],
gravity: splashData[YamlKeys.androidGravityKey],
iosContentMode: iosContentMode?.mode,
android12AndAbove: android12AndAbove,
iosBackgroundContentMode: iosBackgroundContentMode?.mode,
backgroundImageSource: splashData[YamlKeys.backgroundImage],
backgroundImageGravity: splashData[YamlKeys.androidBackgroundGravity],
darkColor: splashData[YamlKeys.colorDarkKey],
darkGravity: splashData[YamlKeys.androidDarkGravityKey],
darkImage: splashData[YamlKeys.imageDarkKey],
darkBackgroundImageSource: splashData[YamlKeys.backgroundImageDarkKey],
);
} on SplashMasterException catch (e) {
log(e.message);
}
}