Home > Mobile >  GestureDetector with child button: prevent onTapDown call
GestureDetector with child button: prevent onTapDown call

Time:05-03

I have a GestureDetector with CupertinoButton inside. How to prevent passing touches up the tree when tapping on the button?

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: GestureDetector(
        onTapDown: (details) => debugPrint('onTapDown'),
        child: Center(
            child: CupertinoButton(
          child: Text('Tap'),
          onPressed: () {},
        )),
      ),
    );
  }
}

In my example, I don't want onTapDown to be printed when the button is tapped. Using Stack is not an option for me, sorry.

CodePudding user response:

Add behavior property of GestureDetector to HitTestBehavior.opaque

Full code:

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTapDown: (details) => debugPrint('onTapDown'),
        child: Center(
            child: CupertinoButton(
          child: const Text('Tap'),
          onPressed: () => debugPrint('Tap'),
        )),
      ),
    );
  }
}

CodePudding user response:

Replace (details) => debugPrint('onTapDown') with null

  • Related