Flutter判断是否是网页端

新建一个文件PlatformUtils.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import 'dart:io';
import 'package:flutter/foundation.dart';

class PlatformUtils {
static bool _isWeb() {
return kIsWeb == true;
}

static bool _isAndroid() {
return _isWeb() ? false : Platform.isAndroid;
}

static bool _isIOS() {
return _isWeb() ? false : Platform.isIOS;
}

static bool _isMacOS() {
return _isWeb() ? false : Platform.isMacOS;
}

static bool _isWindows() {
return _isWeb() ? false : Platform.isWindows;
}

static bool _isFuchsia() {
return _isWeb() ? false : Platform.isFuchsia;
}

static bool _isLinux() {
return _isWeb() ? false : Platform.isLinux;
}

static bool get isWeb => _isWeb();

static bool get isAndroid => _isAndroid();

static bool get isIOS => _isIOS();

static bool get isMacOS => _isMacOS();

static bool get isWindows => _isWindows();

static bool get isFuchsia => _isFuchsia();

static bool get isLinux => _isLinux();
}

使用的时候

先导入头文件

1
import 'PlatformUtils.dart';

然后这样就可以了

1
2
3
4
if(PlatformUtils.isWeb)
{

}