getDeviceType function
getDeviceType /// Determines the type of device screen based on its width and orientation.
This function takes a BuildContext
as input and determines the type of the
device screen based on its width and orientation. It utilizes the MediaQuery
to extract information about the device's orientation and screen size. The
function calculates the device width and then classifies it as 'Desktop',
'Tablet', or 'Mobile' based on specific width thresholds. The determined
DeviceScreensType
is returned.
@param context The build context for accessing device-related information. @return The type of device screen based on width and orientation.
Implementation
DeviceScreensType getDeviceType(BuildContext context) {
final Orientation deviceOrientation = MediaQuery.orientationOf(context);
double deviceWidth = 0;
if (deviceOrientation == Orientation.landscape) {
deviceWidth = MediaQuery.sizeOf(context).height;
} else {
deviceWidth = MediaQuery.sizeOf(context).width;
}
if (deviceWidth > 1200) {
return DeviceScreensType.DESKTOP;
}
if (deviceWidth > 600) {
return DeviceScreensType.TABLET;
}
return DeviceScreensType.MOBILE;
}