【问题标题】:Flutter different theme for Android and iOs为 Android 和 iOs Flutter 不同的主题
【发布时间】:2018-05-31 13:05:17
【问题描述】:

我想为我的 Flutter 应用使用不同的主题,具体取决于它启动的操作系统。在选择要应用的主题时如何检测操作系统?

Theme.of(context).platform == TargetPlatform.iOS

不起作用,因为我还没有应用主题...

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您可以通过将视图包装到具有自定义属性的新 Theme 实例中来轻松覆盖主题。

    您可以执行以下操作:

    return new MaterialApp(
      // default theme here
      theme: new ThemeData(),
      builder: (context, child) {
        final defaultTheme = Theme.of(context);
        if (defaultTheme.platform == TargetPlatform.iOS) {
          return new Theme(
            data: defaultTheme.copyWith(
              primaryColor: Colors.purple
            ),
            child: child,
          );
        }
        return child;
      }
    );
    

    这将指定一个默认主题。然后为IOS覆盖primaryColor

    【讨论】:

    • 当使用这种方法时,我无法覆盖某些值,例如 fontFamily 和其他一些东西......还有其他方法吗?
    • 然后使用new ThemeData() 而不是defaultTheme.copyWith。但是您必须手动分配旧值
    猜你喜欢
    • 2021-04-24
    • 2021-06-08
    • 2020-11-15
    • 2021-10-07
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多