zoukankan      html  css  js  c++  java
  • Flutter 的一些小技巧

    1. 获取状态栏高度

    import 'dart:ui';
    MediaQueryData.fromWindow(window).padding.top

    2. 设置AppBar的高度

    Scaffold( 
            appBar: PreferredSize(
            child: AppBar(
            ),
            preferredSize: Size.fromHeight(screenSize.height * 0.07))
    );

    3. 系统默认的AppBar、TabBar高度

    Dart Packages/flutter/src/material/constans.dart

    /// The height of the toolbar component of the [AppBar].
    const double kToolbarHeight = 56.0;
    
    /// The height of the bottom navigation bar.
    const double kBottomNavigationBarHeight = 56.0;
    
    /// The height of a tab bar containing text.
    const double kTextTabBarHeight = 48.0;
    
    /// The amount of time theme change animations should last.
    const Duration kThemeChangeDuration = Duration(milliseconds: 200);
    
    /// The radius of a circular material ink response in logical pixels.
    const double kRadialReactionRadius = 20.0;
    
    /// The amount of time a circular material ink response should take to expand to its full size.
    const Duration kRadialReactionDuration = Duration(milliseconds: 100);
    
    /// The value of the alpha channel to use when drawing a circular material ink response.
    const int kRadialReactionAlpha = 0x1F;
    
    /// The duration of the horizontal scroll animation that occurs when a tab is tapped.
    const Duration kTabScrollDuration = Duration(milliseconds: 300);
    
    /// The horizontal padding included by [Tab]s.
    const EdgeInsets kTabLabelPadding = EdgeInsets.symmetric(horizontal: 16.0);
    
    /// The padding added around material list items.
    const EdgeInsets kMaterialListPadding = EdgeInsets.symmetric(vertical: 8.0);

    4. 获取当前时间戳

    var now = new DateTime.now();
    print(now.millisecondsSinceEpoch); //单位毫秒,13位时间戳
    
    /** 或者 */
    /** 返回当前时间戳 */
      static int currentTimeMillis() {
        return new DateTime.now().millisecondsSinceEpoch;
      }

    5.时间戳转化成日期

    var now = new DateTime.now();
    var a=now.millisecondsSinceEpoch;  //时间戳
    print(DateTime.fromMillisecondsSinceEpoch(a));

    6. 获取控件大小和相对屏幕位置

    1.首先先需要对控件进行渲染
    初始化GlobalKey :GlobalKey anchorKey = GlobalKey();
    
    2.在需要测量的控件的下面添加key:
    child: Text("点击弹出悬浮窗",
      style: TextStyle(fontSize: 20),
      key: anchorKey
    ),
    
    3.获取控件的坐标:
    RenderBox renderBox = anchorKey.currentContext.findRenderObject();
    var offset =  renderBox.localToGlobal(Offset.zero);
    
    控件的横坐标:offset.dx
    控件的纵坐标:offset.dy
    
    如果想获得控件正下方的坐标:
     RenderBox renderBox = anchorKey.currentContext.findRenderObject();
     var offset =  renderBox.localToGlobal(Offset(0.0, renderBox.size.height));
    
       控件下方的横坐标:offset.dx
       控件下方的纵坐标:offset.dy
    
    4.获取控件的大小:
    RenderBox renderBox = anchorKey.currentContext.findRenderObject();
    final size = renderBox.size;

     7.有网络请求的地方基本上就有md5

    dart有内置的md5加密包,先引入头文件:

    import 'dart:convert';
    import 'package:convert/convert.dart';
    import 'package:crypto/crypto.dart';

    md5加密方法:

    // md5 加密
    String generateMd5(String data) {
      var content = new Utf8Encoder().convert(data);
      var digest = md5.convert(content);
      // 这里其实就是 digest.toString()
      return hex.encode(digest.bytes);
    }

    8. flutter 引入图片资源

    可以单个图片引入,也可以整个文件夹引入

  • 相关阅读:
    纯JS实现俄罗斯方块,打造属于你的游戏帝国
    Java 集合框架
    项目(1-2)ES32获取mpu9250传入数据库
    项目(1-1)ES32获取mpu9250数据网页交互显示
    开发(三)ESP32 硬件配置
    开发(二) ardunio批量固件上传地址
    项目(1-1)人脸识别
    海康相机开发(1) SDK安装和开发
    ARDUNIO IMU processing姿态数据可视化
    ESP8266 tcp透传AP+STA
  • 原文地址:https://www.cnblogs.com/joe235/p/11498060.html
Copyright © 2011-2022 走看看