| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:trackoffical_app/styles/color_schemes.g.dart';
- class SizeFit{
- static double _screenWidth = 0;
- static double _screenHeight = 0;
- static double _screenWidthD = 0;
- static double _screenHeightD = 0;
- static double _rpx=0;
- static double _px=0;
- static const double standardWidth = 750;
- static void screenInit(BuildContext context){
- _screenWidthD = context.width;
- _screenHeightD = context.height;
- final physicalSize = View.of(context).physicalSize;
- // 获取物理分辨率
- var physicalWidth=physicalSize.width;
- var physicalHeight=physicalSize.height;
- //获取dpr
- var dpr=context.devicePixelRatio;
- _screenWidth = physicalWidth/dpr;
- _screenHeight = physicalHeight/dpr;
- // 计算rpx 和px大小
- _rpx = _screenWidth / standardWidth;
- _px = _screenWidth / standardWidth * 2; // 必须是乘以2,因为是以iphone6(750)为标准
- }
- // 按照像素来设置
- static double setPx(double size) {
- return _px * size ;
- }
- // 按照rpx来设置(如果给的设计稿是物理像素用rpx)
- static double setRpx(double size) {
- return _rpx * size;
- }
- }
- extension MNumFit on num {
- double get px {
- //这里的this是数字200.0, 相当于使用时 width: 200.0.px 里面的200.0 ,相当于是它调用方法
- return SizeFit.setPx(toDouble());
- }
- double get rpx {
- //这里和上面都是用的get ,所以外面使用的时候是:width:200.5.rpx
- return SizeFit.setRpx(toDouble());
- }
- /// 屏幕宽度百分比
- double get wp{
- return SizeFit._screenWidthD * (toDouble()/100);
- }
- /// 屏幕高度百分比
- double get hp{
- return SizeFit._screenHeightD * (toDouble()/100);
- }
- }
- class Preview extends StatelessWidget{
- const Preview({super.key, required this.child});
- final Widget child;
- @override
- Widget build(BuildContext context) {
- SizeFit.screenInit(context);
- return child;
- }
- }
- void runPreview(Widget child){
- runApp(GetMaterialApp(
- theme: ThemeData(useMaterial3: true, colorScheme: lightColorScheme),
- home: Preview(child: child)));
- }
|