common_utils 1.0.3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 97

common_utils (Flutter常用工具类库) #

Pub

README of English #

[common_utils] Flutter常用工具类库. 如果你有好的工具类欢迎PR.

1、TimerUtil : 倒计时,定时任务.(新)
2、MoneyUtil : 分转元,支持格式输出.(新)
3、LogUtil : 简单封装打印日志.(新)
4、DateUtil : 日期转换格式化输出.
5、ScreenUtil : 获取屏幕宽、高、密度,AppBar高,状态栏高度,屏幕方向.
6、RegexUtil : 正则验证手机号,身份证,邮箱等等.
7、NumUtil : 保留x位小数.
8、WidgetUtil : 获取Widget宽高,在屏幕上的坐标.
9、ObjectUtil : 判断对象是否为空(String List Map),判断两个List是否相等.

Demo: flutter_demos.

Android扫码下载APK #

Screenshot #

<img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180920-215011.jpg" width="200">" <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180919-231618.jpg" width="200"> <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180919-224146.jpg" width="200"> <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180919-224231.jpg" width="200">

Add dependency #

dependencies:
  common_utils: x.x.x  #latest version

APIs #

  • TimerUtil #

setInterval                 : 设置Timer间隔.
setTotalTime                : 设置倒计时总时间.
startTimer()                : 启动定时Timer.
startCountDown              : 启动倒计时Timer.
updateTotalTime             : 重设倒计时总时间.
cancel                      : 取消计时器.
setOnTimerTickCallback      : 计时器回调.
isActive                    : Timer是否启动.
  • MoneyUtil #

changeF2Y                   : 分 转 元, format格式输出.
changeFStr2YWithUnit        : 分字符串 转 元, format 与 unit 格式 输出.
changeF2YWithUnit           : 分 转 元, format 与 unit 格式 输出.
changeYWithUnit             : 元, format 与 unit 格式 输出.
  • LogUtil #

init(isDebug, tag)          : isDebug: 模式, tag 标签.
e(object, tag)              : 日志e
v(object, tag)              : 日志v,只在debug模式输出.
  • NumUtil #

getIntByValueStr            : 数字字符串转int.
getDoubleByValueStr         : 数字字符串转double.
getNumByValueStr            : 保留x位小数 by 数字字符串.
getNumByValueDouble         : 保留x位小数 by double.
  • DateUtil #

enum DateFormat {
  DEFAULT, //yyyy-MM-dd HH:mm:ss.SSS
  NORMAL, //yyyy-MM-dd HH:mm:ss
  YEAR_MONTH_DAY_HOUR_MINUTE, //yyyy-MM-dd HH:mm
  YEAR_MONTH_DAY, //yyyy-MM-dd
  YEAR_MONTH, //yyyy-MM
  MONTH_DAY, //MM-dd
  MONTH_DAY_HOUR_MINUTE, //MM-dd HH:mm
  HOUR_MINUTE_SECOND, //HH:mm:ss
  HOUR_MINUTE, //HH:mm

  ZH_DEFAULT, //yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
  ZH_NORMAL, //yyyy年MM月dd日 HH时mm分ss秒  /  timeSeparate: ":" --> yyyy年MM月dd日 HH:mm:ss
  ZH_YEAR_MONTH_DAY_HOUR_MINUTE, //yyyy年MM月dd日 HH时mm分  /  timeSeparate: ":" --> yyyy年MM月dd日 HH:mm
  ZH_YEAR_MONTH_DAY, //yyyy年MM月dd日
  ZH_YEAR_MONTH, //yyyy年MM月
  ZH_MONTH_DAY, //MM月dd日
  ZH_MONTH_DAY_HOUR_MINUTE, //MM月dd日 HH时mm分  /  timeSeparate: ":" --> MM月dd日 HH:mm
  ZH_HOUR_MINUTE_SECOND, //HH时mm分ss秒
  ZH_HOUR_MINUTE, //HH时mm分
}
getNowDateMilliseconds          : 获取现在 毫秒.
getNowDateStr                   : 获取现在 日期字符串.(yyyy-MM-dd HH:mm:ss)
getDateMillisecondsByTimeStr    : 获取毫秒 By 日期字符串(Format格式输出).
getDateStrByTimeStr             : 获取日期字符串 By 日期字符串(Format格式输出).
getDateStrByMilliseconds        : 获取日期字符串 By 毫秒(Format格式输出).
getDateStrByDateTime            : 获取日期字符串 By DateTime(Format格式输出).
getWeekDay                      : 获取WeekDay By DateTime.
getZHWeekDay                    : 获取星期 By DateTime.
getWeekDayByMilliseconds        : 获取WeekDay By 毫秒.
getZHWeekDayByMilliseconds      : 获取星期 By 毫秒.
isLeapYearByYear                : 是否是闰年.
  • ScreenUtil #

