这是一些伪代码,显示了我想要实现的目标:
Text txt(text, [subtitle = false]) {
final params = subtitle
? {
'textAlign': TextAlign.center,
'style': TextStyle(color: Colors.purple)
}
: {
'textAlign': TextAlign.left,
'style': TextStyle(color: Colors.black54)
};
return Text(
text,
...params,
);
}
Run Code Online (Sandbox Code Playgroud)
是否可以将变量参数传递给 Flutter 小部件?请记住,Text 小部件只是一个示例,而不是我问题的焦点,它可以是任何其他 Flutter 小部件,例如 Container 或 SizedBox。
我想用另一个字符串替换 Dart 中的 URL 字符串。例子:
if (url == "http://www.example.com/1") {
home = "example";
} else if (url == "http://www.example.com/2") {
home = "another example";
}
Run Code Online (Sandbox Code Playgroud)
难道没有更好的方法,代码更少,速度更快吗?我必须这样做超过 60 次..
我需要知道是否有可能在flutter小部件中使用三元/如果不使用其他语言。
我正在尝试创建一个buttonCreator小部件,该小部件将使用几个参数,其中一个将是背景。我需要检查小部件是否启用了后台。我所拥有的是这个,但是我不知道如何在真实的Dart代码中使用它。
Container buildButton(String text, bool bg){
return new Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.white),
if (bg != true ? color: Colors.white : '')
),
padding: new EdgeInsets.all(15.0),
margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
child: new Center(
child: new Text(
text, style: new TextStyle(color: Colors.white, fontFamily: 'Monsterrat')
)
),
);
};
Run Code Online (Sandbox Code Playgroud)