美文网首页
Flutter 常用函数

Flutter 常用函数

作者: 雨_田 | 来源:发表于2024-07-01 02:52 被阅读0次
    import 'dart:convert' as convert;
    
     ///判断为空
    bool StrIsEmpty(dynamic object) {
      if (object == null) return true;
      if (object == []) return true;
      if (object == '') return true;
      if (object == 'null') return true;
      if (object == '(null)') return true;
      if (object == '<null>') return true;
      if (object is String && object.isEmpty) {
        return true;
      } else if (object is List && object.isEmpty) {
        return true;
      } else if (object is Map && object.isEmpty) {
        return true;
      }
      return false;
    }
    ///判断为非空
    bool StrIsNotEmpty(dynamic object) {
      bool isEmpty = StrIsEmpty(object);
      return isEmpty==false;
    }
    
    
    
    //转为bool值
    bool BoolValue(dynamic obj, {bool defaultBool = false}) {
      if(obj is bool){
        return obj;
      }
      return defaultBool;
    }
    //转为字符串
    String StrValue(dynamic obj){
      if(obj==null || obj=='null' || obj=='<null>' || obj=='(null)' || obj=='[]' || obj=='{}'){
        return '';
      }
      if ((obj is int) || (obj is double) || (obj is Map) || (obj is List)){
        return obj.toString();
      }
      if (obj is bool) {
        if(obj==true){
          return 'true';
        } else {
          return 'false';
        }
      }
      if(obj is String){
        return obj;
      }
      return obj.toString();
    //String转int
    // String a = '33';
    // int b = int.parse(a);
    }
    //转为int整型字符串
    String IntegerStrValue(dynamic obj){
      if(obj==null || obj=='null' || obj=='<null>' || obj=='(null)' || obj=='[]' || obj=='{}'){
        return '0';
      }
      if (obj is bool) {
        if(obj==true){
          return '1';
        } else {
          return '0';
        }
      }
      if(obj is int) {
        return '$obj';
      }
    
      String objStr = StrValue(obj);
      //double或者double类型的字符串不能直接用int.tryParse()
      ///如dart:int ?rateParseInt = int.tryParse('4.0');===>null
      ///即不能用: int ?num = int.tryParse(objStr);
      ///可以使用num中转,将String类型转为int类型
    
      num ?resNum = num.tryParse(objStr);
      int resInt = resNum?.toInt()??0;
      return resInt.toString();
    }
    
    //转为double浮点型字符串
    String DoubleStrValue(dynamic obj){
      if(obj==null || obj=='null' || obj=='<null>' || obj=='(null)' || obj=='[]' || obj=='{}'){
        return '0';
      }
      if (obj is bool) {
        if(obj==true){
          return '1';
        } else {
          return '0';
        }
      }
      String objStr = StrValue(obj);
      double ?num = double.tryParse(objStr);
      if(num is double) {
        return num.toString();
      }
      return '0';
    }
    
    
    
    //转为价格:1,1.1, 1.01; pNum:精度
    String PriceStrValue(obj, {int pNum=2}){
      if(StrIsEmpty(obj)){
        return '0';
      }
      if(obj==null || obj=='null' || obj=='<null>' || obj=='(null)' || obj=='[]' || obj=='{}'){
        return '0';
      }
      if (obj is bool) {
        if(obj==true){
          return '1';
        } else {
          return '0';
        }
      }
    
      double p_getDoubleByValueStr(String valueStr) {
        return double.tryParse(valueStr) ?? 0;
      }
      //doubleStr:1表现的可能为1.000000000001或者0.000000000009
      //String doubleStr = DoubleStrValue(obj);
    
      //浮点型
      double objDouble = DynamicToDouble(obj);//p_getDoubleByValueStr(doubleStr);
    
      //判断负数
      bool hasFu= objDouble<0;
    
      //四舍五入保留两位小数
      String objString = '';
      if(hasFu){ //负号需要提前
        objString = "-${objDouble.abs().toStringAsFixed(pNum)}";
      } else {
        objString =objDouble.toStringAsFixed(pNum);
      }
      if(objString.contains('.')==false){
        return objString;
      }
    
      if(objString.contains('.')){
        List<String> temp=objString.split('.');
        String v0= temp[0];
        String v1 = temp[1];
    
    
        if(pNum>5){//5位以上的全显示
          return '${v0}.${v1}';
        }
        String v1_a =  v1;
    
        if(v1_a.endsWith('00000')){
          int k = v1_a.lastIndexOf('00000');
          v1_a = v1_a.substring(0,k);
        }
        if(v1_a.endsWith('0000')){
          int k = v1_a.lastIndexOf('0000');
          v1_a = v1_a.substring(0,k);
        }
        if(v1_a.endsWith('000')){
          int k = v1_a.lastIndexOf('000');
          v1_a = v1_a.substring(0,k);
        }
        if(v1_a.endsWith('00')){
          int k = v1_a.lastIndexOf('00');
          v1_a = v1_a.substring(0,k);
        }
        if(v1_a.endsWith('0')){
          int k = v1_a.lastIndexOf('0');
          v1_a = v1_a.substring(0,k);
        }
    
        if(StrIsEmpty(v1_a)){
          return v0;
        }
        return '${v0}.${v1_a}';
      }
      return objString;
    
    
    }
    
    //输入框文字去掉空白
      String TFSearchStr(dynamic txt){
      String needStr = StrValue(txt).trim();
      return needStr;
    }
    
    
    //字符串相等
    bool StrIsEqual(dynamic obj1, dynamic obj2){
      if(obj1==obj2){
        return true;
      }
      return false;
    }
    StrThreeConnect(dynamic obj1, dynamic obj2, dynamic obj3) {
      return '$obj1$obj2$obj3';
    }
    
    List JsonStrToList(dynamic obj){
      if(StrIsEmpty(obj)){
        return [];
      }
      if(!(obj is String)){
        return [];
      }
      List list = DynamicTolist(convert.jsonDecode(obj));
      return list;
    }
    Map JsonStrToMap(dynamic obj){
      if(StrIsEmpty(obj)){
        return {};
      }
      if(!(obj is String)){
        return {};
      }
      Map map = DynamicTomap(convert.jsonDecode(obj));
      return map;
    }
    
    String ObjToJsonStr(dynamic obj){
      if((obj is Map) || (obj is List)){
        return convert.jsonEncode(obj);
      }
      return StrValue(obj);
    }
    
    //将map中的key的json字符串转化为json对象
    dynamic MapJsonvalueToObject(dynamic map, String key, {int defaultType=0}){
      dynamic defaultValue = {};
      if(defaultType==0){//转为Map
        defaultValue = {};
      } else if(defaultType==1){//转为List
        defaultValue = [];
      } else if(defaultType==2){//转为String
        defaultValue = '';
      }
      if(map==null || key==null || key.length<1){
        return defaultValue;
      }
      Map myMap = map as Map;
      if(myMap.keys.length<1){
        return defaultValue;
      }
      if(myMap.containsKey(key) == false){
        return defaultValue;
      }
      if((myMap[key] is String) == false){
        return defaultValue;
      }
    
      return convert.jsonDecode(myMap[key])??defaultValue;
    }
    
    
    
    //获取map 的 key 对应 任意类型的value
    dynamic MapValueobj(dynamic map, dynamic key){
      if(!(map is Map)){
        return '';
      }
      if(StrIsEmpty(key)){
        return '';
      }
      Map myMap = map as Map;
      if(myMap.keys.isEmpty){
        return '';
      }
      if(myMap.containsKey(key) == false){
        return '';
      }
      return myMap[key];
    }
    
    //获取map 的 key 对应 List类型的value
    List MapValuelist(dynamic map, dynamic key){
    
      dynamic org = MapValueobj(map, key);
      if(org is List) {
        return org;
      }
      return [];
    }
    
    //获取map 的 key 对应 Map类型的value
    Map MapValuemap(dynamic map, dynamic key){
      dynamic org = MapValueobj(map, key);
      if(org is Map) {
        return org;
      }
      return {};
    }
    //获取map 的 key 对应 int类型的value
    int MapValueint(dynamic map, dynamic key){
      if(!(map is Map)){
        return 0;
      }
      if(StrIsEmpty(key)){
        return 0;
      }
      Map myMap = map as Map;
       if(myMap.keys.length<1){
        return 0;
      }
      if(myMap.containsKey(key) == false){
        return 0;
      }
    
      var res = myMap[key];
      if(res is int){
        return res;
      }
      if(res is double){
        return res.toInt();
      }
      if(res is String){
        //double或者double类型的字符串不能直接用int.tryParse()
        ///如dart:int ?rateParseInt = int.tryParse('4.0');===>null
        ///即不能用: int ?num = int.tryParse(objStr);
        ///可以使用num中转,将String类型转为int类型
        num ?resNum = num.tryParse(res);
        int resInt = resNum?.toInt()??0;
        return resInt;
      }
      return 0;
    }
    
    //获取map 的 key 对应 bool类型的value
    bool MapValuebool(dynamic map, dynamic key,{bool defaultBool = false}){
    
      if(!(map is Map)){
        return defaultBool;
      }
      if(StrIsEmpty(key)){
        return defaultBool;
      }
      Map myMap = map as Map;
      if(myMap.keys.length<1){
        return defaultBool;
      }
      if(myMap.containsKey(key) == false){
        return defaultBool;
      }
    
      var res = myMap[key];
      if(res is bool){
        return res;
      }
      if(res == 1){
        return true;
      }
      if(res == 0){
        return false;
      }
      return defaultBool;
    }
    //获取list数组的index下标对应的元素
    dynamic ListValueAtIndex(dynamic list, int index, {String defaultValue=''}){
      if(!(list is List && list.isNotEmpty)){
        return defaultValue;
      }
      if(index>list.length-1){
        return defaultValue;
      }
      return (list as List)[index];
    }
    
    //获取map 的 key 对应 String类型的value
    String MapValueStr(dynamic map, dynamic key){
    
      if(!(map is Map)){
    return '';
      }
      if(StrIsEmpty(key)){
        return '';
      }
      Map myMap = map as Map;
      if(myMap.keys.isEmpty){
        return '';
      }
      if(myMap.containsKey(key) == false){
        return '';
      }
      dynamic obj = myMap[key];
    
      if(obj==null || obj=='null' || obj=='<null>' || obj=='(null)' || obj=='[]' || obj=='{}'){
        return '';
      }
      if ((obj is int) || (obj is double) || (obj is Map) || (obj is List)){
        return obj.toString();
      }
      if (obj is bool) {
        if(obj==true){
          return 'true';
        } else {
          return 'false';
        }
      }
      if(obj is String){
        return obj;
      }
      return obj.toString();
    
    }
    //获取map 的 key 对应 double类型的value
    double MapValuedouble(dynamic map, dynamic key){
    
      if(!(map is Map)){
        return 0;
      }
      if(StrIsEmpty(key)){
        return 0;
      }
      Map myMap = map as Map;
      if(myMap.keys.length<1){
        return 0;
      }
      if(myMap.containsKey(key) == false){
        return 0;
      }
    
      var res = myMap[key];
      if(res is double){
        return res;
      }
      if(res is int){
        return res.toDouble();
      }
      if(res is String){
        if(double.tryParse(res) is double){
          return double.tryParse(res)??0;
        }
        return 0;
      }
      return 0;
    }
    
    //数据转化为int
    int DynamicToInt(dynamic count) {
      if (StringUtils.isEmpty(count)) {
        return 0;
      } else if (count is double) {
        return count.toInt();
      } else if (count is String) {
        //double或者double类型的字符串不能直接用int.tryParse()
        ///如dart:int ?rateParseInt = int.tryParse('4.0');===>null
        ///即不能用: int ?num = int.tryParse(objStr);
        ///可以使用num中转,将String类型转为int类型
    
        num ?resNum = num.tryParse(count);
          int resInt = resNum?.toInt()??0;
        return resInt;
    
      } else if (count is int) {
        return count;
      } else {
        return 0;
      }
    }
    //数据转化为list(隐式转为显式)
    List DynamicTolist(dynamic count) {
      if (StrIsEmpty(count)) {
        return [];
      } else if (count is List) {
        return count;
      }
      return [];
      }
    //数据转化为Map(隐式转为显式)
    Map DynamicTomap(dynamic count) {
      if (StrIsEmpty(count)) {
        return {};
      } else if (count is Map) {
        return count;
      }
      return {};
    }
    //数据转化为double
    double DynamicToDouble(dynamic count) {
      if (StringUtils.isEmpty(count)) {
        return 0;
      } else if (count is double) {
        return count;
      } else if (count is String) {
        double t = double.tryParse(count) ?? 0;
        return t;
      } else if (count is int) {
        return count.toDouble();
      } else {
        return 0;
      }
    }
    
    //数据转化为bool
    bool DynamicToBool(dynamic count, {bool defaultValue=false}) {
      if (StringUtils.isEmpty(count)) {
        return defaultValue;
      }
    
      if (count is bool) {
        return count;
      }
    
      if (count is String) {
        if(count=='true'){
          return true;
        }
        if(count=='false'){
          return false;
        }
      }
      if (count==1) {
        return true;
      }
    
      return defaultValue;
    }
    
    
    //获取map 的 key 对应逗号(splitStr)拼接的url(字符串)数组
    List MapValueUrlsByCommastr(dynamic map, dynamic key, {String splitStr = ','}){
      String imgsStr = MapValueStr(map, key);
      if(StrIsEmpty(imgsStr)){
        return [];
      }
      dynamic spliArr = imgsStr.split(splitStr);
      if(spliArr is List && (spliArr as List).isNotEmpty){
        return spliArr as List;
      }
      return [];
    }
    
    //获取map 的 key 对应Map<String, dynamic>数组的url数组
    List GetImageUrlsOfMap(dynamic infoMap, String key, {String picKey='filePath'}){
      List imageUrls = [];
      if(!StrIsEmpty(MapValueobj(infoMap, key))){
        for(dynamic obj in infoMap[key]){
          if(!StrIsEmpty(MapValueStr(obj, picKey))){
            imageUrls.add(MapValueStr(obj, picKey));
          }
        }
      }
      return imageUrls;
    }
    
    //从逗号"," 拼接的字符串中转化为包含url的数组
    List GetUrlsByStr(dynamic imgObj, {String flag=','}){
      String imgStrs = StrValue(imgObj);
      if(StrIsEmpty(imgStrs)){
        return [];
      }
      dynamic tmp = imgStrs.split(flag);
      List needArr = [];
      if(tmp is List && tmp.isNotEmpty) {
        needArr.addAll(tmp);
      }
      return needArr;
    }
    
    //从"/" 拼接的字符串中转化为包含url的数组
    List GetListByStr(dynamic imgObj, {String flag='/'}){
      String imgStrs = StrValue(imgObj);
      if(StrIsEmpty(imgStrs)){
        return [];
      }
      dynamic tmp = imgStrs.split(flag);
      List needArr = [];
      if(tmp is List && tmp.isNotEmpty) {
        needArr.addAll(tmp);
      }
      return needArr;
    }
    
    //价格金额显示处理:将字符串转为0.00的形式
    String PriceValue(dynamic obj){
      if(obj==null || obj=='null' || obj=='<null>' || obj=='(null)' || obj=='[]' || obj=='{}' || obj==''){
        return '0.00';
      }
      //int long double 等number类型都可以调用toStringAsFixed
      if ((obj is int) || (obj is double)){
        String priceStr = obj.toStringAsFixed(2);
        return priceStr;
      }
      if(obj is String){
        double price = double.tryParse(obj)??0;
        String priceStr = price.toStringAsFixed(2);
        return priceStr;
      }
      return obj.toString();
    }
    
    
    
    ///检测Map 的 某个key 对应的value不是空(空值)数组----=== MapValuelist(map,     key).isNotEmpty
    bool MapValueForKeyIsNotNilArr({required dynamic map, required String key}) {
    //return true不是空(空值)数组; false 是空;
      if(StringUtils.isEmpty(key)){
        return false;
    }
    
    if((map is Map)==false){
        return false;
      }
      if((map as Map).containsKey(key)==false){
        return false;
      }
      if(((map as Map)[key] is List)==false){
        return false;
      }
      if(((map as Map)[key] as List).length>0){
        return true;
      }
      return false;
    }
    

    相关文章

      网友评论

          本文标题:Flutter 常用函数

          本文链接:https://www.haomeiwen.com/subject/obcmcjtx.html