Jesway / flutter_translate

Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.
MIT License
401 stars 118 forks source link

Screen flashes black while loading delegate #13

Closed SiyrisSoul closed 4 years ago

SiyrisSoul commented 4 years ago

The screen flashes black when loading the delegate. I've looked into this issue, but don't think it is related.

This can be replicated by running the example app and noticing the color flash between the splash image ending and the main app content loading.

Any idea on how to resolve this? I've been looking into it for a while now.

SiyrisSoul commented 4 years ago

Quick update on this. I just built the app I'm working on in release mode on my physical device and I'm not experiencing this issue! It definitely is noticeable in the emulator and not there without this package, but as far as I'm concerned since it works perfectly in release mode this issue might be able to be closed. I haven't tested the example app yet on my device, but I'm sure it works the same.

bratan commented 4 years ago

This is definitely an issue, I could reproduce it on the emulator and from what I can see there are some other open issues (such as https://github.com/flutter/flutter/issues/22007 and https://github.com/flutter/flutter/issues/20827) regarding this behavior.

In our case here, it seems the problem is the localization initialization by Flutter, which happens right after the splash screen is closed and before the main (app) widget is rendered.

If we force the localization initialization before runApp (by calling delegate.changeLocale), the issue is gone, as the localization is initialized during the splash screen instead.

Ex:

void main() async
{
  WidgetsFlutterBinding.ensureInitialized();

  var delegate = await LocalizationDelegate.create(fallbackLocale: 'en_US',supportedLocales: ['en_US', 'es', 'fa']);

  delegate.changeLocale(Locale('en_US'));

  runApp(LocalizedApp(delegate, MyApp()));
}

The problem with this is that on first startup, the current device locale should be used (if supported by the application).

So I think the solution here, in order to have the default locale used on startup and not to have this black screen behavior, is to retrieve the current device locale and initialize it before runApp, instead of letting Flutter decide when to initialize it.

This could be implemented directly in the LocalizationDelegate.create method.

SiyrisSoul commented 4 years ago

Thanks for the update on this! I'm going to look into implementing that if you haven't already gotten to it before I do.