我们可以在 Flutter 中检查设备是智能手机还是平板电脑?
Can we check the device to be smartphone or tablet in Flutter?
我实际上是想弄清楚该应用程序是 运行 在我的 flutter 应用程序中的智能手机还是平板电脑上,但是包 device_info
只能说明设备,而不能说明设备是否是智能手机或平板电脑。有没有办法通过检查设备的大小来做到这一点?
非常感谢
马希
尽管 iOS 在 phone 和平板电脑之间有明显的区别,但 Android 并没有发生这种情况。您需要根据屏幕宽度进行选择。
查看这篇文章以查看有关如何区分的示例:https://flutter.rocks/2018/01/28/implementing-adaptive-master-detail-layouts/
其中一种方法是计算屏幕分辨率的对角线。
import 'package:flutter/widgets.dart';
import 'dart:math';
class TabletDetector {
// iPhone 6S
// |_ [portrait]
// |_ size: 375.0x667.0, pixelRatio: 2.0, pixels: 750.0x1334.0
// |_ diagonal: 765.1888655750291
// |_ [horizontal]
// |_ size: 667.0x375.0, pixelRatio: 2.0, pixels: 1334.0x750.0
// |_ diagonal: 765.1888655750291
// iPhone X
// |_ [portrait]
// |_ size: 375.0x812.0, pixelRatio: 3.0, pixels: 1125.0x2436.0
// |_ diagonal: 894.4098613052072
// |_ [horizontal]
// |_ size: 812.0x375.0, pixelRatio: 3.0, pixels: 2436.0x1125.0
// |_ diagonal: 894.4098613052072
// iPhone XS Max
// |_ [portrait]
// |_ size: 414.0x896.0, pixelRatio: 3.0, pixels: 1242.0x2688.0
// |_ diagonal: 987.0217829409845
// |_ [horizontal]
// |_ size: 896.0x414.0, pixelRatio: 3.0, pixels: 2688.0x1242.0
// |_ diagonal: 987.0217829409845
// iPad Pro (9.7-inch)
// |_ [portrait]
// |_ size: 768.0x1024.0, pixelRatio: 2.0, pixels: 1536.0x2048.0
// |_ diagonal: 1280.0
// |_ [horizontal]
// |_ size: 1024.0x768.0, pixelRatio: 2.0, pixels: 2048.0x1536.0
// |_ diagonal: 1280.0
// iPad Pro (10.5-inch)
// |_ [portrait]
// |_ size: 834.0x1112.0, pixelRatio: 2.0, pixels: 1668.0x2224.0
// |_ diagonal: 1390.0
// |_ [horizontal]
// |_ size: 1112.0x834.0, pixelRatio: 2.0, pixels: 2224.0x1668.0
// |_ diagonal: 1390.0
// iPad Pro (12.9-inch)
// |_ [portrait]
// |_ size: 1024.0x1366.0, pixelRatio: 2.0, pixels: 2048.0x2732.0
// |_ diagonal: 1707.2000468603555
// |_ [horizontal]
// |_ size: 1366.0x1024.0, pixelRatio: 2.0, pixels: 2732.0x2048.0
// |_ diagonal: 1707.2000468603555
static bool isTablet(MediaQueryData query) {
var size = query.size;
var diagonal = sqrt(
(size.width * size.width) +
(size.height * size.height)
);
/*
print(
'size: ${size.width}x${size.height}\n'
'pixelRatio: ${query.devicePixelRatio}\n'
'pixels: ${size.width * query.devicePixelRatio}x${size.height * query.devicePixelRatio}\n'
'diagonal: $diagonal'
);
*/
var isTablet = diagonal > 1100.0;
return isTablet;
}
}
// The equivalent of the "smallestWidth" qualifier on Android.
var shortestSide = MediaQuery.of(context).size.shortestSide;
// Determine if we should use mobile layout or not, 600 here is
// a common breakpoint for a typical 7-inch tablet.
final bool useMobileLayout = shortestSide < 600;
复制自https://flutter.rocks/2018/01/28/implementing-adaptive-master-detail-layouts/
感谢@Sergi
如果您无权访问 BuildContext,则可以使用它。我从sdk/flutter/packages/flutter/lib/src/widgets/app.dart:1252
.
中取出来的
String getDeviceType() {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return data.size.shortestSide < 600 ? 'phone' :'tablet';
}
对于 Android 正如@Chandler 所说,您应该检查屏幕的最小尺寸,但是对于 iOS,您可以使用 [= 100% 准确地确定它是否是 iPad 13=] 包:
加入pubspec.yaml
:
device_info: ^0.4.2+4
static Future<bool> isTablet(BuildContext context) async {
if (Platform.isIOS) {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
return iosInfo.model.toLowerCase() == "ipad";
} else {
// The equivalent of the "smallestWidth" qualifier on Android.
var shortestSide = MediaQuery.of(context).size.shortestSide;
// Determine if we should use mobile layout or not, 600 here is
// a common breakpoint for a typical 7-inch tablet.
return shortestSide > 600;
}
}
这里与其他答案相同,但返回 enum
而不是 bool
或 String
。因为它更封闭,所以更容易使用它。
import 'package:flutter/widgets.dart';
enum DeviceType { Phone, Tablet }
DeviceType getDeviceType() {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return data.size.shortestSide < 550 ? DeviceType.Phone : DeviceType.Tablet;
}
感谢@Chandler 和@bakua 的启发:·)
Usually the Aspect ratio ( width : height) of Android Tablet and iPad is in the range of from 0.75 ~ 1.4, following is the fastest way to check. We can adjust the UI according to the Aspect ratio.
bool isTablet;
double ratio = MediaQuery.of(context).size.width / MediaQuery.of(context).size.height;
if( (ratio >= 0.74) && (ratio < 1.5) )
{
isTablet = true;
} else{
isTablet = false;
}
在你的项目中添加下面的util class并使用它。
class DeviceUtil {
static String get _getDeviceType {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return data.size.shortestSide < 550 ? 'phone' : 'tablet';
}
static bool get isTablet {
return _getDeviceType == 'tablet';
}
}
用法:
DeviceUtil.isTablet; //true
我实际上是想弄清楚该应用程序是 运行 在我的 flutter 应用程序中的智能手机还是平板电脑上,但是包 device_info
只能说明设备,而不能说明设备是否是智能手机或平板电脑。有没有办法通过检查设备的大小来做到这一点?
非常感谢 马希
尽管 iOS 在 phone 和平板电脑之间有明显的区别,但 Android 并没有发生这种情况。您需要根据屏幕宽度进行选择。
查看这篇文章以查看有关如何区分的示例:https://flutter.rocks/2018/01/28/implementing-adaptive-master-detail-layouts/
其中一种方法是计算屏幕分辨率的对角线。
import 'package:flutter/widgets.dart';
import 'dart:math';
class TabletDetector {
// iPhone 6S
// |_ [portrait]
// |_ size: 375.0x667.0, pixelRatio: 2.0, pixels: 750.0x1334.0
// |_ diagonal: 765.1888655750291
// |_ [horizontal]
// |_ size: 667.0x375.0, pixelRatio: 2.0, pixels: 1334.0x750.0
// |_ diagonal: 765.1888655750291
// iPhone X
// |_ [portrait]
// |_ size: 375.0x812.0, pixelRatio: 3.0, pixels: 1125.0x2436.0
// |_ diagonal: 894.4098613052072
// |_ [horizontal]
// |_ size: 812.0x375.0, pixelRatio: 3.0, pixels: 2436.0x1125.0
// |_ diagonal: 894.4098613052072
// iPhone XS Max
// |_ [portrait]
// |_ size: 414.0x896.0, pixelRatio: 3.0, pixels: 1242.0x2688.0
// |_ diagonal: 987.0217829409845
// |_ [horizontal]
// |_ size: 896.0x414.0, pixelRatio: 3.0, pixels: 2688.0x1242.0
// |_ diagonal: 987.0217829409845
// iPad Pro (9.7-inch)
// |_ [portrait]
// |_ size: 768.0x1024.0, pixelRatio: 2.0, pixels: 1536.0x2048.0
// |_ diagonal: 1280.0
// |_ [horizontal]
// |_ size: 1024.0x768.0, pixelRatio: 2.0, pixels: 2048.0x1536.0
// |_ diagonal: 1280.0
// iPad Pro (10.5-inch)
// |_ [portrait]
// |_ size: 834.0x1112.0, pixelRatio: 2.0, pixels: 1668.0x2224.0
// |_ diagonal: 1390.0
// |_ [horizontal]
// |_ size: 1112.0x834.0, pixelRatio: 2.0, pixels: 2224.0x1668.0
// |_ diagonal: 1390.0
// iPad Pro (12.9-inch)
// |_ [portrait]
// |_ size: 1024.0x1366.0, pixelRatio: 2.0, pixels: 2048.0x2732.0
// |_ diagonal: 1707.2000468603555
// |_ [horizontal]
// |_ size: 1366.0x1024.0, pixelRatio: 2.0, pixels: 2732.0x2048.0
// |_ diagonal: 1707.2000468603555
static bool isTablet(MediaQueryData query) {
var size = query.size;
var diagonal = sqrt(
(size.width * size.width) +
(size.height * size.height)
);
/*
print(
'size: ${size.width}x${size.height}\n'
'pixelRatio: ${query.devicePixelRatio}\n'
'pixels: ${size.width * query.devicePixelRatio}x${size.height * query.devicePixelRatio}\n'
'diagonal: $diagonal'
);
*/
var isTablet = diagonal > 1100.0;
return isTablet;
}
}
// The equivalent of the "smallestWidth" qualifier on Android.
var shortestSide = MediaQuery.of(context).size.shortestSide;
// Determine if we should use mobile layout or not, 600 here is
// a common breakpoint for a typical 7-inch tablet.
final bool useMobileLayout = shortestSide < 600;
复制自https://flutter.rocks/2018/01/28/implementing-adaptive-master-detail-layouts/
感谢@Sergi
如果您无权访问 BuildContext,则可以使用它。我从sdk/flutter/packages/flutter/lib/src/widgets/app.dart:1252
.
String getDeviceType() {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return data.size.shortestSide < 600 ? 'phone' :'tablet';
}
对于 Android 正如@Chandler 所说,您应该检查屏幕的最小尺寸,但是对于 iOS,您可以使用 [= 100% 准确地确定它是否是 iPad 13=] 包:
加入pubspec.yaml
:
device_info: ^0.4.2+4
static Future<bool> isTablet(BuildContext context) async {
if (Platform.isIOS) {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
return iosInfo.model.toLowerCase() == "ipad";
} else {
// The equivalent of the "smallestWidth" qualifier on Android.
var shortestSide = MediaQuery.of(context).size.shortestSide;
// Determine if we should use mobile layout or not, 600 here is
// a common breakpoint for a typical 7-inch tablet.
return shortestSide > 600;
}
}
这里与其他答案相同,但返回 enum
而不是 bool
或 String
。因为它更封闭,所以更容易使用它。
import 'package:flutter/widgets.dart';
enum DeviceType { Phone, Tablet }
DeviceType getDeviceType() {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return data.size.shortestSide < 550 ? DeviceType.Phone : DeviceType.Tablet;
}
感谢@Chandler 和@bakua 的启发:·)
Usually the Aspect ratio ( width : height) of Android Tablet and iPad is in the range of from 0.75 ~ 1.4, following is the fastest way to check. We can adjust the UI according to the Aspect ratio.
bool isTablet;
double ratio = MediaQuery.of(context).size.width / MediaQuery.of(context).size.height;
if( (ratio >= 0.74) && (ratio < 1.5) )
{
isTablet = true;
} else{
isTablet = false;
}
在你的项目中添加下面的util class并使用它。
class DeviceUtil {
static String get _getDeviceType {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return data.size.shortestSide < 550 ? 'phone' : 'tablet';
}
static bool get isTablet {
return _getDeviceType == 'tablet';
}
}
用法:
DeviceUtil.isTablet; //true