import 'dart:ui';

import 'package:circle_app/common/values/values.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Util {

}
//获取图片
String getBaseImage(String image) {
  return '${Values.base_images}${image}.png';
}

String getHomeImage(String image) {
  return '${Values.home_images}${image}.png';
}

String getLoginImage(String image) {
  return '${Values.login_images}${image}.png';
}

String getMineImage(String image) {
  return '${Values.mine_images}${image}.png';
}

String getMsgImage(String image) {
  return '${Values.msg_images}${image}.png';
}

String getTabbarImage(String image) {
  return '${Values.tabbar_images}${image}.png';
}

///弹窗文本提示 msg: 提示内容
showToast(String msg) {
  Fluttertoast.showToast(
      msg: msg,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.black54,
      textColor: Colors.white,
      fontSize: 16.0
  );
}

///value: 文本内容；fontSize : 文字的大小；fontWeight：文字权重；maxWidth：文本框的最大宽度；maxLines：文本支持最大多少行
double calculateTextHeight(String value, fontSize, FontWeight fontWeight,
    double maxWidth, int maxLines) {
  value = filterText(value);
  TextPainter painter = TextPainter(

    ///AUTO：华为手机如果不指定locale的时候，该方法算出来的文字高度是比系统计算偏小的。
      locale: WidgetsBinding.instance!.window.locale,
      maxLines: maxLines,
      textDirection: TextDirection.ltr,
      textScaleFactor: 1, //字体缩放大小
      text: TextSpan(
          text: value,
          style: TextStyle(
            fontWeight: fontWeight,
            fontSize: fontSize,
          )));
  painter.layout(maxWidth: maxWidth);

  ///文字的宽度:painter.width
  return painter.height;
}

String filterText(String text) {
  String tag = '<br>';
  while (text.contains('<br>')) {
    // flutter 算高度,单个\n算不准,必须加两个
    text = text.replaceAll(tag, '\n\n');
  }
  return text;
}