Flutter - 如何对平台(Android、iOS、Web)使用条件编译?
Flutter - How use conditional compilation for platform (Android, iOS, Web)?
我正在用 Flutter 创建一个移动应用程序。现在我有一个问题,对于一个平台,我会为另一个平台使用插件,我需要编写我的平台代码(插件的实现不合适)。
我看到了几种解决方案:
最好创建多个项目,并在其中使用条件编译和共享文件。我在 visual studio 中使用了这种技术。但我现在正在使用 android 工作室。没有项目文件,只有文件夹。
条件编译支持也有问题。我发现这个 article 并且条件编译非常有限。
创建您自己的插件并充分利用它。但是比较费力。
你有什么建议也许还有第三种方法?
添加这个库(不需要包)
import 'dart:io' show Platform;
现在您可以创建一个函数来检查用户使用的平台。
Widget getWidgetBasedOnPlatform() {
if (Platform.isIOS) {
return Container(); //the one for iOS
}
else if (Platform.isAndroid) {
return Container(); //the one for Android
}
}
在处理多个环境(例如 IO 和 Web)时,添加存根 classes 可能有助于在编译时解决依赖关系,这样,您可以轻松集成多个平台依赖库,而无需为每个编译妥协。
例如,一个插件可以按以下方式构建:
- my_plugin_io.dart
- my_plugin_web.dart
- my_plugin_stub.dart
- my_plugin.dart
让我们用一个简单的例子来分解它:
my_plugin.dart
在这里您可以真正让您的插件 class 用于多个项目(即环境)。
import 'my_plugin_stub.dart'
if (dart.library.io) 'my_plugin_io.dart'
if (dart.library.html) 'my_plugin_web.dart';
class MyPlugin {
void foo() {
var bar = myPluginMethod(); // it will either resolve for the web or io implementation at compile time
}
}
my_plugin_stub.dart
这就是在编译时(存根)实际解析到正确的myPluginMethod()
方法的内容。
Object myPluginMethod() {
throw UnimplementedError('Unsupported');
}
然后创建平台实现
my_plugin_web.dart
import 'dart:html' as html;
Object myPluginMethod() {
// Something that use dart:html data for example
}
my_plugin_io.dart
import 'dart:io';
Object myPluginMethod() {
// Something that use dart:io data for example
}
其他官方替代方案可能会通过创建共享同一界面的独立项目而通过。这就像 Flutter 团队一直在为他们的 web + io 插件所做的那样,导致一个项目可以与多个项目捆绑在一起:
- my_plugin_io
- my_plugin_web
- my_plugin_desktop
- my_plugin_interface
可能会找到解释这一点的好文章 here。
刚刚在 SO 中输入它,所以如果我有一些打字错误,我很抱歉,但你应该很容易在编辑器上找到它。
您只需导入:
import 'dart:io';
然后使用基于以下条件的条件:
// Platform.isIOS // Returns true on iOS devices
// Platform.isAndroid // Returns true on Android devices
if (Platform.isIOS) {
navigationBar = new BottomNavigationBar(...);
}
if (Platform.isAndroid) {
drawer = new Drawer(...);
}
我正在用 Flutter 创建一个移动应用程序。现在我有一个问题,对于一个平台,我会为另一个平台使用插件,我需要编写我的平台代码(插件的实现不合适)。
我看到了几种解决方案:
最好创建多个项目,并在其中使用条件编译和共享文件。我在 visual studio 中使用了这种技术。但我现在正在使用 android 工作室。没有项目文件,只有文件夹。
条件编译支持也有问题。我发现这个 article 并且条件编译非常有限。
创建您自己的插件并充分利用它。但是比较费力。
你有什么建议也许还有第三种方法?
添加这个库(不需要包)
import 'dart:io' show Platform;
现在您可以创建一个函数来检查用户使用的平台。
Widget getWidgetBasedOnPlatform() {
if (Platform.isIOS) {
return Container(); //the one for iOS
}
else if (Platform.isAndroid) {
return Container(); //the one for Android
}
}
在处理多个环境(例如 IO 和 Web)时,添加存根 classes 可能有助于在编译时解决依赖关系,这样,您可以轻松集成多个平台依赖库,而无需为每个编译妥协。
例如,一个插件可以按以下方式构建:
- my_plugin_io.dart
- my_plugin_web.dart
- my_plugin_stub.dart
- my_plugin.dart
让我们用一个简单的例子来分解它:
my_plugin.dart
在这里您可以真正让您的插件 class 用于多个项目(即环境)。
import 'my_plugin_stub.dart'
if (dart.library.io) 'my_plugin_io.dart'
if (dart.library.html) 'my_plugin_web.dart';
class MyPlugin {
void foo() {
var bar = myPluginMethod(); // it will either resolve for the web or io implementation at compile time
}
}
my_plugin_stub.dart
这就是在编译时(存根)实际解析到正确的myPluginMethod()
方法的内容。
Object myPluginMethod() {
throw UnimplementedError('Unsupported');
}
然后创建平台实现
my_plugin_web.dart
import 'dart:html' as html;
Object myPluginMethod() {
// Something that use dart:html data for example
}
my_plugin_io.dart
import 'dart:io';
Object myPluginMethod() {
// Something that use dart:io data for example
}
其他官方替代方案可能会通过创建共享同一界面的独立项目而通过。这就像 Flutter 团队一直在为他们的 web + io 插件所做的那样,导致一个项目可以与多个项目捆绑在一起:
- my_plugin_io
- my_plugin_web
- my_plugin_desktop
- my_plugin_interface
可能会找到解释这一点的好文章 here。
刚刚在 SO 中输入它,所以如果我有一些打字错误,我很抱歉,但你应该很容易在编辑器上找到它。
您只需导入:
import 'dart:io';
然后使用基于以下条件的条件:
// Platform.isIOS // Returns true on iOS devices
// Platform.isAndroid // Returns true on Android devices
if (Platform.isIOS) {
navigationBar = new BottomNavigationBar(...);
}
if (Platform.isAndroid) {
drawer = new Drawer(...);
}