Flutter 获取屏幕ppi 和 LP/mm(每毫米逻辑像素的数量)

Flutter当中并没有直接的api可以获取到屏幕的ppi和LP/mm(每毫米逻辑像素的数量)

先装一下这个包:

dependencies:

device_ext_info: ^0.0.2

然后封装了一个函数

Future _getPPI(BuildContext context)async {

    final width = MediaQuery.of(context).size.width;

    final height = MediaQuery.of(context).size.height;

    final diagonal = sqrt(width*width+height*height);

    final screenSizeInches =await DeviceExtInfo.screenSizeInches;

    return diagonal/screenSizeInches;

  }


Future _getLPPerMm(BuildContext context)async {

    final width = MediaQuery.of(context).size.width;

    final height = MediaQuery.of(context).size.height;

    final diagonal = sqrt(width*width+height*height);

    final screenSizeInches =await DeviceExtInfo.screenSizeInches;

    return diagonal/screenSizeInches/25.4;

  }

你可能感兴趣的:(Flutter 获取屏幕ppi 和 LP/mm(每毫米逻辑像素的数量))