facebook twitter hatena line email

「Flutter/キャスト」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(色と文字列)
(小数点から整数)
 
(同じ利用者による、間の11版が非表示)
行1: 行1:
 +
==数字と文字列==
 +
===数字から文字列===
 +
<pre>
 +
int num;
 +
try {
 +
  num = int.parse(text.toString());
 +
} catch (exception) {
 +
  num = 1;
 +
}
 +
</pre>
 +
 +
===文字列から数字===
 +
String text = 10.toString();
 +
 +
==intからdoubleへ==
 +
int num = 1;
 +
double d = 1 + .0;
 +
 +
==小数点から整数==
 +
int i = 2;
 +
(i / 10).floor();
 +
or
 +
int idPer1000 = (id / 1000).floorToDouble() as int;
 +
 
==色と文字列==
 
==色と文字列==
 
参考: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
 
<pre>
 
<pre>
Color color = new Color(0x12345678);
+
Color color = new Color(0xff443a49);
String colorString = color.toString(); // Color(0x12345678)
+
String colorString = color.toString(); // Color(0xff443a49)
String valueString = colorString.split('(0x')[1].split(')')[0]; // 12345678
+
String valueString = colorString.split('(0x')[1].split(')')[0]; // ff443a49
 
int value = int.parse(valueString, radix: 16);
 
int value = int.parse(valueString, radix: 16);
 
Color otherColor = new Color(value);
 
Color otherColor = new Color(value);
 +
</pre>
 +
 +
メソッドにした時
 +
<pre>
 +
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);
 +
  }
 
</pre>
 
</pre>

2020年1月6日 (月) 01:45時点における最新版

数字と文字列

数字から文字列

int num;
try {
  num = int.parse(text.toString());
} catch (exception) {
  num = 1;
}

文字列から数字

String text = 10.toString();

intからdoubleへ

int num = 1;
double d = 1 + .0;

小数点から整数

int i = 2;
(i / 10).floor();

or

int idPer1000 = (id / 1000).floorToDouble() as int;

色と文字列

参考:https://stackoverflow.com/questions/49835146/how-to-convert-flutter-color-to-string-and-back-to-a-color

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);
  }