init
This commit is contained in:
19
lib/utils/angle.dart
Normal file
19
lib/utils/angle.dart
Normal file
@ -0,0 +1,19 @@
|
||||
import 'dart:math';
|
||||
|
||||
/// 角度处理工具类
|
||||
class RadianUtils{
|
||||
|
||||
///弧度是角的度量单位 单位缩写是rad 360°角=2π弧度
|
||||
///在Flutter中,π 使用 [pi] 来表示 1弧度约为57.3°,1°为π/180弧度
|
||||
///弧度换算成角度 参数[radian]为弧度
|
||||
static double radianToAngle(double radian) {
|
||||
return radian * 180 / (pi);
|
||||
}
|
||||
|
||||
|
||||
///角度换算成弧度 参数[angle]为角度
|
||||
static double angleToRadian(double angle) {
|
||||
return angle * pi / 180;
|
||||
}
|
||||
|
||||
}
|
||||
25
lib/utils/color.dart
Normal file
25
lib/utils/color.dart
Normal file
@ -0,0 +1,25 @@
|
||||
import 'dart:ui';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
//调用的时候需要把hex改一下,比如#223344 needs change to 0xFF223344
|
||||
//即把#换成0xFF即可
|
||||
|
||||
MaterialColor createMaterialColor(Color color) {
|
||||
List strengths = <double>[.05];
|
||||
Map swatch = <int, Color>{};
|
||||
final int r = color.red, g = color.green, b = color.blue;
|
||||
|
||||
for (int i = 1; i < 10; i++) {
|
||||
strengths.add(0.1 * i);
|
||||
}
|
||||
strengths.forEach((strength) {
|
||||
final double ds = 0.5 - strength;
|
||||
swatch[(strength * 1000).round()] = Color.fromRGBO(
|
||||
r + ((ds < 0 ? r : (255 - r)) * ds).round(),
|
||||
g + ((ds < 0 ? g : (255 - g)) * ds).round(),
|
||||
b + ((ds < 0 ? b : (255 - b)) * ds).round(),
|
||||
1,
|
||||
);
|
||||
});
|
||||
return MaterialColor(color.value, swatch);
|
||||
}
|
||||
5
lib/utils/data_parse.dart
Normal file
5
lib/utils/data_parse.dart
Normal file
@ -0,0 +1,5 @@
|
||||
import 'package:fengshui_compass/utils/recv_parse.dart';
|
||||
|
||||
|
||||
|
||||
|
||||
163
lib/utils/navigator_utils.dart
Normal file
163
lib/utils/navigator_utils.dart
Normal file
@ -0,0 +1,163 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 创建人: Created by zhaolong
|
||||
/// 创建时间:Created by on 2020/12/9.
|
||||
///
|
||||
/// 可关注公众号:我的大前端生涯 获取最新技术分享
|
||||
/// 可关注网易云课堂:https://study.163.com/instructor/1021406098.htm
|
||||
/// 可关注博客:https://blog.csdn.net/zl18603543572
|
||||
///
|
||||
|
||||
class NavigatorUtils {
|
||||
///普通打开页面的方法
|
||||
///[context] 上下文对象
|
||||
///[targPage] 目标页面
|
||||
///[isReplace] 是否替换当前页面 A -B
|
||||
static void pushPage({
|
||||
BuildContext context,
|
||||
Widget targPage,
|
||||
bool isReplace = false,
|
||||
Function(dynamic value) dismissCallBack,
|
||||
}) {
|
||||
PageRoute pageRoute;
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
pageRoute = MaterialPageRoute(builder: (BuildContext context) {
|
||||
return targPage;
|
||||
});
|
||||
} else {
|
||||
pageRoute = CupertinoPageRoute(builder: (BuildContext context) {
|
||||
return targPage;
|
||||
});
|
||||
}
|
||||
|
||||
if (isReplace) {
|
||||
Navigator.of(context).pushReplacement(pageRoute).then((value) {
|
||||
if (dismissCallBack != null) {
|
||||
dismissCallBack(value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Navigator.of(context).push(pageRoute).then((value) {
|
||||
if (dismissCallBack != null) {
|
||||
dismissCallBack(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
///普通打开页面的方法
|
||||
///[context] 上下文对象
|
||||
///[targPage] 目标页面
|
||||
///[isReplace] 是否替换当前页面 A -B
|
||||
///[opaque] 是否以背景透明的方式打开页面
|
||||
static void pushPageByFade({
|
||||
BuildContext context,
|
||||
Widget targPage,
|
||||
bool isReplace = false,
|
||||
int startMills = 400,
|
||||
bool opaque = false,
|
||||
Function(dynamic value) dismissCallBack,
|
||||
}) {
|
||||
PageRoute pageRoute = PageRouteBuilder(
|
||||
//背景透明 方式打开新的页面
|
||||
opaque: opaque,
|
||||
pageBuilder: (BuildContext context, Animation<double> animation,
|
||||
Animation<double> secondaryAnimation) {
|
||||
return targPage;
|
||||
},
|
||||
transitionDuration: Duration(milliseconds: startMills),
|
||||
//动画
|
||||
transitionsBuilder: (BuildContext context, Animation<double> animation,
|
||||
Animation<double> secondaryAnimation, Widget child) {
|
||||
return FadeTransition(
|
||||
opacity: animation,
|
||||
child: child,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (isReplace) {
|
||||
Navigator.of(context).pushReplacement(pageRoute).then((value) {
|
||||
if (dismissCallBack != null) {
|
||||
dismissCallBack(value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Navigator.of(context).push(pageRoute).then((value) {
|
||||
if (dismissCallBack != null) {
|
||||
dismissCallBack(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
///中间缩放的形式打开页面
|
||||
/// 开源项目 Flutter-HO 早起的年轻人
|
||||
/// github https://github.com/zhaolongs/flutter-ho
|
||||
static void pushPageByCenterScale({
|
||||
BuildContext context,
|
||||
Widget targPage,
|
||||
bool isReplace = false,
|
||||
int startMills = 400,
|
||||
int reversMills = 400,
|
||||
bool opaque = false,
|
||||
Function(dynamic value) dismissCallBack,
|
||||
}) {
|
||||
if (isReplace) {
|
||||
Navigator.of(context)
|
||||
.pushReplacement(
|
||||
_createRoute(context, targPage, startMills, reversMills))
|
||||
.then((value) {
|
||||
if (dismissCallBack != null) {
|
||||
dismissCallBack(value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Navigator.of(context)
|
||||
.push(_createRoute(context, targPage, startMills, reversMills))
|
||||
.then((value) {
|
||||
if (dismissCallBack != null) {
|
||||
dismissCallBack(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static Route _createRoute(BuildContext parentContext, Widget targPage,
|
||||
int startMills, int reversMills) {
|
||||
return PageRouteBuilder<void>(
|
||||
pageBuilder: (context, animation, secondaryAnimation) {
|
||||
return targPage;
|
||||
},
|
||||
transitionDuration: Duration(milliseconds: startMills),
|
||||
reverseTransitionDuration: Duration(milliseconds: reversMills),
|
||||
transitionsBuilder: (context, animation, secondaryAnimation, child) {
|
||||
var rectAnimation = _createTween(parentContext)
|
||||
.chain(CurveTween(curve: Curves.ease))
|
||||
.animate(animation);
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
PositionedTransition(rect: rectAnimation, child: child),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static Tween<RelativeRect> _createTween(BuildContext context) {
|
||||
var windowSize = MediaQuery.of(context).size;
|
||||
var box = context.findRenderObject() as RenderBox;
|
||||
var rect = box.localToGlobal(Offset.zero) & box.size;
|
||||
var relativeRect = RelativeRect.fromSize(rect, windowSize);
|
||||
|
||||
return RelativeRectTween(
|
||||
begin: relativeRect,
|
||||
end: RelativeRect.fill,
|
||||
);
|
||||
}
|
||||
}
|
||||
23
lib/utils/recv_parse.dart
Normal file
23
lib/utils/recv_parse.dart
Normal file
@ -0,0 +1,23 @@
|
||||
// 处理串口接收字节数组转换
|
||||
String intToHex(int i, {int pad = 2}) {
|
||||
return i.toRadixString(16).padLeft(pad, '0').toUpperCase();
|
||||
}
|
||||
|
||||
List<int> hexToUnits(String hexStr, {int combine = 2}) {
|
||||
hexStr = hexStr.replaceAll(" ", "");
|
||||
List<int> hexUnits = [];
|
||||
for (int i = 0; i < hexStr.length; i += combine) {
|
||||
hexUnits.add(hexToInt(hexStr.substring(i, i + combine)));
|
||||
}
|
||||
return hexUnits;
|
||||
}
|
||||
|
||||
int hexToInt(String hex) {
|
||||
return int.parse(hex, radix: 16);
|
||||
}
|
||||
|
||||
String formatReceivedData(recv) {
|
||||
return recv
|
||||
.map((List<int> char) => char.map((c) => intToHex(c)).join())
|
||||
.join();
|
||||
}
|
||||
91
lib/utils/sp_utils.dart
Normal file
91
lib/utils/sp_utils.dart
Normal file
@ -0,0 +1,91 @@
|
||||
///lib/utils/sp_utils.dart
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class SPUtil {
|
||||
///静态实例
|
||||
static SharedPreferences _sharedPreferences;
|
||||
|
||||
///应用启动时需要调用
|
||||
///初始化
|
||||
static Future<bool> init() async {
|
||||
_sharedPreferences = await SharedPreferences.getInstance();
|
||||
return true;
|
||||
}
|
||||
|
||||
//清除数据
|
||||
static void remove(String key) async {
|
||||
if (_sharedPreferences.containsKey(key)) {
|
||||
_sharedPreferences.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
// 异步保存基本数据类型
|
||||
static Future save(String key, dynamic value) async {
|
||||
if (value is String) {
|
||||
_sharedPreferences.setString(key, value);
|
||||
} else if (value is bool) {
|
||||
_sharedPreferences.setBool(key, value);
|
||||
} else if (value is double) {
|
||||
_sharedPreferences.setDouble(key, value);
|
||||
} else if (value is int) {
|
||||
_sharedPreferences.setInt(key, value);
|
||||
} else if (value is List<String>) {
|
||||
_sharedPreferences.setStringList(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
// 异步读取
|
||||
static Future<String> getString(String key) async {
|
||||
return _sharedPreferences.getString(key);
|
||||
}
|
||||
|
||||
static Future<int> getInt(String key) async {
|
||||
return _sharedPreferences.getInt(key);
|
||||
}
|
||||
|
||||
static Future<bool> getBool(String key) async {
|
||||
return _sharedPreferences.getBool(key);
|
||||
}
|
||||
|
||||
static Future<double> getDouble(String key) async {
|
||||
return _sharedPreferences.getDouble(key);
|
||||
}
|
||||
|
||||
///保存自定义对象
|
||||
static Future saveObject(String key, dynamic value) async {
|
||||
///通过 json 将Object对象编译成String类型保存
|
||||
_sharedPreferences.setString(key, json.encode(value));
|
||||
}
|
||||
|
||||
///获取自定义对象
|
||||
///返回的是 Map<String,dynamic> 类型数据
|
||||
static dynamic getObject(String key) {
|
||||
String _data = _sharedPreferences.getString(key);
|
||||
if (_data == null) {
|
||||
return null;
|
||||
}
|
||||
return (_data.isEmpty) ? null : json.decode(_data);
|
||||
}
|
||||
|
||||
///保存列表数据
|
||||
static Future<bool> putObjectList(String key, List<Object> list) {
|
||||
///将Object的数据类型转换为String类型
|
||||
List<String> _dataList = list?.map((value) {
|
||||
return json.encode(value);
|
||||
})?.toList();
|
||||
return _sharedPreferences.setStringList(key, _dataList);
|
||||
}
|
||||
|
||||
///获取对象集合数据
|
||||
///返回的是List<Map<String,dynamic>>类型
|
||||
static List<Map> getObjectList(String key) {
|
||||
if (_sharedPreferences == null) return null;
|
||||
List<String> dataLis = _sharedPreferences.getStringList(key);
|
||||
return dataLis?.map((value) {
|
||||
Map _dataMap = json.decode(value);
|
||||
return _dataMap;
|
||||
})?.toList();
|
||||
}
|
||||
}
|
||||
31
lib/utils/token_helper.dart
Normal file
31
lib/utils/token_helper.dart
Normal file
@ -0,0 +1,31 @@
|
||||
import 'package:fengshui_compass/utils/sp_utils.dart';
|
||||
|
||||
import '../models/login_bean.dart';
|
||||
|
||||
class TokenHelper {
|
||||
TokenHelper._();
|
||||
|
||||
static TokenHelper getInstance = TokenHelper._();
|
||||
|
||||
LoginBean _loginBean;
|
||||
|
||||
bool get isLogin => _loginBean != null;
|
||||
|
||||
set loginBean(LoginBean bean) {
|
||||
_loginBean = bean;
|
||||
SPUtil.saveObject("token_bean", _loginBean);
|
||||
}
|
||||
|
||||
void init() {
|
||||
Map<String, dynamic> map = SPUtil.getObject("token_bean");
|
||||
if (map != null) {
|
||||
//加载缓存
|
||||
_loginBean = LoginBean.fromMap(map);
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_loginBean = null;
|
||||
SPUtil.remove("token_bean");
|
||||
}
|
||||
}
|
||||
31
lib/utils/user_helper.dart
Normal file
31
lib/utils/user_helper.dart
Normal file
@ -0,0 +1,31 @@
|
||||
import 'package:fengshui_compass/models/user_bean.dart';
|
||||
import 'package:fengshui_compass/utils/sp_utils.dart';
|
||||
|
||||
class UserHelper {
|
||||
UserHelper._();
|
||||
|
||||
static UserHelper getInstance = UserHelper._();
|
||||
|
||||
UserBean _userBean;
|
||||
|
||||
bool get isLogin => _userBean != null;
|
||||
|
||||
set userBean(UserBean bean) {
|
||||
_userBean = bean;
|
||||
SPUtil.saveObject("user_bean", _userBean);
|
||||
}
|
||||
|
||||
void init() {
|
||||
Map<String, dynamic> map = SPUtil.getObject("user_bean");
|
||||
if (map != null) {
|
||||
//加载缓存
|
||||
_userBean = UserBean.fromMap(map);
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_userBean = null;
|
||||
SPUtil.remove("user_bean");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user