69 lines
2.5 KiB
Dart
69 lines
2.5 KiB
Dart
// responsive.dart
|
|
// Utility class for handling responsive design in Flutter apps. Provides helpers for device type checks, scaling, and adaptive sizing.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// The Responsive class provides static methods to help adapt UI to different screen sizes (mobile, tablet, desktop).
|
|
class Responsive {
|
|
/// Returns true if the device is considered a mobile (width < 600px).
|
|
static bool isMobile(BuildContext context) =>
|
|
MediaQuery.of(context).size.width < 600;
|
|
|
|
/// Returns true if the device is considered a tablet (600px <= width < 1200px).
|
|
static bool isTablet(BuildContext context) =>
|
|
MediaQuery.of(context).size.width >= 600 &&
|
|
MediaQuery.of(context).size.width < 1200;
|
|
|
|
/// Returns true if the device is considered a desktop (width >= 1200px).
|
|
static bool isDesktop(BuildContext context) =>
|
|
MediaQuery.of(context).size.width >= 1200;
|
|
|
|
/// Returns the current screen width in logical pixels.
|
|
static double getWidth(BuildContext context) =>
|
|
MediaQuery.of(context).size.width;
|
|
|
|
/// Returns the current screen height in logical pixels.
|
|
static double getHeight(BuildContext context) =>
|
|
MediaQuery.of(context).size.height;
|
|
|
|
/// Returns a width scaled by the given percentage of the screen width.
|
|
static double getScaledWidth(BuildContext context, double percentage) =>
|
|
getWidth(context) * (percentage / 100);
|
|
|
|
/// Returns a height scaled by the given percentage of the screen height.
|
|
static double getScaledHeight(BuildContext context, double percentage) =>
|
|
getHeight(context) * (percentage / 100);
|
|
|
|
/// Returns appropriate padding based on device type (mobile/tablet/desktop).
|
|
static EdgeInsets getPadding(BuildContext context) {
|
|
if (isMobile(context)) {
|
|
return const EdgeInsets.all(16.0);
|
|
} else if (isTablet(context)) {
|
|
return const EdgeInsets.all(24.0);
|
|
} else {
|
|
return const EdgeInsets.all(32.0);
|
|
}
|
|
}
|
|
|
|
/// Returns a font size scaled for the device type.
|
|
static double getFontSize(BuildContext context, double baseSize) {
|
|
if (isMobile(context)) {
|
|
return baseSize;
|
|
} else if (isTablet(context)) {
|
|
return baseSize * 1.2;
|
|
} else {
|
|
return baseSize * 1.4;
|
|
}
|
|
}
|
|
|
|
/// Returns an icon size scaled for the device type.
|
|
static double getIconSize(BuildContext context, double baseSize) {
|
|
if (isMobile(context)) {
|
|
return baseSize;
|
|
} else if (isTablet(context)) {
|
|
return baseSize * 1.2;
|
|
} else {
|
|
return baseSize * 1.4;
|
|
}
|
|
}
|
|
} |