Flutter 适配不同的屏幕 flutter_screenutil-程序员宅基地

技术标签: flutter  

https://stackoverflow.com/questions/49704497/how-to-make-flutter-app-responsive-according-to-different-screen-size

https://jaycoding.tech/tutorials/guides/how-to-create-a-responsive-app-in-flutte-xmafdg

 

在实际的使用中,会用到flutter_screenutil 第三方插件,使用方法为

首先,在pubspec.yaml 中加入依赖:

flutter_screenutil: ^5.0.0+2

其次,在main.dart文件中加入以下代码

ScreenUtilInit( // New Widget Here!!
  designSize: Size(325, 667), // Design Size
  child: MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
      primarySwatch: Colors.blue,
      visualDensity: VisualDensity.adaptivePlatformDensity,
    ),
    home: MyHomePage(title: 'Flutter Demo Home Page'),
  ),
);

后面和大小和高度有关的可以用以下的写法:

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
    return ScreenUtilInit(
      designSize: Size(360, 690),
      builder: () => MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter_ScreenUtil',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          textTheme: TextTheme(button: TextStyle(fontSize: 45.sp)),
        ),
        builder: (context, widget) {
          return MediaQuery(
            //Setting font does not change with system font size
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: widget,
          );
        },
        home: HomePage(title: 'FlutterScreenUtil Demo'),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    printScreenInformation();
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                // Using Extensions
                Container(
                  padding: EdgeInsets.all(10.w),
                  width: 0.5.sw,
                  height: 200.h,
                  color: Colors.red,
                  child: Text(
                    'My actual width: ${0.5.sw}dp \n\n'
                    'My actual height: ${200.h}dp',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 12.sp,
                    ),
                  ),
                ),
                // Without using Extensions
                Container(
                  padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
                  width: 180.w,
                  height: 200.h,
                  color: Colors.blue,
                  child: Text(
                    'My design draft width: 180dp\n\n'
                    'My design draft height: 200dp',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.sp,
                    ),
                  ),
                ),
              ],
            ),
            Container(
              padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
              width: 100.r,
              height: 100.r,
              color: Colors.green,
              child: Text(
                'I am a square with a side length of 100',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: ScreenUtil().setSp(12),
                ),
              ),
            ),
            Text('Device width:${ScreenUtil().screenWidth}dp'),
            Text('Device height:${ScreenUtil().screenHeight}dp'),
            Text('Device pixel density:${ScreenUtil().pixelRatio}'),
            Text('Bottom safe zone distance:${ScreenUtil().bottomBarHeight}dp'),
            Text('Status bar height:${ScreenUtil().statusBarHeight}dp'),
            Text(
              'The ratio of actual width to UI design:${ScreenUtil().scaleWidth}',
              textAlign: TextAlign.center,
            ),
            Text(
              'The ratio of actual height to UI design:${ScreenUtil().scaleHeight}',
              textAlign: TextAlign.center,
            ),
            SizedBox(
              height: 10.h,
            ),
            Text('System font scaling factor:${ScreenUtil().textScaleFactor}'),
            SizedBox(height: 5),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  '16sp, will not change with the system.',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 16.sp,
                  ),
                  textScaleFactor: 1.0,
                ),
                Text(
                  '16sp,if data is not set in MediaQuery,my font size will change with the system.',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 16.sp,
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  void printScreenInformation() {
    print('Device width dp:${1.sw}dp');
    print('Device height dp:${1.sh}dp');
    print('Device pixel density:${ScreenUtil().pixelRatio}');
    print('Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}dp');
    print('Status bar height dp:${ScreenUtil().statusBarHeight}dp');
    print('The ratio of actual width to UI design:${ScreenUtil().scaleWidth}');
    print(
        'The ratio of actual height to UI design:${ScreenUtil().scaleHeight}');
    print('System font scaling:${ScreenUtil().textScaleFactor}');
    print('0.5 times the screen width:${0.5.sw}dp');
    print('0.5 times the screen height:${0.5.sh}dp');
    print('Screen orientation:${ScreenUtil().orientation}');
  }
}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/computerclass/article/details/117951938

智能推荐

关于移动端、浏览器适配解决方案优缺点对比_rem适配的优缺点_前端的一只小白的博客-程序员宅基地

以下仅供自己学习记录!一. 移动端适配问题1、使用rem进行浏览器适配:比较常用rem(fontsizeoftherootelement)是指相对于根元素的字体大小的单位。简单的说它就是一个相对单位。看到rem一定会想起em单位,em(fontsizeoftheelement)是指相对于父元素的字体大小的单位。它们之间其实很相似,只不过一个计算的规则是依赖根元素一个是..._rem适配的优缺点

遮挡目标检测持续汇总_Matorch的博客-程序员宅基地

