How to convert Android's DP to Flutter's LP? What is the difference between DP and LP?
Solution 1
According to the documentation (window.devicePixelRatio
and Flutter for Android Developers), there is no difference between DP and LP.
Device pixels are also referred to as physical pixels. Logical pixels are also referred to as device-independent or resolution-independent pixels.
Flutter doesn’t have
dp
s but there are logical pixels, which are basically the same as device-independent pixels. The so-called devicePixelRatio expresses the ratio of physical pixels in a single logical pixel.
Solution 2
According to https://api.flutter.dev/flutter/dart-ui/FlutterView/devicePixelRatio.html there are roughly 38 logical pixels per centimeter, or about 96 logical pixels per inch, of the physical display.
And according to https://developer.android.com/training/multiscreen/screendensities,One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density).
So we can say:
160 dp == 1 inch == 96 lp
![kosiara - Bartosz Kosarzycki](https://i.stack.imgur.com/SiTPw.jpg?s=256&g=1)
kosiara - Bartosz Kosarzycki
I'm a senior Java/Android developer constantly developing his skills more here: https://sites.google.com/site/bkosarzyckiaboutme/
Updated on December 09, 2022Comments
-
kosiara - Bartosz Kosarzycki over 1 year
I got the designs for a new app. All the dimensions are Android-ready and are given in DP - (Density-independent Pixels). How can I convert these values to Flutter's LP (Logical Pixels). I know that
Window.devicePixelRatio
gives me the number of device pixels for each logical pixel.What's the exact difference between DP and LP? Are there any built-in methods for dp to lp conversion?