单独的网络和移动颤动 [英] separate web and mobile flutter

查看:48
本文介绍了单独的网络和移动颤动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的flutter项目中,我想将Web和移动设备分开.我使用下面的代码,但不能在Web应用程序中使用(当我使用此模式在chrome中进行测试时)

I want to separate between web and mobile in my flutter project. I use below code but not work in web app (when I'm test in chrome with this mode)

onTap: () {    
Platform.isAndroid || Platform.isIOS
            ? Navigator.push(context,
                MaterialPageRoute(builder: (_) => ArticlePage(title, id)))
            : launch(URL);
}

在网络应用中,我的按钮不起作用,而在移动设备中,我的按钮起作用.有任何想法或提示吗?!

in web app my button not work and in mobile is work. any idea or tip?!

推荐答案

您可以:

1-使用类似 universal_io 的软件包.

1- use a package like universal_io.

2-如果您不想使用软件包,则需要创建两个文件

2- if you don't want to use a package you need to create two files

第一个 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;
}

然后将其导入到要使用的位置

and then import it like this where you want to use it

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)));
}

这应该为您解决问题

这篇关于单独的网络和移动颤动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