【问题标题】:separate web and mobile flutter单独的 web 和移动颤振
【发布时间】:2021-05-26 20:28:28
【问题描述】:
我想在我的 Flutter 项目中将 Web 和移动设备分开。我使用下面的代码,但不能在网络应用程序中工作(当我用这种模式在 chrome 中测试时)
onTap: () {
Platform.isAndroid || Platform.isIOS
? Navigator.push(context,
MaterialPageRoute(builder: (_) => ArticlePage(title, id)))
: launch(URL);
}
在网络应用程序中,我的按钮不起作用,而在移动设备中则起作用。
任何想法或提示?!
【问题讨论】:
标签:
flutter
flutter-web
flutter-method-channel
【解决方案1】:
你可以:
1- 使用像universal_io 这样的包。
2-如果你不想使用一个包你需要创建两个文件
第一个platform_io.dart
import 'dart:io';
class QPlatform {
static const bool isWeb = false;
static final bool isIOS = Platform.isIOS || Platform.isMacOS;
}
第二个platfrom_web.dart
class QPlatform {
static const bool isWeb = true;
static final bool isIOS = false;
}
然后像这样导入到你想用的地方
import 'platform/platform_web.dart'
if (dart.library.io) 'platform/platform_io.dart';
// and use it
onTap: () {
QPlatform.isWeb
? launch(URL)
: Navigator.push(context,
MaterialPageRoute(builder: (_) => ArticlePage(title, id)));
}
这应该对你有用