[英]Flutter: Use a parameter inside a widget call (Icon)
I am new to flutter. I have extracted a widget in order to create signin-buttons.我是 flutter 的新手。我提取了一个小部件以创建登录按钮。 I want to pass a parameter which specifies the icon which should be used, however I cannot add my parameter inside the method call of the Icon widget.
我想传递一个指定应该使用的图标的参数,但是我不能在图标小部件的方法调用中添加我的参数。 How can this be managed?
如何管理?
The error appears inside the Icon-Widget.错误出现在图标小部件内。 Code:
代码:
class LoginButtonIcon extends StatelessWidget {
final String iconName;
const LoginButtonIcon({Key key, @required this.iconName}) : super(key: key);
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () => "Pressed",
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Icon(
Icons.iconName,
color: Colors.white,
),
SizedBox(
width: 15,
),
Text("RaisedButton",
style: TextStyle(color: Colors.white, fontSize: 14)),
],
),
color: Colors.black54);
}
}
iconName type should not be String , it should be IconData and you should pass full icon like this Icons.add iconName类型不应该是String ,它应该是IconData并且你应该像这样传递完整的图标Icons.add
you are on the right way.你是在正确的方式。 now declare a function which return a Icon-Widget.
现在声明一个 function 返回一个图标小部件。 In the function use a switch-case, to return a different Icon depending on the String which is delivered as parameter.
在 function 中使用 switch-case,根据作为参数传递的字符串返回不同的图标。
Icon _getCorrectIcon() {
switch (iconName) {
case 'name-a':
return Icon(Icons.a);
case 'name-b':
return Icon(Icons.b);
case 'name-c':
return Icon(Icons.c);
default:
return Icon(Icons.a);
}
}
replace your Row to:将您的行替换为:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
_getCorrectIcon(),
SizedBox(
width: 15,
),
Text("RaisedButton",
style: TextStyle(color: Colors.white, fontSize: 14)),
],
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.