我们可以将设备检查为智能手机或平板电脑吗?

时间:2018-03-26 05:33:04

标签: dart flutter

我实际上是想弄清楚应用程序是在智能手机或平板电脑上运行的,但是device_info包只能告诉设备,而不是设备是智能手机还是平板电脑。有没有办法我们可以通过检查设备的大小来做到这一点。

非常感谢 鳅

7 个答案:

答案 0 :(得分:16)

如果您无权访问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';
  }

答案 1 :(得分:3)

尽管iOS在手机和平​​板电脑之间有明显的分离,但这在Android中并不会发生。您需要根据屏幕宽度进行选择。

查看此文章,查看有关如何区分的示例:https://flutter.rocks/2018/01/28/implementing-adaptive-master-detail-layouts/

答案 2 :(得分:2)

// 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 = smallestDimension < 600;

https://flutter.rocks/2018/01/28/implementing-adaptive-master-detail-layouts/复制

感谢@Sergi

答案 3 :(得分:1)

其中一种方法是计算屏幕分辨率的对角线。

$(document).ready(function() {

$('#calendar').fullCalendar({
  header: {
    left: 'prev,next today',
    center: 'title',
    right: 'month,agendaWeek,agendaDay,listWeek'
  },
  defaultDate: '2018-03-12',
  navLinks: true, // can click day/week names to navigate views
  editable: true,
  eventLimit: true, // allow "more" link when too many events
  events: [


    {
      title: 'All Day Event',
      start: '2018-03-01',
    },
    {
      title: 'Long Event',
      start: '2018-03-07',
      end: '2018-03-10'
    },
    {
      id: 999,
      title: 'Repeating Event',
      start: '2018-03-09T16:00:00'
    },
    {
      id: 999,
      title: 'Repeating Event',
      start: '2018-03-16T16:00:00'
    },
    {
      title: 'Conference',
      start: '2018-03-11',
      end: '2018-03-13'
    },
    {
      title: 'Meeting',
      start: '2018-03-12T10:30:00',
      end: '2018-03-12T12:30:00'
    },
    {
      title: 'Lunch',
      start: '2018-03-12T12:00:00'
    },
    {
      title: 'Meeting',
      start: '2018-03-12T14:30:00'
    },
    {
      title: 'Happy Hour',
      start: '2018-03-12T17:30:00'
    },
    {
      title: 'Dinner',
      start: '2018-03-12T20:00:00'
    },
    {
      title: 'Birthday Party',
      start: '2018-03-13T07:00:00'
    },
    {
      title: 'Click for Google',
      url: 'http://google.com/',
      start: '2018-03-28'
    }
  ]
});

答案 4 :(得分:1)

与其他查询中的相同,但是返回enum而不是boolString。由于它更封闭,因此更易于使用。

import 'package:flutter/widgets.dart';

enum DeviceType { Phone, Tablet }

DeviceType getDeviceType() {
  final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
  return data.size.shortestSide < 600 ? DeviceType.Phone : DeviceType.Tablet;
}

感谢@Chandler和@bakua的启发:·)

答案 5 :(得分:0)

对于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;
    }
  }

答案 6 :(得分:0)

通常,Android平板电脑和iPad的宽高比(宽度:高度)在0.75〜1.4的范围内,以下是最快的检查方法。我们可以根据宽高比调整用户界面。

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