遮挡目标检测持续汇总常常将遮挡分为目标与目标间的遮挡(Crowd)、目标与背景间的遮挡(Occlusion)文章目录遮挡目标检测持续汇总Double Anchor R-CNN for Human Detection in a CrowdBi-box Regression for Pedestrian Detection and Occlusion EstimationDetection in Crowded Scenes: One Proposal, Multiple PredictionsAdapti_遮挡目标检测

使用计算机教学的不足,高中计算机教学中存在的不足及提升途径_凯米拉诺的博客-程序员宅基地

高中计算机教学中存在的不足及提升途径计算机课程是一项应用性很强的课程,不仅需要理论知识的学习,更重要的是学生亲自进行实践,下面是小编搜集整理的一篇探究高中计算机课堂教学问题的论文范文,供大家阅读查看。引言随着科技力量的不断发展,我国的信息技术已经提升到了一个较高的水平,各行各业都对人才的计算机水平有一定的要求。因此,可以说计算机技术已经成为目前学校教育中的一项必备课程。高中阶段作为学生智力和能力提..._学习电脑存在的不足

SharePoint 2013 "通知我"功能简介_c# sharepoint 文档设置通知_霖雨的博客-程序员宅基地

功能简介  "通知我"主要是在列表或者文档库里面的项目,有添加/删除/修改等操作,发送邮件通知设置的用户的功能;可以针对列表或者文档库设置通知,也可以针对单一项目设置通知功能,是SharePoint的一个Out Of Box的功能。  使用必须首先在管理中心设置传出邮件,然后确保用户的Email属性里有邮件地址,最后配置通知即可。下面简单介绍下通知服务的配置。一、管理中心配置邮件服务_c# sharepoint 文档设置通知

教你一招520Python表白(图片,爬虫 处理)!!!_The_way_inf的博客-程序员宅基地

TIPS:程序效果:以上效果可在下面的项目中实现,视频中出现的文字可以修改,但是为了防止个别童靴的‘’拿来主义‘’这里就不说明哪里改了,自己慢慢看。文末获取完整码源。项目结构废话不多说,先来看看项目:这里先介绍一下各个模块的功能Fold_win用于生成一个窗口,主要用于最后面显示表白语句。main主控程序,负责最后的效果呈现。Image_c负责对图像进行处理,添加图片水印,设置壁纸。set_p_w获取相应的图片资源和表白的句子,主要是爬虫去爬取。这里要说明一下的是虽然爬虫会爬取相应的

最新最全论文合集——语码转换_语码转换论文_AMiner学术搜索和科技情报挖掘的博客-程序员宅基地

AMiner平台(https://www.aminer.cn)由清华大学计算机系研发,拥有我国完全自主知识产权。平台包含了超过2.3亿学术论文/专利和1.36亿学者的科技图谱,提供学者评价、专家发现、智能指派、学术地图等科技情报专业化服务。系统2006年上线,吸引了全球220个国家/地区1000多万独立IP访问,数据下载量230万次,年度访问量超过1100万,成为学术搜索和社会网络挖掘研究的重要数据和实验平台。必读论文:https://www.aminer.cn/topic论文集地址:https:/._语码转换论文

随便推点

爱分享最新文章更新_文章分享最近更新-程序员宅基地

美国人延年益寿的6个好习惯iPhone再也不怕丢,iOS7查找我的iPhone帮你找回!安全专家提醒:蕨菜蕨根粉含致癌物少吃为妙北京机场大巴启动电信天翼4G无线WIFI网络你们知道吗?北京3000多辆公交车上有WiFi无线网络如何提高炉石传说水平各位玩家总结一些心得和技巧11个最让人抓狂的游戏像素鸟真不难如何选购和鉴别真假芝麻油?注意啦!QQ群共享出现名叫女神沐浴照实为蠕虫病毒20个摄影技巧熟记于_文章分享最近更新

【Netty】Netty实例开源项目_好一点netty项目-程序员宅基地

https://blog.csdn.net/qq_26525215/article/details/81989644高质量技术免费分享https://blog.csdn.net/everyonetimeismoney/article/details/94412711_好一点netty项目

21届AsiaInfo(亚信)软件校园招聘技术技能测评_JAVA(40道题)_亚信科技在线测评40道选择题_努力找实习的博客-程序员宅基地

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar_亚信科技在线测评40道选择题

nRF52832 UARTE-程序员宅基地

由于nRF将普通UART、使用DMA的UARTE区分,实际情况下都会使用DMA,所以只学习UARTE。nRF精简的够狠毒 - -! 概述: 1. UARTE的dma最长为256个字节,有些短。 2. 如果需要自动循坏接收,Shortcut between ENDRX event and STARTRX task。 3. 波特率最高1M。115200 bps,1200 bps 功耗一样

python学习笔记——pass del_read_data python_sola_f的博客-程序员宅基地

原文地址:del" style="text-decoration:none; color:rgb(65,104,77)">python学习笔记——pass del作者:萧萧Let’s take a quick lool at two statements:pass and del 1 pass: 空语句什么也不执行,用处颇广。 if name =='Ra_read_data python