I am new to flutter. I have extracted a widget in order to create signin-buttons. 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
you are on the right way. now declare a function which return a Icon-Widget. In the function use a switch-case, to return a different Icon depending on the String which is delivered as parameter.
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)),
],
),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.