Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not an issue: No need for secondary splash screen #271

Closed
Ahmed-gubara opened this issue Jan 25, 2022 · 4 comments
Closed

Not an issue: No need for secondary splash screen #271

Ahmed-gubara opened this issue Jan 25, 2022 · 4 comments

Comments

@Ahmed-gubara
Copy link

Ahmed-gubara commented Jan 25, 2022

Thanks to this comment by @0x48Maciek

You can keep native splash screen as long as you want by the following code:

Future<void> main() async {
  final binding = WidgetsFlutterBinding.ensureInitialized();

  // Prevents app from closing splash screen, app layout will be build but not displayed.
  binding.deferFirstFrame();
  binding.addPostFrameCallback((_) async {
    BuildContext? context = binding.renderViewElement;
    if (context != null) {
      // Run any sync or awaited async function you want to wait for before showing app layout
      await Init.instance.initialize(context);
    }

    // Closes splash screen, and show the app layout.
    binding.allowFirstFrame();
  });

  runApp(...);
}

class Init {
  Init._();
  static final instance = Init._();
  Future initialize(BuildContext context) 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));
  }
}
@jonbhanson
Copy link
Owner

Thank you @Ahmed-gubara! I will add this to the documentation.

@justinenerio
Copy link

Thanks for this update! One thing, can you expose BuildContext in initializeFunction. It can be useful for precaching images.

@jonbhanson
Copy link
Owner

@justinenerio thanks for the suggestion. I made an update that exposes BuildContext as you requested. Please let me know if it works OK.

@justinenerio
Copy link

Looks great, works as intended. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants