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 引入图片资源

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值