我将此代码用于我的 UI 中的响应性。所以这段代码基本上做的是计算屏幕的大小,我使用下面的函数根据在 Figma 或 Adobe XD 中提供给我的设计来放置确切的字体大小。使用这种方法,我能够创建像素完美的 UI。
升级到 后Flutter 2.0.3
,我收到空安全错误。我能够解决其中的大部分问题,但我无法解决此错误。请指教。
Complete Code
import 'package:flutter/material.dart';
class SizeConfig {
static MediaQueryData? _mediaQueryData;
static double? screenWidth;
static double? screenHeight;
static double? defaultSize;
static Orientation? orientation;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData!.size.width;
screenHeight = _mediaQueryData!.size.height;
orientation = _mediaQueryData!.orientation;
if (orientation == Orientation.landscape) {
defaultSize = screenHeight! * 0.024;
} else {
defaultSize = screenWidth! * 0.024;
}
}
}
double getSize(double size) {
var defaultsSize = SizeConfig.defaultSize * size;
return (defaultsSize / 10);
}
// Get the proportionate height as per screen size
double getProportionateScreenHeight(double inputHeight) {
double screenHeight = SizeConfig.screenHeight!;
// 812 is the layout height that designer use
return (inputHeight / 812.0) * screenHeight;
}
// Get the proportionate width as per screen size
double getProportionateScreenWidth(double inputWidth) {
double screenWidth = SizeConfig.screenWidth!;
// 375 is the layout width that Figma provides
return (inputWidth / 375.0) * screenWidth;
}
Error