Flutter 的一些小技巧

Posted joe235

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 引入图片资源

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

技术图片

以上是关于Flutter 的一些小技巧的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 12 个小技巧

Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套

Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套

Flutter 小技巧之霓虹灯文本的「故障」效果的实现

C#一些小技巧

一些js小技巧