【发布时间】:2021-03-05 02:30:33
【问题描述】:
我正在尝试创建一个带有图标(左侧)和右侧文字的简单圆形按钮。
我关注了https://stackoverflow.com/a/63124491/13684332,它建议我们可以在FittedBox 中使用fit:BoxFit.cover 来制作Text。我也试过fit:BoxFit.scaleDown 和fit:BoxFit.fitWidth。所有这些都会导致一些像素溢出文本小部件。
class RoundedButtonWithIcon extends StatelessWidget {
RoundedButtonWithIcon(
{@required this.text,
@required this.onPress,
this.icon,
this.color,
this.image});
final String text;
final Function onPress;
Icon icon;
Image image;
Color color;
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child: FlatButton(
child: Row(children: [
Container(
margin: const EdgeInsets.only(right: 3),
child: image != null ? image : (icon != null ? icon : null)),
FittedBox(
fit:BoxFit.cover,
child:
Text(
text,
style: TextStyle(
//fontSize: 13,
fontWeight: FontWeight.w400,
fontFamily: 'Roboto',
color: color != null ? color : Colors.white),
)
)]),
onPressed: () {
onPress();
},
textColor: Colors.white,
color: Colors.gray,
shape: RoundedRectangleBorder(
side: BorderSide(
color: Colors.red, width: 1.3),
borderRadius: BorderRadius.circular(5)),
));
}
}
【问题讨论】:
-
您可以使用 FlatButton.icon 构造函数来实现上述结果。更多详情请点击链接:api.flutter.dev/flutter/material/FlatButton/…