Snapshot do README original do repositório-fonte vinculado. Consulte o repositório para o conteúdo mais recente.
Language: English | 中文简体
Flutter常用工具类库
flustars依赖于Dart常用工具类库common_utils ,以及对其他第三方库封装,致力于为大家分享简单易用工具类。如果你有好的工具类欢迎PR.
目前包含SharedPreferences Util, Screen Util, Directory Util, Widget Util, Image Util。
[✓] Flutter (Channel stable, v2.0.0, locale zh-Hans-CN)
Pub
dependencies:
flustars: ^2.0.1
sp_util: ^2.0.3
v2.0.0
Migrate to null-safety.
v0.3.3
分拆sp_util 成单独的库,可以直接引用
common_utils 新版本v1.2.0。
如果项目中使用了 flustars: ^0.2.6及以上版本。
删除pubspec.lock文件,直接运行flutter packages get 即可使用最新版!
common_utils v1.2.0
1、新增JsonUtil。
2、新增EncryptUtil 简单加解密。
3、LogUtil 更新。
String objStr = "{\"name\":\"成都市\"}" ;
City hisCity = JsonUtil.getObj(objStr, (v) => City.fromJson(v));
String listStr = "[{\"name\":\"成都市\"}, {\"name\":\"北京市\"}]" ;
List<City> cityList = JsonUtil.getObjList(listStr, (v) => City.fromJson(v));
const String key = '11, 22, 33, 44, 55, 66' ;
String value = 'Sky24n' ;
String encode = EncryptUtil.xorBase64Encode(value, key); // WH1YHgMs
String decode = EncryptUtil.xorBase64Decode(encode, key); // Sky24n
//超长log查看
common_utils e — — — — — — — — — — — — — — — — st — — — — — — — — — — — — — — — —
common_utils e | 0 ,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
common_utils e | 7 ,988,989,990,991,992,993,994,995,996,997,998,999,
common_utils e — — — — — — — — — — — — — — — — ed — — — — — — — — — — — — — — — —
1、SpUtil : 单例"同步"SharedPreferences工具类。支持get传入默认值,支持存储对象,支持存储对象数组。
2、ScreenUtil : 屏幕适配,获取屏幕宽、高、密度,AppBar高,状态栏高度,屏幕方向.
3、WidgetUtil : 监听Widget渲染状态,获取Widget宽高,在屏幕上的坐标,获取网络/本地图片尺寸.
4、DioUtil : 单例Dio网络工具类(已迁移至此处DioUtil )。
5、ImageUtil : 获取网络/本地图片尺寸.
1、TimelineUtil : 时间轴.(新)
2、TimerUtil : 倒计时,定时任务.(新)
3、MoneyUtil : 精确转换,元转分,分转元,支持格式输出.(新)
4、LogUtil : 简单封装打印日志.(新)
5、DateUtil : 日期转换格式化输出.
6、RegexUtil : 正则验证手机号,身份证,邮箱等等.
7、NumUtil : 保留x位小数, 精确加、减、乘、除, 防止精度丢失.
8、ObjectUtil : 判断对象是否为空(String List Map),判断两个List是否相等.
9、TextUtil : TextUtil.
10、EncryptUtil : EncryptUtil.
11、JsonUtil : JsonUtil.
APIs
getObj
getObjList
putObject
getObject
putObjectList
getObjectList
getString
putString
getBool
putBool
getInt
putInt
getDouble
putDouble
getStringList
putStringList
getDynamic
haveKey
getKeys
remove
clear
isInitialized
void main () async {
WidgetsFlutterBinding .ensureInitialized ();
await SpUtil .getInstance ();
runApp (MyApp ());
}
class MyAppState extends State <MyApp > {
@override
void initState () {
super .initState ();
SpUtil .putString ("username" , "Sky24n" );
String userName = SpUtil .getString ("username" );
print ("MyHomePage userName: " + userName);
bool isFirst = SpUtil .getBool ("userName" , defValue: true );
SpUtil .putBool ("isFirst" , false );
print ("MyHomePage isFirst: $isFirst " );
City city = new City ();
city.name = "成都市" ;
SpUtil .putObject ("loc_city" , city);
City hisCity = SpUtil .getObj ("loc_city" , (v) => City .fromJson (v));
print ("City: " + (hisCity == null ? "null" : hisCity.toString ()));
List <City > list = new List ();
list.add (new City (name: "成都市" ));
list.add (new City (name: "北京市" ));
SpUtil .putObjectList ("loc_city_list" , list);
List <City > _cityList = SpUtil .getObjList ("loc_city_list" , (v) => City .fromJson (v));
print ("City list: " + (_cityList == null ? "null" : _cityList.toString ()));
}
@override
Widget build (BuildContext context) {
return MaterialApp ();
}
}
getWidth : 返回根据屏幕宽适配后尺寸.
getHeight : 返回根据屏幕高适配后尺寸.
getWidthPx : 返回根据屏幕宽适配后尺寸.
getHeightPx : 返回根据屏幕高适配后尺寸.
getSp : 返回根据屏幕宽适配后字体尺寸.
screenWidth : 获取屏幕宽.
screenHeight : 获取屏幕高.
screenDensity : 获取屏幕密度.
appBarHeight : 获取系统AppBar 高度.
statusBarHeight : 获取系统状态栏高度.
getScreenW (ctx) : 当前屏幕 宽.
getScreenH (ctx) : 当前屏幕 高.
getStatusBarH (ctx) : 当前状态栏高度.
getBottomBarH (ctx) : 当前BottomBar 高度.
getScaleW (ctx,size) : 返回根据屏幕宽适配后尺寸.
getScaleH (ctx,size) : 返回根据屏幕高适配后尺寸.
getScaleSp (ctx,size) : 返回根据屏幕宽适配后字体尺寸.
getScaleSp (ctx,size) : 返回根据屏幕宽适配后字体尺寸.
getAdapterSize (size) : 返回适配后尺寸,可用于宽,高,字体尺寸.
getAdapterSizeCtx (ctx,size) : 返回适配后尺寸,可用于宽,高,字体尺寸.
double adapterSize = ScreenUtil .getInstance ().getAdapterSize (100 );
double adapterSize = ScreenUtil .getAdapterSizeCtx (context, 100 );
一、不依赖context
double screenWidth = ScreenUtil .getInstance ().screenWidth;
double screenHeight = ScreenUtil .getInstance ().screenHeight;
double screenDensity = ScreenUtil .getInstance ().screenDensity;
double statusBarHeight = ScreenUtil .getInstance ().statusBarHeight;
double bottomBarHeight = ScreenUtil .getInstance ().bottomBarHeight;
double appBarHeight = ScreenUtil .getInstance ().appBarHeight;
double adapterW100 = ScreenUtil .getInstance ().getWidth (100 );
double adapterH100 = ScreenUtil .getInstance ().getHeight (100 );
double adapterSp100 = ScreenUtil .getInstance ().getSp (100 );
double adapterW100px = ScreenUtil .getInstance ().getWidthPx (300 );
double adapterH100px = ScreenUtil .getInstance ().getHeightPx (300 );
二、依赖context
double screenWidth = ScreenUtil .getScreenW (context);
double screenHeight = ScreenUtil .getScreenH (context);
double screenDensity = ScreenUtil .getScreenDensity (context);
double statusBarHeight = ScreenUtil .getStatusBarH (context);
double bottomBarHeight = ScreenUtil .getBottomBarH (context);
double adapterW100 = ScreenUtil .getScaleW (context, 100 );
double adapterH100 = ScreenUtil .getScaleH (context, 100 );
double adapterSp100 = ScreenUtil .getScaleSp (context, 100 );
Orientation orientation = ScreenUtil .getOrientation (context);
setInitDir
initTempDir
initAppDocDir
initAppSupportDir
initStorageDir
createDirSync
createDir
getTempPath
getAppDocPath
getAppSupportPath
getStoragePath
createTempDirSync
createAppDocDirSync
createStorageDirSync
createTempDir
createAppDocDir
createStorageDir
await DirectoryUtil .getInstance ();
String tempPath = DirectoryUtil .getTempPath (
category: 'Pictures' , fileName: 'demo' , format: 'png' );
print ("thll tempPath: $tempPath " );
String appDocPath = DirectoryUtil .getAppDocPath (
category: 'Pictures' , fileName: 'demo' , format: 'png' );
print ("thll appDocPath: $appDocPath " );
String appSupportPath = DirectoryUtil .getAppSupportPath (
category: 'Pictures' , fileName: 'demo' , format: 'png' );
print ("thll appSupportPath: $appSupportPath " );
String storagePath = DirectoryUtil .getStoragePath (
category: 'Pictures' , fileName: 'demo' , format: 'png' );
print ("thll storagePath: $storagePath " );
asyncPrepare : Widget 渲染监听,监听widget宽高变化,callback返回宽高等参数.
getWidgetBounds : 获取widget 宽高.
getWidgetLocalToGlobal : 获取widget在屏幕上的坐标.
getImageWH : 获取图片宽高,加载错误情况返回 Rect .zero.(单位 px).
getImageWHE : 获取图片宽高,加载错误会抛出异常.(单位 px).
WidgetUtil widgetUtil = new WidgetUtil ();
widgetUtil.asyncPrepare (context, true , (Rect rect) {
});
Rect rect = WidgetUtil .getWidgetBounds (context);
Offset offset = WidgetUtil .getWidgetLocalToGlobal (context);
Image image = new Image (image: new CachedNetworkImageProvider ("Url" ));
Rect rect1 = await WidgetUtil .getImageWH (image: image);
Image imageAsset = new Image .asset ("" );
Image imageFile = new Image .file (File ("path" ));
Image imageNetwork = new Image .network ("url" );
Image imageMemory = new Image .memory (null );
Rect rect2 = await WidgetUtil .getImageWH (url: "Url" );
Rect rect3 = await WidgetUtil .getImageWH (localUrl: "assets/images/3.0x/ali_connors.png" );
WidgetUtil .getImageWH (url: "Url" ).then ((Rect rect) {
print ("rect: " + rect.toString ();
});
WidgetUtil .getImageWHE (url: "Url" ).then ((Rect rect) {
print ("rect: " + rect.toString ();
}).catchError ((error) {
print ("rect: " + error.toString ();
});
getImageWH
DioUtil .openDebug ();
Options options = DioUtil .getDefOptions ();
options.baseUrl = "http://www.wanandroid.com/" ;
HttpConfig config = new HttpConfig (options: options);
DioUtil ().setConfig (config);
DioUtil ().request<List >(Method .get , "banner/json" );
DioUtil .getInstance ().request (Method .get , "banner/json" );
LoginReq req = new LoginReq ('username' , 'password' );
DioUtil ().request (Method .post, "user/login" ,data: req.toJson ());
FormData formData = new FormData .from ({
"username" : "username" ,
"password" : "password" ,
});
DioUtil ().requestR (Method .post, "user/login" ,data: rformData);
I/flutter ( 5922 ): ----------------Http Log ----------------
I/flutter ( 5922 ): [statusCode]: 200
I/flutter ( 5922 ): [request ]: method: GET baseUrl: http:
I/flutter ( 5922 ): [reqdata ]: null
I/flutter ( 5922 ): [response ]: {data: {curPage: 1 , datas: [], offset: 0 , over: true , pageCount: 0 , size: 20 , total: 0 }, errorCode: 0 , errorMsg: }
关于作者
GitHub : Sky24n
简书 : Sky24n
掘金 : Sky24n
项目合集 : FlutterRepos
Changelog
Please see the Changelog page to know what's recently changed.
Apps
flutter_wanandroid
Moss .
A GitHub client app developed with Flutter, which supports Android iOS Web.
Web :Flutter Web .