我想为我的flutter应用程序使用不同的主题,具体取决于它开始使用的操作系统。在选择要应用的主题时,如何检测操作系统?
Theme.of(context).platform == TargetPlatform.iOS
不起作用,因为我还没有应用主题......
答案 0 :(得分:2)
您可以通过将视图包装到具有自定义属性的新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
。