首页 > 解决方案 > 如何在小部件中设置条件?

问题描述

我想在这段代码中设置条件 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)
                                                          ),

标签: flutter

解决方案


为此使用三元运算符

// ......
text: "JACKPOT : "+ value==7 ? j.j7.toString() : j.j14.toString()
// ........
// Given j7 & j14 are int

推荐阅读