208 lines
5.2 KiB
Dart
208 lines
5.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class ScreenAdapter {
|
|
static void setPreferredOrientations() {
|
|
SystemChrome.setPreferredOrientations([
|
|
DeviceOrientation.portraitUp,
|
|
DeviceOrientation.portraitDown,
|
|
DeviceOrientation.landscapeLeft,
|
|
DeviceOrientation.landscapeRight,
|
|
]);
|
|
}
|
|
|
|
static void enableSystemUIOverlay() {
|
|
SystemChrome.setEnabledSystemUIMode(
|
|
SystemUiMode.manual,
|
|
overlays: [
|
|
SystemUiOverlay.top,
|
|
SystemUiOverlay.bottom,
|
|
],
|
|
);
|
|
}
|
|
|
|
static void enableImmersiveMode() {
|
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
|
|
}
|
|
|
|
static void setSystemUIOverlayStyle({
|
|
Color? statusBarColor,
|
|
Brightness? statusBarIconBrightness,
|
|
}) {
|
|
SystemChrome.setSystemUIOverlayStyle(
|
|
SystemUiOverlayStyle(
|
|
statusBarColor: statusBarColor ?? Colors.transparent,
|
|
statusBarIconBrightness: statusBarIconBrightness ?? Brightness.dark,
|
|
),
|
|
);
|
|
}
|
|
|
|
static bool isSplitScreen(BuildContext context) {
|
|
final screenWidth = MediaQuery.of(context).size.width;
|
|
final screenHeight = MediaQuery.of(context).size.height;
|
|
final aspectRatio = screenWidth / screenHeight;
|
|
|
|
// 分屏模式通常具有特殊的宽高比
|
|
return aspectRatio > 2.0 || aspectRatio < 0.5;
|
|
}
|
|
|
|
static bool isPopupWindow(BuildContext context) {
|
|
final screenWidth = MediaQuery.of(context).size.width;
|
|
final screenHeight = MediaQuery.of(context).size.height;
|
|
|
|
// 小窗模式通常屏幕尺寸较小
|
|
return screenWidth < 600 || screenHeight < 800;
|
|
}
|
|
|
|
static bool isTabletMode(BuildContext context) {
|
|
final shortestSide = MediaQuery.of(context).size.shortestSide;
|
|
return shortestSide >= 600;
|
|
}
|
|
|
|
static void adaptForSplitScreen(BuildContext context) {
|
|
if (isSplitScreen(context)) {
|
|
// 分屏模式下的适配
|
|
SystemChrome.setEnabledSystemUIMode(
|
|
SystemUiMode.manual,
|
|
overlays: [SystemUiOverlay.top],
|
|
);
|
|
}
|
|
}
|
|
|
|
static void adaptForPopupWindow(BuildContext context) {
|
|
if (isPopupWindow(context)) {
|
|
// 小窗模式下的适配
|
|
SystemChrome.setEnabledSystemUIMode(
|
|
SystemUiMode.manual,
|
|
overlays: [SystemUiOverlay.top],
|
|
);
|
|
}
|
|
}
|
|
|
|
static void handleOrientationChange(BuildContext context) {
|
|
final orientation = MediaQuery.of(context).orientation;
|
|
|
|
if (orientation == Orientation.landscape) {
|
|
// 横屏模式适配
|
|
SystemChrome.setEnabledSystemUIMode(
|
|
SystemUiMode.manual,
|
|
overlays: [SystemUiOverlay.top],
|
|
);
|
|
} else {
|
|
// 竖屏模式适配
|
|
SystemChrome.setEnabledSystemUIMode(
|
|
SystemUiMode.manual,
|
|
overlays: [
|
|
SystemUiOverlay.top,
|
|
SystemUiOverlay.bottom,
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
static Size getAdaptiveSize(BuildContext context) {
|
|
final mediaQuery = MediaQuery.of(context);
|
|
final size = mediaQuery.size;
|
|
final orientation = mediaQuery.orientation;
|
|
|
|
if (isSplitScreen(context)) {
|
|
// 分屏模式下的尺寸调整
|
|
return Size(
|
|
size.width * 0.9,
|
|
size.height * 0.85,
|
|
);
|
|
}
|
|
|
|
if (isPopupWindow(context)) {
|
|
// 小窗模式下的尺寸调整
|
|
return Size(
|
|
size.width * 0.95,
|
|
size.height * 0.9,
|
|
);
|
|
}
|
|
|
|
if (orientation == Orientation.landscape) {
|
|
// 横屏模式下的尺寸调整
|
|
return Size(
|
|
size.width * 0.95,
|
|
size.height * 0.9,
|
|
);
|
|
}
|
|
|
|
return size;
|
|
}
|
|
|
|
static EdgeInsets getAdaptivePadding(BuildContext context) {
|
|
final size = MediaQuery.of(context).size;
|
|
|
|
if (isSplitScreen(context)) {
|
|
return EdgeInsets.symmetric(
|
|
horizontal: size.width * 0.02,
|
|
vertical: size.height * 0.02,
|
|
);
|
|
}
|
|
|
|
if (isPopupWindow(context)) {
|
|
return EdgeInsets.symmetric(
|
|
horizontal: size.width * 0.03,
|
|
vertical: size.height * 0.03,
|
|
);
|
|
}
|
|
|
|
return const EdgeInsets.all(16.0);
|
|
}
|
|
|
|
static double getAdaptiveFontSize(BuildContext context, double baseFontSize) {
|
|
final screenWidth = MediaQuery.of(context).size.width;
|
|
|
|
if (isSplitScreen(context)) {
|
|
return baseFontSize * 0.9;
|
|
}
|
|
|
|
if (isPopupWindow(context)) {
|
|
return baseFontSize * 0.85;
|
|
}
|
|
|
|
if (screenWidth < 600) {
|
|
return baseFontSize * 0.9;
|
|
} else if (screenWidth > 1200) {
|
|
return baseFontSize * 1.1;
|
|
}
|
|
|
|
return baseFontSize;
|
|
}
|
|
|
|
static int getAdaptiveGridColumns(BuildContext context) {
|
|
final screenWidth = MediaQuery.of(context).size.width;
|
|
|
|
if (isSplitScreen(context)) {
|
|
return 2;
|
|
}
|
|
|
|
if (isPopupWindow(context)) {
|
|
return 1;
|
|
}
|
|
|
|
if (screenWidth >= 1440) {
|
|
return 6;
|
|
} else if (screenWidth >= 1024) {
|
|
return 4;
|
|
} else if (screenWidth >= 768) {
|
|
return 3;
|
|
} else {
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
static void setupAdaptiveConfig(BuildContext context) {
|
|
// 设置自适应配置
|
|
setPreferredOrientations();
|
|
enableSystemUIOverlay();
|
|
|
|
// 根据屏幕状态进行适配
|
|
adaptForSplitScreen(context);
|
|
adaptForPopupWindow(context);
|
|
handleOrientationChange(context);
|
|
}
|
|
}
|