Skip to content

Commit

Permalink
BREAKING CHANGE: Added removeAfter method which allows the native spl…
Browse files Browse the repository at this point in the history
…ash to remain until an initialization routine is complete. In order to use this method, the flutter_native_splash dependency must be moved from dev_dependencies to dependencies. Thanks Ahmed for the tip. Closes #271.
  • Loading branch information
jonbhanson committed Jan 29, 2022
1 parent 5fcab74 commit c1bdae0
Show file tree
Hide file tree
Showing 25 changed files with 349 additions and 376 deletions.
50 changes: 2 additions & 48 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,9 @@
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/
pubspec.lock

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
2 changes: 1 addition & 1 deletion .metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file should be version controlled and should not be manually edited.

version:
revision: 20e59316b8b8474554b38493b8ca888794b0234a
revision: 77d935af4db863f6abd0b9c31c7e6df2a13de57b
channel: stable

project_type: package
7 changes: 7 additions & 0 deletions .run/create.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="create" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application">
<option name="filePath" value="$PROJECT_DIR$/bin/create.dart" />
<option name="workingDirectory" value="$PROJECT_DIR$/example" />
<method v="2" />
</configuration>
</component>
7 changes: 7 additions & 0 deletions .run/remove.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="remove" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application">
<option name="filePath" value="$PROJECT_DIR$/bin/remove.dart" />
<option name="workingDirectory" value="$PROJECT_DIR$/example" />
<method v="2" />
</configuration>
</component>
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [2.0.0] - (2022-Jan-29)
* BREAKING CHANGE: Added `removeAfter` method which allows the native splash to remain until an initialization routine is complete. In order to use this method, the `flutter_native_splash` dependency must be moved from `dev_dependencies` to `dependencies`. Thanks [Ahmed](https://github.com/Ahmed-gubara) for the tip. Closes [#271](https://github.com/jonbhanson/flutter_native_splash/issues/271).

## [1.3.3] - (2022-Jan-01)
* Merged PR that adds branding. Thanks [Faiizii](https://github.com/Faiizii) for PR. Closes [#256](https://github.com/jonbhanson/flutter_native_splash/issues/256).
* Updated readme. Closes [#258](https://github.com/jonbhanson/flutter_native_splash/issues/258).
Expand Down
51 changes: 29 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ When your app is opened, there is a brief time while the native app loads Flutte
<img src="https://raw.githubusercontent.com/jonbhanson/flutter_native_splash/master/splash_demo_dark.gif" />
</p>

# What's New

You can now keep the splash screen up while your app initializes! No need for a secondary splash screen anymore. Just use the `removeAfter` method to remove the splash screen after your initialization is complete. See [example below](https://pub.dev/packages/flutter_native_splash#3-set-up-app-initialization-optional).

# Usage

Would you prefer a video tutorial instead? Check out <a href="https://www.youtube.com/watch?v=8ME8Czqc-Oc">Johannes Milke's tutorial</a>.

First, add `flutter_native_splash` as a dev dependency in your pubspec.yaml file. It belongs in `dev_dependencies` because it is a command line tool. Please disregard the instructions in the *Installing* tab that gives different instructions.
First, add `flutter_native_splash` as a dependency in your pubspec.yaml file.

```yaml
dev_dependencies:
flutter_native_splash: ^1.3.3
dependencies:
flutter_native_splash: ^2.0.0
```
Don't forget to `flutter pub get`.
Expand Down Expand Up @@ -59,8 +63,8 @@ flutter_native_splash:
# To position the branding image at the bottom of the screen you can use bottom, bottomRight,
# and bottomLeft. The default values is bottom if not specified or specified something else.
#
# Make sure this content mode value should not be similar to android_gravity value and ios_content_mode
# value.
# Make sure this content mode value should not be similar to android_gravity value and
# ios_content_mode value.
branding_mode: bottom
# The color_dark, background_image_dark, and image_dark are parameters that set the background
Expand Down Expand Up @@ -127,17 +131,31 @@ To specify the YAML file location just add --path with the command in the termin
flutter pub run flutter_native_splash:create --path=path/to/my/file.yaml
```

## 3. Set up app initialization (optional)

By default, the splash screen will be removed when Flutter has drawn the first frame. If you would like the splash screen to remain while your initializes, you can use the `removeAfter` method in the following manner:

```dart
void main() {
FlutterNativeSplash.removeAfter(initialization);
// runApp will run, but not be shown until initialization completes:
runApp(const MyApp());
}
void initialization() async {
// This is where you can initialize the resources needed by your app while
// the splash screen is displayed. After this function completes, the
// splash screen will be removed.
}
```

# Android 12 Support

Android 12 has a [new method](https://developer.android.com/about/versions/12/features/splash-screen) of adding splash screens, which consists of a window background, icon, and the icon background. Currently, this package supports setting the background color and the icon is taken from the launcher icon.

If you [enable Android 12 support](https://developer.android.com/about/versions/12/setup-sdk), the package will add a `styles.xml` in `values-v31` and `values-night-v31` resource folders, which will provide Android 12 support while maintaining the legacy splash screen for previous versions of Android.

NOTE: The splash screen may not appear when you launch the app from Android Studio. However, it should appear when you launch by clicking on the launch icon in Android.

# Recommendations
## Secondary splash screen:
The native splash screen is displayed while the native app loads the Flutter framework. Once Flutter loads, there are probably still resources that need to be loaded before your app is ready. For this reason, you should consider implementing a secondary Flutter splash screen or placeholders that display while these resources load. The [example](https://github.com/jonbhanson/flutter_native_splash/blob/master/example/lib/main.dart) shows an implementation of a secondary splash screen.

# FAQs
## I got the error "A splash screen was provided to Flutter, but this is deprecated."
Expand All @@ -152,11 +170,8 @@ This message is not related to this package but is related to a [change](https:/
```
The solution is to remove the above code. Note that this will also remove the fade effect between the native splash screen and your app.

## Can I change the duration of the splash screen?
No, it is not possible to change the duration of the splash screen. The native splash screen is displayed while the native app loads the Flutter framework. Because the resources in your app cannot load while the native splash screen is displayed, the native splash screen must be as fast as possible. Note that delaying the user experience is a poor design decision.

## Are animations/lottie/GIF images supported?
Not at this time. However, you may want to consider a secondary splash screen that supports animation. See the [secondary splash screen](#secondary-splash-screen) recommendation.
Not at this time. PRs are always welcome!

## I got the error AAPT: error: style attribute 'android:attr/windowSplashScreenBackground' not found
This attribute is only found in Android 12, so if you are getting this error, it means your project is not fully set up for Android 12. Did you [update your app's build configuration](https://developer.android.com/about/versions/12/setup-sdk#config)?
Expand All @@ -166,7 +181,7 @@ This is caused by an [iOS splash caching bug](https://stackoverflow.com/question

## I see a white screen between splash screen and app
1. It may be caused by an [iOS splash caching bug](https://stackoverflow.com/questions/33002829/ios-keeping-old-launch-screen-and-app-icon-after-update), which can be solved by uninstalling your app, powering off your device, power back on, and then try reinstalling.
2. It may be caused by the delay due to initialization in your app. To test this, make a test where your `main()` returns a `Container(color: Colors.black);`. If the white flash goes away, it will show that your content is not loading fast enough. You could solve this by creating a [secondary splash screen](https://pub.dev/packages/flutter_native_splash#secondary-splash-screen) or loading a barebones version of your app with placeholders, and then populate the placeholders as content loads.
2. It may be caused by the delay due to initialization in your app. To solve this, put any initialization code in the `removeAfter` method.

# Notes
* If the splash screen was not updated correctly on iOS or if you experience a white screen before the splash screen, run `flutter clean` and recompile your app. If that does not solve the problem, delete your app, power down the device, power up the device, install and launch the app as per [this StackOverflow thread](https://stackoverflow.com/questions/33002829/ios-keeping-old-launch-screen-and-app-icon-after-update).
Expand All @@ -192,14 +207,6 @@ This is caused by an [iOS splash caching bug](https://stackoverflow.com/question
* Your splash image will be resized to `1x`, `2x`, `3x`, and `4x` sizes and placed in `web/splash/img`.
* The splash style sheet will be added to the app's `web/index.html`, as well as the HTML for the splash pictures.

# Removing

If you would like to restore Flutter's default white splash screen, run the following command in the terminal:

```
flutter pub run flutter_native_splash:remove
```

# Acknowledgments

This package was originally created by [Henrique Arthur](https://github.com/henriquearthur) and it is currently maintained by [Jon Hanson](https://github.com/jonbhanson).
Expand Down
6 changes: 2 additions & 4 deletions bin/create.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:args/args.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart'
as flutter_native_splash;
import 'package:flutter_native_splash/cli_commands.dart';

void main(List<String> args) {
var parser = ArgParser();
parser.addOption('path',
callback: (path) => {flutter_native_splash.createSplash(path: path)});
parser.addOption('path', callback: (path) => {createSplash(path: path)});
parser.parse(args);
}
6 changes: 2 additions & 4 deletions bin/remove.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:args/args.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart'
as flutter_native_splash;
import 'package:flutter_native_splash/cli_commands.dart';

void main(List<String> args) {
var parser = ArgParser();
parser.addOption('path',
callback: (path) => {flutter_native_splash.removeSplash(path: path)});
parser.addOption('path', callback: (path) => {removeSplash(path: path)});
parser.parse(args);
}
Binary file removed example/assets/1.5x/splash.png
Binary file not shown.
Binary file removed example/assets/1.5x/splash_dark.png
Binary file not shown.
Binary file removed example/assets/2.0x/splash.png
Binary file not shown.
Binary file removed example/assets/2.0x/splash_dark.png
Binary file not shown.
Binary file removed example/assets/3.0x/splash.png
Binary file not shown.
Binary file removed example/assets/3.0x/splash_dark.png
Binary file not shown.
Binary file removed example/assets/4.0x/splash.png
Binary file not shown.
Binary file removed example/assets/4.0x/splash_dark.png
Binary file not shown.
Binary file removed example/assets/splash.png
Binary file not shown.
Binary file removed example/assets/splash_dark.png
Binary file not shown.
91 changes: 31 additions & 60 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top]);
FlutterNativeSplash.removeAfter(initialization);
runApp(const MyApp());
}

void initialization() async {
// This is where you can initialize the resources needed by your app while
// the splash screen is displayed. Remove the following example because
// delaying the user experience is a bad design practice!
// ignore_for_file: avoid_print
print('ready in 3...');
await Future.delayed(const Duration(seconds: 1));
print('ready in 2...');
await Future.delayed(const Duration(seconds: 1));
print('ready in 1...');
await Future.delayed(const Duration(seconds: 1));
print('go!');
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Init.instance.initialize(),
builder: (context, AsyncSnapshot snapshot) {
// Show splash screen while waiting for app resources to load:
if (snapshot.connectionState == ConnectionState.waiting) {
return const MaterialApp(home: Splash());
} else {
// Loading is done, return the app:
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
},
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
Expand Down Expand Up @@ -128,33 +129,3 @@ class _MyHomePageState extends State<MyHomePage> {
);
}
}

class Splash extends StatelessWidget {
const Splash({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
bool lightMode =
MediaQuery.of(context).platformBrightness == Brightness.light;
return Scaffold(
backgroundColor:
lightMode ? const Color(0xffe1f5fe) : const Color(0xff042a49),
body: Center(
child: lightMode
? Image.asset('assets/splash.png')
: Image.asset('assets/splash_dark.png')),
);
}
}

class Init {
Init._();
static final instance = Init._();

Future initialize() async {
// This is where you can initialize the resources needed by your app while
// the splash screen is displayed. Remove the following example because
// delaying the user experience is a bad design practice!
await Future.delayed(const Duration(seconds: 3));
}
}
9 changes: 5 additions & 4 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.8"
version: "3.1.9"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -91,12 +91,12 @@ packages:
source: hosted
version: "1.0.4"
flutter_native_splash:
dependency: "direct dev"
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "1.3.3"
version: "2.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -108,7 +108,7 @@ packages:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
version: "3.1.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -228,3 +228,4 @@ packages:
version: "3.1.0"
sdks:
dart: ">=2.15.1 <3.0.0"
flutter: ">=1.17.0"
Loading

0 comments on commit c1bdae0

Please sign in to comment.