BirjuVachhani / adaptive_theme

Easiest way to add support for light and dark theme in your flutter app.
https://pub.dev/packages/adaptive_theme
Apache License 2.0
464 stars 37 forks source link

App theme doesn't update if app is started while the system was in dark mode #32

Closed Sandai64 closed 2 years ago

Sandai64 commented 2 years ago

Describe the bug App theme doesn't update if the app is started while the system was in dark mode. I have only tested this on an Android 11 phone, the Oneplus Nord.

The code

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

  @override
  Widget build(BuildContext context)
  {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);

    // Auto-reset immersive mode on UI change after 1 sec
    SystemChrome.setSystemUIChangeCallback
    (
      (systemOverlaysAreVisible) => Future.delayed
      (
        const Duration(seconds: 2),
        ()
        {
          SystemChrome.restoreSystemUIOverlays();
        }
      )
    );

    return AdaptiveTheme
    (
      light: AppTheme.lightTheme, // Custom Material theme overrides
      dark: AppTheme.darkTheme,
      initial: AdaptiveThemeMode.system,
      builder: (theme, darkTheme)
      {
        return MaterialApp
        (
          supportedLocales: const
          [
            Locale('en'),
            Locale('fr'), // Fallback lang
          ],

          localizationsDelegates:
          [
            FlutterI18nDelegate
              (
              translationLoader: FileTranslationLoader
                (
                  basePath: 'assets/i18n',
                  fallbackFile: 'fr'
              ),
              missingTranslationHandler: (key, locale)
              {
                if (kDebugMode)
                {
                  print("i18n --- Missing Key: $key, languageCode: ${locale?.languageCode}");
                }
              },
            ),
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate
          ],

          debugShowCheckedModeBanner: true,
          title: 'My App',
          initialRoute: '/',
          routes: AppRoutes.appRoutes,
          theme: theme,
          darkTheme: darkTheme,
        );
      }
    );
  }
}

Environment

Additional info

Stratovarius93 commented 2 years ago

I have the same error, any solution ?

gnassro commented 2 years ago

same problem

Arfmann21 commented 2 years ago

Same here, anyone got a fix?

BirjuVachhani commented 2 years ago

I have released a fix for system theme change not being detected. Please try v2.3.1 and if this issue is fixed.

gnassro commented 2 years ago

I have release a fix for system theme change not being detected. Please try v2.3.1 and if this issue is fixed.

same issue, app theme doesn't update if app is started while the system was in dark mode. only work when the app is started while system not in dark mode

BirjuVachhani commented 2 years ago

That's strange. I also do have Oneplus Nord CE with Android 11 and when I start my app while system is in dart mode, it works fine for me.

Checkout this part of the docs where the mode is retrieved from storage before loading the app. Maybe this can fix your issue but I am not sure.

To help me reproduce this, could you please create an example app with the same issue and provide exact steps to reproduce? (like does it happen on first run or second run?) At least post the exact code here.

gnassro commented 2 years ago

That's strange. I also do have Oneplus Nord CE with Android 11 and when I start my app while system is in dart mode, it works fine for me.

Checkout this part of the docs where the mode is retrieved from storage before loading the app. Maybe this can fix your issue but I am not sure.

To help me reproduce this, could you please create an example app with the same issue and provide exact steps to reproduce? (like does it happen on first run or second run?) At least post the exact code here.

yes you are right, i think there is something wrong in my project, because when i create new project and using the same ThemeData that i used in my project, it worked fine

BirjuVachhani commented 2 years ago

@gnassro I thought so. Please check your configuration and make sure you're not changing theme explicitly from somewhere.

BirjuVachhani commented 2 years ago

@erwangse Does the latest release work for you?

gnassro commented 2 years ago

@gnassro I thought so. Please check your configuration and make sure you're not changing theme explicitly from somewhere.

I discovered that the problem because of using this package https://pub.dev/packages/preload_page_view. when i replace it with the default Widget, it work fine

BirjuVachhani commented 2 years ago

@gnassro Ah! I think that package caches widgets so those widgets are not updated when the theme is changed. Although this is not a solution, can you try setting the preloadPagesCount to 0 (if allowed) just to check if that works as widgets shouldn't be cached in that case.

gnassro commented 2 years ago

@gnassro Ah! I think that package caches widgets so those widgets are not updated when the theme is changed. Although this is not a solution, can you try setting the preloadPagesCount to 0 (if allowed) just to check if that works as widgets shouldn't be cached in that case.

@BirjuVachhani i tried, but same issue, i think i need to change this package. thanks

BirjuVachhani commented 2 years ago

Closing this issue as it seems to be fixed. Feel free to reopen if it does work for some reason.

Sandai64 commented 2 years ago

Hi there, just wanted to make a quick follow-up and add a bit of context to this ! I'm the (currently) sole developer of a flutter app in my company, sorry I didn't respond in time. As for your first concern, I do cache theme changes in Hive & restore them on startup. And as for your second, the new release seems to work just fine ! Thanks ! :+1: