UITargetPlatform
在 Flutter 3.0.5 中,TargetPlatform 枚举没有 web 平台的值。
Theme.of(context).platform 用于使 UI 适应不同的平台,但对于 web 平台,你无法使用它(在 TargetPlatform 枚举中不存在 web),必须使用全局变量 kIsWeb。
此包包含枚举 UITargetPlatform,它具有与 TargetPlatform 相同的值,并添加了一个新的值 web。
它还包含全局变量 debugDefaultUITargetPlatformIsWeb 用于测试。 test_screen 包使用它来进行 web 测试。
用法
从 Theme 获取 TargetPlatform 并将其传递给 UITargetPlatform.fromTargetPlatform()。
@override
Widget build(BuildContext context) {
final platform =
UITargetPlatform.fromTargetPlatform(Theme.of(context).platform);
switch (platform) {
case UITargetPlatform.iOS:
return _cupertinoSlider();
case UITargetPlatform.web:
return _webSlider();
default:
return _defaultSlider();
}
}