flutter - 如何在小部件中设置条件?
问题描述
我想在这段代码中设置条件 if (value==7) { Textspan(text: "JACKPOT :"+j.j7)} else { Textspan(text: "JACKPOT :"+j.j14}是 JACKPOT 写的,这段代码在 futurebuilder 中执行:
Container(
margin: EdgeInsets
.only(
bottom: 10),
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: "JACKPOT : " +
" ",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight
.w800,
color: Colors
.black)
),
WidgetSpan(
child: Icon(
FontAwesomeIcons
.euroSign,
color: Colors
.amber[900],
size: 20)
),
解决方案
为此使用三元运算符
// ......
text: "JACKPOT : "+ value==7 ? j.j7.toString() : j.j14.toString()
// ........
// Given j7 & j14 are int
推荐阅读
- php - 为 GET 或 Retrieve 捕获条带错误
- javascript - 尝试 javascript 让我的消息在显示几秒钟后消失。我的代码不起作用
- python - requests.session() 对象在另一个类中无法识别
- android - 使用 DataBinding 使文本的某些部分变为粗体
- gremlin - 如何在gremlin中指定广度优先策略或深度优先策略
- php - 当从 shell for loop 内部调用 php getmxrr() 时,性能会下降
- vue.js - VueJS如何在v-html指令中附加div
- hadoop - Hadoop 集群 - “hadoop”用户 ssh 通信
- python - Tensorflow:如何平铺以特定顺序重复的张量?
- javascript - 嵌套异步循环中的节点 MySQL 两个查询