Flutter:如果 child 被点击,从 parent 和 child 触发 ontap/onpressed
Flutter: fire ontap/onpressed from parent and child if child is tapped
我有一个 GestureDetector 和很多孩子,例如 TextButtons。如果按下一个按钮,我想从 Gesturedetector 和一个按钮触发 ontap。在下面的例子中,如果我按下按钮 1,输出应该是
Button 1 tap
GestureDetector tap
我尝试更改 GestureDetector 的行为,但没有任何效果。
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => print("GestureDetector tap"),
child: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Column(
children: [
TextButton(
child: Text("Button 1"),
onPressed: () => print("Button 1 tap"),
),
TextButton(
child: Text("Button 2"),
onPressed: () => print("Button 2 tap"),
)
],
)
),
);
}
}
我可以写一个方法并将其放入所有 onTap/onpressed,但我想我遗漏了一些东西。
我找到了属性 onTertiaryTapDown。
...This is called after a short timeout, even if the winning gesture
has not yet been selected....
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTertiaryTapDown: (tapDownDetails) =>{ print("GestureDetector tap")},
child: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Column(
children: [
TextButton(
child: Text("Button 1"),
onPressed: () => print("Button 1 tap"),
),
TextButton(
child: Text("Button 2"),
onPressed: () => print("Button 2 tap"),
)
],
)
),
);
}
}
我有一个 GestureDetector 和很多孩子,例如 TextButtons。如果按下一个按钮,我想从 Gesturedetector 和一个按钮触发 ontap。在下面的例子中,如果我按下按钮 1,输出应该是
Button 1 tap
GestureDetector tap
我尝试更改 GestureDetector 的行为,但没有任何效果。
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => print("GestureDetector tap"),
child: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Column(
children: [
TextButton(
child: Text("Button 1"),
onPressed: () => print("Button 1 tap"),
),
TextButton(
child: Text("Button 2"),
onPressed: () => print("Button 2 tap"),
)
],
)
),
);
}
}
我可以写一个方法并将其放入所有 onTap/onpressed,但我想我遗漏了一些东西。
我找到了属性 onTertiaryTapDown。
...This is called after a short timeout, even if the winning gesture has not yet been selected....
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTertiaryTapDown: (tapDownDetails) =>{ print("GestureDetector tap")},
child: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Column(
children: [
TextButton(
child: Text("Button 1"),
onPressed: () => print("Button 1 tap"),
),
TextButton(
child: Text("Button 2"),
onPressed: () => print("Button 2 tap"),
)
],
)
),
);
}
}