【问题标题】:Dynamic Splash Screen in FlutterFlutter 中的动态启动画面
【发布时间】:2020-11-02 05:42:17
【问题描述】:

我有不同版本的本机启动画面,我想在之前在应用中选择其主题时显示每个版本。例如,假设我有一个浅色闪屏和一个深色闪屏,并且默认情况下会显示浅色,但是如果用户在应用设置中将应用主题更改为深色主题,那么除了我要进行的更改在我的应用程序中执行此操作,下次用户打开应用程序时,我想向他显示初始屏幕的深色版本。

注意:我说的是原生启动画面

【问题讨论】:

  • 您可以有一个变量来保存您的主题是深色还是浅色的值,并使用它来决定显示正确的初始屏幕。您可以将数据(选定的主题)保存到共享首选项或数据库中,然后在 main 方法中获取。
  • @ShubhamGupta 原生启动画面会在主方法中的任何行运行之前显示

标签: android ios flutter dart splash-screen


【解决方案1】:

我想说共享偏好可以是实现这一目标的方法之一。
经过一番努力,我想出了下面的示例代码:
ma​​in.dart

SharedPreferences prefs;
String theme;

void main() async {
  prefs = await SharedPreferences.getInstance();
  // just for checking purpose
  if (prefs.getString("theme") == null) {
    await prefs.setString("theme", "darkTheme");
  }
  theme = (prefs.getString("theme") != null) ? "darkTheme" : "lightTheme";
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp(theme: theme));
}

class MyApp extends StatelessWidget {
  final theme;

  const MyApp({Key key, this.theme}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Brightness themeData =
        theme == "darkTheme" ? Brightness.dark : Brightness.light;
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(brightness: themeData),
      home: theme == "darkTheme"
          ? Scaffold(
              body: Center(
                child: Container(
                  child: Text("Dark Theme"),
                ),
              ),
            )
          : Scaffold(
              body: Center(
                child: Container(
                  child: Text("Light Theme"),
                ),
              ),
            ),
      debugShowCheckedModeBanner: false,
    );
  }
}

MainActivity.kt

import android.content.Context
import android.content.SharedPreferences
import io.flutter.embedding.android.FlutterActivity

class MainActivity : FlutterActivity() {
    var sharedPreferences: SharedPreferences =
            getSharedPreferences("FlutterSharedPreferences", 0)
    private val theme: String? = sharedPreferences.getString("flutter." + "theme", "lightTheme")
    override fun setTheme(resid: Int) {
        super.setTheme(resid)
        if (theme == "darkTheme") {
            application.setTheme(R.style.LaunchTheme)
        }
        if (theme == "lightTheme") {
            application.setTheme(R.style.NormalTheme)
        }
    }
}

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowEnableSplitTouch">false</item>
        <item name="android:splitMotionEvents">false</item>
        <item name="android:windowBackground">@android:color/black</item>
    </style>

    <style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowEnableSplitTouch">false</item>
        <item name="android:splitMotionEvents">false</item>
    </style>
</resources>

仅供参考:-

最终,Flutter 将值存储在原生共享偏好中。数据的检索格式不同。

Android:

var sharedPreferences: SharedPreferences =
            getSharedPreferences("FlutterSharedPreferences", 0)
    private val theme: String? = sharedPreferences.getString("flutter." + "theme", "lightTheme")

IOS:

if let name = NSUserDefaults.standard.string(forKey: "flutter.test") {
    print(name)
}

参考:-

ios, android

此代码对我有用。 :)

【讨论】:

  • 在启动应用程序之前存储和检索主题究竟如何使应用程序显示不同的原生闪屏?!
  • 在应用安装后的第一时间,你会看到默认的启动画面。之后,如果共享首选项为空,我们必须将其设置为某个值。当您第一次打开应用程序时(不是在第一次安装应用程序之后)MainActivity 从共享偏好中获取主题并相应地执行。最终,您将从 dart 设置共享首选项,并在下次原生启动画面时使用其值。
  • 是的,但您的代码中没有任何内容引用不同的启动屏幕文件,只是一个主题字符串
  • 谢谢@SanketVekariya 我试过你的方法,但它不起作用。 setTheme 不影响。你能分享更多信息吗?
  • 这个解决方案不起作用,因为 MainActivity.kt 在颤振的 main() 方法之前被调用。
猜你喜欢
  • 2020-04-16
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
相关资源
最近更新 更多