How does flutter make screen adaptation?

how does flutter make screen adaptation

Mar.25,2022

refer to https://github.com/OpenFlutte.

.
/*
 * Created by  on 2018/9/29.
 * email: zhuoyuan93@gmail.com
 */

import 'package:flutter/material.dart';

class ScreenUtil {
  static ScreenUtil instance = new ScreenUtil();

  /// UI , px
  /// Size of the phone in UI Design , px
  double width;
  double height;

  /// "" false
  /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
  bool allowFontScaling;

  static MediaQueryData _mediaQueryData;
  static double _screenWidth;
  static double _screenHeight;
  static double _pixelRatio;
  static double _statusBarHeight;

  static double _bottomBarHeight;

  static double _textScaleFactor;

  ScreenUtil({
    this.width = 1080,
    this.height = 1920,
    this.allowFontScaling = false,
  });

  static ScreenUtil getInstance() {
    return instance;
  }

  void init(BuildContext context) {
    MediaQueryData mediaQuery = MediaQuery.of(context);
    _mediaQueryData = mediaQuery;
    _pixelRatio = mediaQuery.devicePixelRatio;
    _screenWidth = mediaQuery.size.width;
    _screenHeight = mediaQuery.size.height;
    _statusBarHeight = mediaQuery.padding.top;
    _bottomBarHeight = _mediaQueryData.padding.bottom;
    _textScaleFactor = mediaQuery.textScaleFactor;
  }

  static MediaQueryData get mediaQueryData => _mediaQueryData;

  /// 
  /// The number of font pixels for each logical pixel.
  static double get textScaleFactory => _textScaleFactor;

  /// 
  /// The size of the media in logical pixels (e.g, the size of the screen).
  static double get pixelRatio => _pixelRatio;

  ///  dp
  /// The horizontal extent of this size.
  static double get screenWidthDp => _screenWidth;

  /// dp
  ///The vertical extent of this size. dp
  static double get screenHeightDp => _screenHeight;

  ///  px
  /// The vertical extent of this size. px
  static double get screenWidth => _screenWidth * _pixelRatio;

  ///  px
  /// The vertical extent of this size. px
  static double get screenHeight => _screenHeight * _pixelRatio;

  ///  dp 
  /// The offset from the top
  static double get statusBarHeight => _statusBarHeight;

  ///  dp
  /// The offset from the bottom.
  static double get bottomBarHeight => _bottomBarHeight;

  /// dpUIpx
  /// The ratio of the actual dp to the design draft px
  get scaleWidth => _screenWidth / instance.width;

  get scaleHeight => _screenHeight / instance.height;

  /// UI
  /// ,.
  /// Adapted to the device width of the UI Design.
  /// Height can also be adapted according to this to ensure no deformation ,
  /// if you want a square
  setWidth(num width) => width * scaleWidth;

  /// UI
  /// UI,
  /// ,.
  /// UI
  /// Highly adaptable to the device according to UI Design
  /// It is recommended to use this method to achieve a high degree of adaptation
  /// when it is found that one screen in the UI design
  /// does not match the current style effect, or if there is a difference in shape.
  setHeight(num height) => height * scaleHeight;

  ///
  ///@param [fontSize] UI,px.
  ///Font size adaptation method
  ///@param [fontSize] The size of the font on the UI design, in px.
  ///@param [allowFontScaling]
  setSp(num fontSize) => allowFontScaling
      ? setWidth(fontSize)
      : setWidth(fontSize) / _textScaleFactor;
}
Menu