screenWidth               : 获取屏幕宽.
screenHeight              : 获取屏幕高.
screenDensity             : 获取屏幕密度.
appBarHeight              : 获取系统AppBar高度.
statusBarHeight           : 获取系统状态栏高度.
getScreenWidth            : 获取当前屏幕宽.
getScreenHeight           : 获取当前屏幕高.
getOrientation            : 获取当前屏幕方向.
  • WidgetUtil #

asyncPrepare              : 监听widget宽高变化,callback返回宽高等参数.
getWidgetBounds           : 获取widget 宽高.
getWidgetLocalToGlobal    : 获取widget在屏幕上的坐标.
  • RegexUtil #

isMobileSimple            : 简单验证手机号
isMobileExact             : 精确验证手机号
isTel                     : 验证电话号码
isIDCard                  : 验证身份证号码
isIDCard15                : 验证身份证号码 15 位
isIDCard18                : 简单验证身份证号码 18 位
isIDCard18Exact           : 精确验证身份证号码 18 位
isEmail                   : 验证邮箱
isURL                     : 验证 URL
isZh                      : 验证汉字
isDate                    : 验证 yyyy-MM-dd 格式的日期校验,已考虑平闰年
isIP                      : 验证 IP 地址
  • ObjectUtil #

isEmptyString             : 判断String是否为空.
isEmptyList               : 判断List是否为空.
isEmptyMap                : 判断Map是否为空.
isEmpty                   : 判断对象是否为空.(String List Map).
isNotEmpty                : 判断对象是否非空.(String List Map).
twoListIsEqual            : 判断两个List是否相等.

Example #


// Import package
import 'package:common_utils/common_utils.dart';

//MoneyUtil example
String moneyTxt = MoneyUtil.changeFStr2YWithUnit("1160", format: MoneyFormat.NORMAL, unit: MoneyUnit.YUAN_ZH);
String moneyTxt = MoneyUtil.changeYWithUnit("1.66", unit: MoneyUnit.YUAN_ZH);

//TimerUtil example
TimerUtil timerUtil;
  //定时任务test
  timerUtil = new TimerUtil(mInterval: 1000);
  //timerUtil.setInterval(1000);
  timerUtil.setOnTimerTickCallback((int value) {
      LogUtil.e("TimerTick: " + value.toString());
  });
  timerUtil.startTimer();
  //timerUtil.cancel();
 
TimerUtil timerCountDown;
     //倒计时test
    timerCountDown = new TimerUtil(mInterval: 1000, mTotalTime: 3 * 1000);
//    timerCountDown.setInterval(1000);
//    timerCountDown.setTotalTime(3 * 1000);
    timerCountDown.setOnTimerTickCallback((int value) {
       double tick = (value / 1000);
       LogUtil.e("CountDown: " + tick.toInt().toString());
    });
    timerCountDown.startCountDown();
    //timerUtil.cancel();

//LogUtil example
LogUtil.init(isDebug: true, tag: "test");
LogUtil.e("...log...", tag: "test");
LogUtil.v("...log...", tag: "test");
    
