当每个按钮的onPressed
函数调用时,我想插入另一个函数hookButtonOnPressed
。
的例子:
TextButton(
onPressed: (){
print('onPressedInvoked');
},
child: Text('Test')
)
void hookButtonOnPressed() {
print('hookButtonOnPressedInvoked');
}
所以当我按下TextButton
时,我希望控制台会显示
hookButtonOnPressedInvoked
onPressedInvoked
您可以通过创建自定义TextButton小部件
来实现这一点void hookButtonOnPressed() {
print('hookButtonOnPressedInvoked');
}
// custom Button Widget
class CustomTextButton extends StatelessWidget {
final Function callback;
CustomTextButton({
required this.callback,
});
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
callback();
hookButtonOnPressed();
},
child: const Text('Test')
);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
CustomTextButton(callback:()=>{print("hello")}),
CustomTextButton(callback:()=>{print("hello")}),
],
),
),
);
}
}
同时按下两个键的输出
hello
hookButtonOnPressedInvoked
hello
hookButtonOnPressedInvoked