util.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. class Util {
  4. Color stringToColor(String str) {
  5. if (str == null || str.length == 0 || str.length != 9) {
  6. return Colors.white;
  7. }
  8. String strColor = str.substring(1, 9);
  9. int value = int.parse(strColor, radix: 16);
  10. return Color(value);
  11. }
  12. void setPageTitle(String title){
  13. SystemChrome.setApplicationSwitcherDescription(ApplicationSwitcherDescription(label: title));
  14. }
  15. Size boundingTextSize(String text,TextStyle style,double maxWidth,{int maxLines = 100}){
  16. TextPainter textPainter = TextPainter(
  17. textDirection: TextDirection.ltr,
  18. text: TextSpan(text: text,style: style), maxLines: maxLines
  19. )..layout(maxWidth: maxWidth);
  20. return textPainter.size;
  21. }
  22. bool didExceedMaxLines(String text,TextStyle style,double width,int maxLines){
  23. TextPainter _textPainter = TextPainter(
  24. maxLines: maxLines,
  25. text: TextSpan(
  26. text: text, style: style),
  27. textDirection: TextDirection.ltr)..layout(
  28. maxWidth: width
  29. );
  30. if (_textPainter.didExceedMaxLines){
  31. return true;
  32. }
  33. return false;
  34. }
  35. }