//DateUtil example
String timeNow = DateUtil.getDateStrByDateTime(DateTime.now());//2018-09-16 23:14:56
String timeNow = DateUtil.getDateStrByDateTime(DateTime.now(),format: DateFormat.ZH_NORMAL);//2018年09月16日 23时16分15秒
String weekday = DateUtil.getWeekDay(DateTime.parse("2018-09-16"));//Sunday
String weekdayZh = DateUtil.getZHWeekDay(DateTime.parse("2018-09-16"));//星期日

//First Page init. Notice!!!
ScreenUtil.getInstance().init(context);

ScreenUtil.screenWidth
ScreenUtil.screenHeight
ScreenUtil.statusBarHeight
ScreenUtil.screenDensity

List listA = ["A", "B", "C"];
List listB = ["A", "B", "C"];
print("Two List Is Equal: " + ObjectUtil.twoListIsEqual(listA, listB).toString());

// Global variable,Reference example
WidgetUtil widgetUtil = new WidgetUtil();

@override
Widget build(BuildContext context) {
  widgetUtil.asyncPrepare(context, false, (Rect rect) {
     double width = rect.width;
     double height = rect.height;
  });
    return ;
 }

//Widgets must be rendered completely. Otherwise return Rect.zero.
Rect rect = WidgetUtil.getWidgetBounds(context);
double width = rect.width;
double height = rect.height;

//Widgets must be rendered completely. Otherwise return Offset.zero.
Offset offset = WidgetUtil.getWidgetLocalToGlobal(context);
double dx = offset.dx  
double dx = offset.dy

1.0.3 #

  • TODO: Newly added TimerUtil, MoneyUtil, LogUtil,Some updates.

1.0.2 #

  • TODO: fix bug.

1.0.1 #

  • TODO: Newly added NumUtil,Some updates.

1.0.0 #

  • TODO: Newly added DateUtil.

0.0.2 #

  • TODO: Contains ScreenUtil, WidgetUtil, ObjectUtil, RegexUtil.

0.0.1 #

  • TODO: CommonUtils initial release.

example/lib/main.dart

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

