Flutter / Dart:将十六进制颜色字符串转换为颜色? [英] Flutter/Dart: Convert HEX color string to Color?

查看:691
本文介绍了Flutter / Dart:将十六进制颜色字符串转换为颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的数据库将颜色保存为 String 之类的颜色,例如 #AABBCC,因此我基本上是在寻找类似这样的函数: Color。 parseColor(#AABBCC); for Flutter

Our database has colors saved as a String like "#AABBCC" and so I'm basically looking for a function like this: Color.parseColor("#AABBCC"); for Flutter

Color类需要类似 Color(0xFF42A5F5),因此我需要将#AABBCC 转换为 0xFFAABBCC

The Color class requires something like this Color(0xFF42A5F5) so I need to convert "#AABBCC" to 0xFFAABBCC

推荐答案

/// Construct a color from a hex code string, of the format #RRGGBB.
Color hexToColor(String code) {
  return new Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
}

这篇关于Flutter / Dart:将十六进制颜色字符串转换为颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