Home > Enterprise >  Flutter: fire ontap/onpressed from parent and child if child is tapped
Flutter: fire ontap/onpressed from parent and child if child is tapped

Time:09-18

I have a GestureDetector and a lot of childs e.g TextButtons. I want to fire the ontap from the Gesturedetector AND one Button if one Button is pressed. In the following example if i press Button 1 the output should be

Button 1 tap
GestureDetector tap

I tried to change the behaviour of the GestureDetector, but nothing worked.

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"),
              )
            ],
          )
          ),
    );
  }
}

I can write a method and put it in all onTap/onpressed, but i think i am missing something.

CodePudding user response:

I found the proppertie 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"),
              )
            ],
          )
          ),
    );
  }
}
  • Related