Flutter App 中的状态恢复闪烁/闪烁屏幕

nic*_*er 5 themes flicker onresume dart flutter

我已经根据设备设置模式在我的应用程序中实现了深色和浅色主题。

我将应用程序置于后台,当我回来时,我在“恢复”上看到一些闪烁/闪烁。

这是我的应用程序中用于观察行为的随机页面:

在此输入图像描述

行为就像在恢复时首先重置浅色主题,然后记住这实际上是深色主题集。然后我认为主应用程序主题可以在状态恢复时重新加载,但我不太确定。

材料应用程序是否在恢复时重新加载?

这是我的MaterialApp

MaterialApp(
              navigatorKey: NavigationService.navigatorKey,
              theme: ThemeProvider.instance.getLightTheme(),
              darkTheme: ThemeProvider.instance.getDarkTheme(),
              themeMode: ThemeProvider.instance.getThemeMode(),
              locale: Locale(SharedPreferencesManager.instance.getLocale()),
              debugShowCheckedModeBanner: false,
              localizationsDelegates: const [
                LocalizationDelegate(),
                CountryLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: LocaleManager().getSupportedLocale(),
              home: WillPopScope(
                onWillPop: () async => false,
                child: const HomePage(),
              ));
Run Code Online (Sandbox Code Playgroud)

主题的主题提供者管理器方法:

ThemeMode getThemeMode() {
    if (SharedPreferencesManager.instance.getTheme() == "dark") {
      onDarkMode = true;
      return ThemeMode.dark;
    } else if (SharedPreferencesManager.instance.getTheme() == "light") {
      onDarkMode = false;
      return ThemeMode.light;
    } else {
      return ThemeMode.system;
    }
  }

  ThemeData getDarkTheme() {
    return ThemeData.dark().copyWith(
      scaffoldBackgroundColor: Colors.black,
      primaryColor: shadow,
      primaryColorLight: shadow2,
      shadowColor: Colors.transparent,
      splashColor: Colors.transparent,
      highlightColor: shadow,
      dialogBackgroundColor: shadow,
      brightness: Brightness.dark,
      dividerTheme: DividerThemeData(
        color: Colors.white.withOpacity(0.1),
        thickness: 0.5,
        space: 0,
      ),
      textTheme: const TextTheme(
          headline1: TextStyle(
              fontSize: 16, fontWeight: FontWeight.bold, color: highlight),
          headline2: TextStyle(
              fontSize: 14, fontWeight: FontWeight.bold, color: highlight),
          bodyText1: TextStyle(
            fontSize: 16,
            color: highlight,
          ),
          bodyText2: TextStyle(
            fontSize: 14,
            color: highlight,
          ),
          subtitle1: TextStyle(
              fontSize: 22, fontWeight: FontWeight.bold, color: highlight),
          subtitle2: TextStyle(
              fontSize: 18, fontWeight: FontWeight.bold, color: highlight)),
      // Android over scroll
      colorScheme:
          ColorScheme.fromSwatch().copyWith(secondary: Colors.transparent),
    );
  }

  ThemeData getLightTheme() {
    return ThemeData.light().copyWith(
        //scaffoldBackgroundColor: Colors.white,
        splashColor: Colors.transparent,
        primaryColor: Colors.white,
        primaryColorLight: light,
        shadowColor: elevationColor,
        brightness: Brightness.light,
        highlightColor: light,
        dialogBackgroundColor: Colors.white,
        dividerColor: light.withOpacity(0.5),
        dividerTheme: DividerThemeData(
            color: light.withOpacity(0.5), thickness: 0.5, space: 0),
        textTheme: const TextTheme(
            headline1: TextStyle(
                fontSize: 16, fontWeight: FontWeight.bold, color: shadow),
            headline2: TextStyle(
                fontSize: 14, fontWeight: FontWeight.bold, color: shadow),
            bodyText1: TextStyle(fontSize: 16, color: shadow),
            bodyText2: TextStyle(fontSize: 14, color: shadow),
            subtitle1: TextStyle(
                fontSize: 22, color: shadow, fontWeight: FontWeight.bold),
            subtitle2: TextStyle(
                fontSize: 18, color: shadow, fontWeight: FontWeight.bold)),
        colorScheme:
            ColorScheme.fromSwatch().copyWith(secondary: Colors.transparent));
  }
Run Code Online (Sandbox Code Playgroud)

此行为并非 100% 发生,但假设在恢复时 5 次出现 1 次,而从未在启动状态(首次启动)出现。

有人可以告诉我如果我在某些配置上得到这个结果是错误的吗?

这和主题有关吗?或者也许什么都没有

nic*_*er 0

这个问题似乎在 Flutter 3 版本中得到了修复