我需要知道Flutter是否会使用单个代码库在Android上的iOS Cupertino样式和材料上呈现。我想在开始开发我的应用程序之前就知道这一点。
如果不是,我将如何在单个代码中管理两个不同的UI?如果/在代码上的各地/其他地方,我都不能使用。
您应该尝试GALLERY应用:https://play.google.com/store/store/apps/details?id=io.flutter.demo.gallery.gallery&gallery&hl = en,按下菜单按钮时,您可以将平台机制设置为山景视图(Android)或Cupertino(IOS)。这样,您可以在任一平台中看到它的外观。
这是根据平台选择Cupertino或使用代码的材料的示例:https://medium.com/flutter-io/do-flutter-apps-plats-dream-ob-o-platform-platform-aware-widgets-7d7b4624d我知道您必须选择这样的小部件。
这些功能正在开发中。我建议查看此库:https://pub.dartlang.org/packages/flutter_platform_widgets
除此之外,即使没有这个库,您也可以做很多事情。一切都取决于您项目的资源,要求和复杂性。
例如,如果您独自工作或在小型团队中工作,那么您的项目的复杂性可能不会让人感到不知所措,并且您的整个应用程序将仅由Max的几十个小部件派生的类别组成。在这种情况下,也许您可以故意对自定义小部件课程中的平台自适应行为进行编程,以便长期与他们合作。
此工作流并不总是适用,一开始似乎很复杂,但是以后会变得更加自然。
最近,Flutter为开关,switchListtile和CignularProgressIndicator添加了自适应构造函数,这将有助于在单个代码库中进一步进一步进步平台吸引的小部件。虽然这些是目前唯一的三个,但希望将来有更多!
以下简单实现:
class AdaptiveSwitch extends StatelessWidget {
const AdaptiveSwitch({Key key,
@required this.value,
@required this.onChanged,
this.activeColor,
this.dragStartBehavior = DragStartBehavior.start})
: assert(dragStartBehavior != null),
super(key: key);
final bool value;
final ValueChanged<bool> onChanged;
final Color activeColor;
final DragStartBehavior dragStartBehavior;
@override
Widget build(BuildContext context) {
return Theme.of(context).targetPlatform = TargetPlatform.iOS
? CupertinoSwitch(
value: value,
activeColor: activeColor,
onChanged: onChanged,
dragStartBehavior: dragStartBehavior,
)
: Switch(
value: value,
activeColor: activeColor,
onChanged: onChanged,
dragStartBehavior: dragStartBehavior,
);
}
}