import 'package:common_utils/common_utils.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(home: new HomePage());
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _sampleUrl = "示例工程地址" +
      "\n" +
      "\n" +
      "Sample project url"
      "\n" +
      "\n" +
      "https://github.com/Sky24n/flutter_demos";
  TimerUtil timerUtil;
  TimerUtil timerCountDown;

  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  void dispose() {
    super.dispose();
    if (timerUtil != null) timerUtil.cancel();
    if (timerCountDown != null) timerCountDown.cancel();
  }

  void init() {
    /**  ----------------MoneyUtil----------------  */
    String yuan = '1.66';
    LogUtil.e(MoneyUtil.changeFStr2YWithUnit("1160",
            format: MoneyFormat.NORMAL, unit: MoneyUnit.YUAN_ZH) +
        "\n" +
        MoneyUtil.changeYWithUnit(yuan, MoneyUnit.YUAN_ZH));
    /**  ----------------MoneyUtil----------------  */

    /**  ----------------TimerUtil----------------  */
    //定时任务test
    timerUtil = new TimerUtil(mInterval: 1000);
    //timerUtil.setInterval(1000);
    timerUtil.setOnTimerTickCallback((int value) {
      LogUtil.e("TimerTick: " + value.toString());
    });
    timerUtil.startTimer();

    //倒计时test
    timerCountDown = new TimerUtil(mInterval: 1000, mTotalTime: 3 * 1000);
//    timerCountDown.setInterval(1000);
//    timerCountDown.setTotalTime(3 * 1000);
    timerCountDown.setOnTimerTickCallback((int value) {
      double tick = (value / 1000);
      LogUtil.e("CountDown: " + tick.toInt().toString());
    });
    timerCountDown.startCountDown();
    /**  ----------------TimerUtil----------------  */

    /**  ----------------ScreenUtil----------------  */
    LogUtil.e("ScreenUtil screenWidth: " +
        ScreenUtil.screenWidth.toString() +
        "   screenHeight: " +
        ScreenUtil.screenHeight.toString() +
        "   statusBarHeight: " +
        ScreenUtil.statusBarHeight.toString() +
        "   screenDensity: " +
        ScreenUtil.screenDensity.toString() +
        "");

    /**  ----------------ScreenUtil----------------  */

    /**  ----------------ObjectUtil----------------  */
    List<String> listA = ["A", "B", "C"];
    List<String> listB = ["A", "B", "C"];
    LogUtil.e("Two List Is Equal: " +
        ObjectUtil.twoListIsEqual(listA, listB).toString());
    /**  ----------------ObjectUtil----------------  */

    /**  ----------------DateUtil----------------  */
    LogUtil.e("getTimeByDateTime: " +
        DateUtil.getDateStrByDateTime(DateTime.now(),
            format: DateFormat.NORMAL, dateSeparate: "/", timeSeparate: ":") +
        "\n" +
        DateUtil.getZHWeekDay(DateTime.parse("2018-09-16")) +
        "\n" +
        DateUtil.getDateStrByDateTime(DateTime.now()) +
        "\n" +
        DateUtil.getDateStrByDateTime(DateTime.now(),
            format: DateFormat.ZH_NORMAL) +
        "\n" +
        DateUtil.getNowDateStr() +
        "");
    /**  ----------------DateUtil----------------  */

    /**  ----------------NumUtil----------------  */
    //保留小数点后2位数
    String tempStr = "1.5561111";
    double value = NumUtil.getNumByValueStr(tempStr, fractionDigits: 2);
    LogUtil.e("getDoubleByStr: " + '$value'); //result=1.56
    /**  ----------------NumUtil----------------  */

    /**  ----------------LogUtil----------------  */
    LogUtil.init(isDebug: true, tag: "test");
    LogUtil.e("...log...", tag: "test");
    LogUtil.v("...log...", tag: "test");
    /**  ----------------LogUtil----------------  */

    /**  ----------------Util----------------  */
    /**  ----------------Util----------------  */
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.getInstance().init(context);

    return new Scaffold(
        appBar: new AppBar(
          title: const Text('Common Utils'),
          centerTitle: true,
        ),
        body: new Center(
          child: new Text(
            _sampleUrl,
            textAlign: TextAlign.center,
            style: new TextStyle(fontSize: 16.0, color: Colors.blue),
          ),
        ));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  common_utils: ^1.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:common_utils/common_utils.dart';
  
Version Uploaded Documentation Archive
1.1.1 Mar 8, 2019 Go to the documentation of common_utils 1.1.1 Download common_utils 1.1.1 archive
1.1.0 Mar 7, 2019 Go to the documentation of common_utils 1.1.0 Download common_utils 1.1.0 archive
1.0.9 Oct 31, 2018 Go to the documentation of common_utils 1.0.9 Download common_utils 1.0.9 archive
1.0.8 Oct 5, 2018 Go to the documentation of common_utils 1.0.8 Download common_utils 1.0.8 archive
1.0.7 Oct 5, 2018 Go to the documentation of common_utils 1.0.7 Download common_utils 1.0.7 archive
1.0.6 Oct 4, 2018 Go to the documentation of common_utils 1.0.6 Download common_utils 1.0.6 archive
1.0.5 Oct 4, 2018 Go to the documentation of common_utils 1.0.5 Download common_utils 1.0.5 archive
1.0.4 Oct 3, 2018 Go to the documentation of common_utils 1.0.4 Download common_utils 1.0.4 archive
1.0.3 Sep 29, 2018 Go to the documentation of common_utils 1.0.3 Download common_utils 1.0.3 archive
1.0.2 Sep 28, 2018 Go to the documentation of common_utils 1.0.2 Download common_utils 1.0.2 archive

All 14 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
95
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on May 8, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.0
  • pana: 0.12.15
  • Flutter: 1.5.8

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/src/num_util.dart. (-1.49 points)

Analysis of lib/src/num_util.dart reported 3 hints:

line 1 col 1: Prefer using /// for doc comments.

line 8 col 3: Prefer using /// for doc comments.

line 19 col 3: Prefer using /// for doc comments.

Fix lib/src/log_util.dart. (-0.50 points)

Analysis of lib/src/log_util.dart reported 1 hint:

line 13 col 17: Name non-constant identifiers using lowerCamelCase.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8