您可以为 MaterialStates 设置不同的值:
class TextButtonPadding extends StatelessWidget {
EdgeInsets _getPadding(Set<MaterialState> states) {
const interactiveStates = <MaterialState>{
MaterialState.pressed,
MaterialState.hovered,
};
if (states.any(interactiveStates.contains)) {
return EdgeInsets.all(30.0);
}
return EdgeInsets.zero;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: TextButton(
style: ButtonStyle(
padding: MaterialStateProperty.resolveWith(_getPadding),
),
onPressed: () {},
child: Text(
'Text Button',
),
),
),
);
}
}
或者为所有 MaterialStates 设置一个值:
class TextButtonPadding extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: TextButton(
style: ButtonStyle(
padding: MaterialStateProperty.all(
EdgeInsets.all(30.0),
),
),
onPressed: () {},
child: Text(
'Text Button',
),
),
),
);
}
}
请参阅https://api.flutter.dev/flutter/material/MaterialStateProperty-class.html