屏幕适配

手动实现适配

import 'package:flutter/material.dart';
import 'dart:ui';
class Adapt {
  static MediaQueryData mediaQuery = MediaQueryData.fromWindow(window);
  static double _width = mediaQuery.size.width;
  static double _height = mediaQuery.size.height;
  static double _topbarH = mediaQuery.padding.top;
  static double _botbarH = mediaQuery.padding.bottom;
  static double _pixelRatio = mediaQuery.devicePixelRatio;
  static var _ratio;
  static init(int number){
    int uiwidth = number is int ? number : 750;
    _ratio = _width / uiwidth;
  }
  static px(number){
    if(!(_ratio is double || _ratio is int)){Adapt.init(750);}
    return number * _ratio;
  }
  static onepx(){
    return 1/_pixelRatio;
  }
  static screenW(){
    return _width;
  }
  static screenH(){
    return _height;
  }
  static padTopH(){
    return _topbarH;
  }
  static padBotH(){
    return _botbarH;
  }
}

使用示例:

Container(
  width: Adapt.px(375),
  height: Adapt.px(750),
  color: Colors.blue,
  child: Text(
    '设备宽度:${Adapt.px(375)}dp',
    style: TextStyle(
      color: Colors.white,
      fontSize: 24,
    ),
  ),
),
  • Adapt.px(100) 计算适配后的尺寸
  • Adapt.onepx() 1px像素大小

参考: Flutter开发:手机屏幕适配(自适应)方案

flutter_screenutil

通过 flutter_screenutil 包进行 flutter 屏幕适配

安装:

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2
  # 新添加的依赖
  flutter_screenutil: ^0.4.2
dev_dependencies:
  flutter_test:
    sdk: flutter

示例:

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter_ScreenUtil',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'FlutterScreenUtil Demo'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  
  _MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  
  Widget build(BuildContext context) {
    // 设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          print('ScreenUtil.getInstance().width:${ScreenUtil.getInstance().width}');
          print('ScreenUtil().width:${ScreenUtil().width}');
        },
        child: Icon(Icons.accessible_forward),
      ),
      body: new Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: ScreenUtil.getInstance().setWidth(375),
                  height: ScreenUtil.getInstance().setHeight(750),
                  color: Colors.red,
                  child: Text(
                    '设备宽度:${ScreenUtil.getInstance().setWidth(375)}dp',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: ScreenUtil.getInstance().setSp(24),
                    ),
                  ),
                ),
                Container(
                  width: ScreenUtil.getInstance().setWidth(375),
                  height: ScreenUtil.getInstance().setHeight(750),
                  color: Colors.blue,
                  child: Text(
                    '设备宽度:${ScreenUtil.getInstance().setWidth(375)}dp',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: ScreenUtil(allowFontScaling: true).setSp(24),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

相关API:

  • ScreenUtil.screenWidth 设备宽度
  • ScreenUtil.screenHeight 设备高度
  • ScreenUtil.pixelRatio 设备的像素密度
  • ScreenUtil.bottomBarHeight 底部安全区距离
  • ScreenUtil.statusBarHeight 状态栏高度
  • ScreenUtil.getInstance().scaleWidth 实际宽度的dp与设计稿px的比例
  • ScreenUtil.getInstance().scaleHeight 实际高度的dp与设计稿px的比例
  • ScreenUtil.textScaleFactory 系统的字体缩放比例
  • ScreenUtil.getInstance().setSp(24) 设置字体大小,不会随着系统的文字缩放比例变化
  • ScreenUtil(allowFontScaling: true).setSp(24) 设置字体大小,会随着系统的文字缩放比例变化
  • ScreenUtil.getInstance().setWidth(375) 按照设计稿宽度设置大小
  • ScreenUtil.getInstance().setHeight(375) 按照设计稿高度设置大小
  • ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio 宽度和字体相对于设计稿放大的比例
  • ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio 高度相对于设计稿放大的比例

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress