您可以构造一个名为MyElevatedButton 的新类。
class MyElevatedButton extends StatelessWidget {
final VoidCallback? onPressed;
final Widget child;
final Color? color;
final Color? disabledColor;
final Color? textColor;
final Color? disabledTextColor;
final Color? hoverColor;
final Color? splashColor;
final double? elevation;
final double? disabledElevation;
final EdgeInsetsGeometry? padding;
final OutlinedBorder? shape;
MyElevatedButton({
Key? key,
required this.onPressed,
required this.child,
this.color,
this.disabledColor,
this.textColor,
this.disabledTextColor,
this.hoverColor,
this.splashColor,
this.elevation,
this.disabledElevation,
this.padding,
this.shape,
}) : super(key: key);
bool _isPressed(Set<MaterialState> states) => states.contains(MaterialState.pressed);
bool _isDisabled(Set<MaterialState> states) => states.contains(MaterialState.disabled);
bool _isHovered(Set<MaterialState> states) => states.contains(MaterialState.hovered);
@override
Widget build(BuildContext context) {
final style = ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color?>((states) {
if (_isDisabled(states)) {
return disabledColor;
} else if (_isHovered(states)) {
return hoverColor;
}
return color;
}),
foregroundColor: MaterialStateProperty.resolveWith<Color?>((states) {
if (_isDisabled(states)) return disabledTextColor;
}),
overlayColor: MaterialStateProperty.resolveWith<Color?>((states) {
if (_isPressed(states)) return splashColor;
}),
elevation: MaterialStateProperty.resolveWith<double?>((states) {
if (_isDisabled(states)) return disabledElevation;
return elevation;
}),
shape: MaterialStateProperty.all<OutlinedBorder?>(shape),
padding: MaterialStateProperty.all<EdgeInsetsGeometry?>(padding),
);
return ElevatedButton(
onPressed: onPressed,
style: style,
child: child,
);
}
}
这是示例代码(一个是RaisedButton,另一个是ElevatedButton,使用相同的属性)
@override
Widget build(BuildContext context) {
final onPressed = () {};
final child = Text('Button');
final color = Colors.indigoAccent;
final disabledColor = Colors.indigo;
final textColor = Colors.white;
final disabledTextColor = Colors.grey;
final hoverColor = Colors.pinkAccent;
final splashColor = Colors.black;
final elevation = 8.0;
final disabledElevation = 0.0;
final shape = StadiumBorder();
final padding = EdgeInsets.all(12);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('RaisedButton'),
SizedBox(height: 4),
RaisedButton(
onPressed: onPressed,
color: color,
disabledColor: disabledColor,
textColor: textColor,
disabledTextColor: disabledTextColor,
hoverColor: hoverColor,
splashColor: splashColor,
elevation: elevation,
padding: padding,
shape: shape,
child: child,
),
SizedBox(height: 30),
Text('ElevatedButton'),
SizedBox(height: 4),
MyElevatedButton(
onPressed: onPressed,
color: color,
disabledColor: disabledColor,
textColor: textColor,
disabledTextColor: disabledTextColor,
hoverColor: hoverColor,
splashColor: splashColor,
elevation: elevation,
disabledElevation: disabledElevation,
padding: padding,
shape: shape,
child: child,
),
],
),
),
);
}
我介绍了大部分常见属性,但不是全部,请随意添加您自己的属性,因为所有属性的工作方式都相似。唯一不可复制的是RaisedButton 的highlightColor,因为overlayColor 的集合一次只反映一个属性,并且优先于backgroundColor。
禁用状态: