Flutter 颤振2:带有图标和文本的按钮
由于Flutter 颤振2:带有图标和文本的按钮,flutter,Flutter,由于RaisedButton,FlatButton等被弃用。 是否有关于按钮的小部件,提供文本和图标以防止创建行或其他解决方法?我在下面创建了名为IconTextButton的类: class IconTextButton extends StatelessWidget { IconTextButton({@required this.title,@required this.onPressed, @required this.icon,this.color,this.shape});
RaisedButton
,FlatButton
等被弃用。
是否有关于按钮的小部件,提供文本和图标以防止创建行或其他解决方法?我在下面创建了名为IconTextButton的类:
class IconTextButton extends StatelessWidget {
IconTextButton({@required this.title,@required this.onPressed, @required this.icon,this.color,this.shape});
final Icon icon;
final Color color;
final Text title;
final ShapeBorder shape;
final void Function() onPressed;
@override
Widget build(BuildContext context) {
return new Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: MaterialButton(
shape: shape,
color: color,
onPressed: onPressed,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
icon,
title
],
),
)
);
}
}
现在转到代码:) 我在下面创建了名为IconTextButton的类:
class IconTextButton extends StatelessWidget {
IconTextButton({@required this.title,@required this.onPressed, @required this.icon,this.color,this.shape});
final Icon icon;
final Color color;
final Text title;
final ShapeBorder shape;
final void Function() onPressed;
@override
Widget build(BuildContext context) {
return new Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: MaterialButton(
shape: shape,
color: color,
onPressed: onPressed,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
icon,
title
],
),
)
);
}
}
现在转到代码:) 您可以使用
下面是示例按钮:
代码:
//按钮的颜色状态。
Color\u getTextColor(设置状态)=>states.any({
MaterialState.pressed,
材料状态悬停,
材料状态聚焦,
}.包含)
? 颜色。绿色
:颜色:蓝色;
小部件_myButton(){
返回提升按钮图标(
onPressed:(){/*在这里做点什么*/},
图标:图标(Icons.edit),
样式:ButtonStyle(背景色:MaterialStateColor.resolveWith(_getTextColor)),
标签:文本(
“更新”,
样式:TextStyle(颜色:Colors.white),
));
}
您可以使用
下面是示例按钮:
代码:
//按钮的颜色状态。
Color\u getTextColor(设置状态)=>states.any({
MaterialState.pressed,
材料状态悬停,
材料状态聚焦,
}.包含)
? 颜色。绿色
:颜色:蓝色;
小部件_myButton(){
返回提升按钮图标(
onPressed:(){/*在这里做点什么*/},
图标:图标(Icons.edit),
样式:ButtonStyle(背景色:MaterialStateColor.resolveWith(_getTextColor)),
标签:文本(
“更新”,
样式:TextStyle(颜色:Colors.white),
));
}
您只需使用TextButton.icon()
构造函数即可
TextButton.icon(
icon:icon(),//此处显示您的图标
label:Text(),//此处显示您的文本
按下:(){},
)
有关TextButton.icon()构造函数的详细信息,您可以。只需使用
TextButton.icon()
构造函数即可
TextButton.icon(
icon:icon(),//此处显示您的图标
label:Text(),//此处显示您的文本
按下:(){},
)
有关TextButton.icon()构造函数的详细信息,您可以。这是所述的解决方法。其他解决方案更好;)这是所描述的解决方法。其他解决方案更好;)很好,没有注意到带有{…}.图标的构造函数,谢谢!很好,没有注意到带有{…}.图标的构造函数,谢谢!很好,没有注意到带有{…}.图标的构造函数,谢谢!很好,没有注意到带有{…}.图标的构造函数,谢谢!
// Color state for button.
Color _getTextColor(Set<MaterialState> states) => states.any(<MaterialState>{
MaterialState.pressed,
MaterialState.hovered,
MaterialState.focused,
}.contains)
? Colors.green
: Colors.blue;
Widget _myButton() {
return ElevatedButton.icon(
onPressed: () {/* do something here */ },
icon: Icon(Icons.edit),
style: ButtonStyle(backgroundColor: MaterialStateColor.resolveWith(_getTextColor)),
label: Text(
"Update",
style: TextStyle(color: Colors.white),
));
}