「Flutter/キャスト」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→色と文字列) |
|||
| 行1: | 行1: | ||
| + | ==数字と文字== | ||
| + | ===数字から文字=== | ||
| + | int num; | ||
| + | try { | ||
| + | num = int.parse(text.toString()); | ||
| + | } catch (exception) { | ||
| + | num = 1; | ||
| + | } | ||
| + | ===文字から数字=== | ||
| + | |||
==色と文字列== | ==色と文字列== | ||
参考:https://stackoverflow.com/questions/49835146/how-to-convert-flutter-color-to-string-and-back-to-a-color | 参考:https://stackoverflow.com/questions/49835146/how-to-convert-flutter-color-to-string-and-back-to-a-color | ||
2019年11月14日 (木) 14:34時点における版
数字と文字
数字から文字
int num; try {
num = int.parse(text.toString());
} catch (exception) {
num = 1;
}
文字から数字
色と文字列
Color color = new Color(0xff443a49);
String colorString = color.toString(); // Color(0xff443a49)
String valueString = colorString.split('(0x')[1].split(')')[0]; // ff443a49
int value = int.parse(valueString, radix: 16);
Color otherColor = new Color(value);
メソッドにした時
String str = toStrByColor(new Color(0xff443a49));
Color color = toColorByStr("443a49");
static String toStrByColor(Color color) {
return color.toString().split('(0x')[1].split(')')[0];
}
static Color toColorByStr(String str) {
int value = int.parse(str, radix: 16);
return new Color(value);
}
