我实际上是想弄清楚该应用程序是在我的flutter应用程序中是在智能手机还是平板电脑上运行,但包装device_info只能显示有关设备的信息,而不能告知该设备是智能手机还是平板电脑。有没有一种方法可以通过检查设备的大小来做到这一点。
非常感谢Mahi
bak*_*kua 25
如果您无权访问 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';
}
Run Code Online (Sandbox Code Playgroud)
小智 15
其中一种方法是计算屏幕分辨率的对角线。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
Ser*_*ace 13
尽管 iOS 在手机和平板电脑之间有明显的区别,但这在 Android 中不会发生。您需要根据屏幕宽度进行选择。
查看本文以查看有关如何区分的示例:https : //flutter.rocks/2018/01/28/implementing-adaptive-master-detail-layouts/
对于 Android,正如 @Chandler 所说,您应该检查屏幕的最小尺寸,但对于 iOS,您可以使用device_info包以 100% 的准确度识别它是否是 iPad:
加入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;
}
}
Run Code Online (Sandbox Code Playgroud)
2023 年,Dart 3 解决方案的灵感来自 @bakua 的答案,以及这个答案
bool get isTablet {
final firstView = WidgetsBinding.instance.platformDispatcher.views.first;
final logicalShortestSide = firstView.physicalSize.shortestSide / firstView.devicePixelRatio;
return logicalShortestSide > 600;
}
Run Code Online (Sandbox Code Playgroud)
// 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;
Run Code Online (Sandbox Code Playgroud)
复制自https://flutter.rocks/2018/01/28/implementing-adaptive-master-detail-layouts/
谢谢@Sergi
这里与其他 aswers 相同,但返回 anenum而不是 abool或 a 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;
}
Run Code Online (Sandbox Code Playgroud)
感谢@Chandler 和@bakua 的启发:·)
| 归档时间: |
|
| 查看次数: |
4157 次 |
| 最近记录: |