在Flutter中丢弃FlatButton
中的填充可以通过自定义按钮的样式来实现。以下是一种可能的方法:
class CustomFlatButton extends FlatButton {
const CustomFlatButton({
Key key,
@required VoidCallback onPressed,
ButtonTextTheme textTheme = ButtonTextTheme.normal,
Color textColor,
Color disabledTextColor,
Color color,
Color disabledColor,
Color focusColor,
Color hoverColor,
Color highlightColor,
EdgeInsetsGeometry padding,
ShapeBorder shape,
Clip clipBehavior,
MaterialTapTargetSize materialTapTargetSize,
@required Widget child,
}) : super(
key: key,
onPressed: onPressed,
textTheme: textTheme,
textColor: textColor,
disabledTextColor: disabledTextColor,
color: color,
disabledColor: disabledColor,
focusColor: focusColor,
hoverColor: hoverColor,
highlightColor: highlightColor,
padding: padding,
shape: shape,
clipBehavior: clipBehavior,
materialTapTargetSize: materialTapTargetSize,
child: child,
);
@override
Widget build(BuildContext context) {
return super.build(context);
}
}
CustomFlatButton
的build
方法中,将FlatButton
的默认填充设为零,并设置按钮的最小尺寸为0。@override
Widget build(BuildContext context) {
final EdgeInsetsGeometry defaultPadding = ButtonTheme.of(context).padding;
return ButtonTheme(
padding: EdgeInsets.zero,
minWidth: 0,
child: FlatButton(
onPressed: onPressed,
textTheme: textTheme,
textColor: textColor,
disabledTextColor: disabledTextColor,
color: color,
disabledColor: disabledColor,
focusColor: focusColor,
hoverColor: hoverColor,
highlightColor: highlightColor,
padding: padding ?? defaultPadding,
shape: shape,
clipBehavior: clipBehavior,
materialTapTargetSize: materialTapTargetSize,
child: child,
),
);
}
这样,你就可以使用CustomFlatButton
代替原始的FlatButton
,并且它将没有任何填充。
示例用法:
CustomFlatButton(
onPressed: () {
// 处理按钮点击事件
},
child: Text('按钮'),
),
这是一种在Flutter中丢弃FlatButton
填充的方法,可以根据具体需求进行调整和定制。如果需要了解更多关于Flutter的信息,可以访问腾讯云的Flutter相关产品和文档:
领取专属 10元无门槛券
手把手带您无忧上云