4

I'm trying to make a Flutter app that can respond to a tap anywhere on the screen, using a GestureDetector. Here's my code:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "App",
      home: MyScreen(),
    );
  }
}

class MyScreen extends StatefulWidget {
  @override
  State createState() => MyScreenState();
}

class MyScreenState extends State<MyScreen> {
  String _text = "Hello";

  @override
  Widget build(BuildContext context) {
    _onTapUp(TapUpDetails details) {
      var x = details.globalPosition.dx;
      var y = details.globalPosition.dy;
      print("tap up " + x.toString() + ", " + y.toString());

      setState(() {
        _text = "Hello world";
      });
    }

    return GestureDetector(
      onTapUp: _onTapUp,
      child: Scaffold(
          appBar: AppBar(
              title: Text("App")
          ),
          body: Text(_text),
      ),
    );
  }
}

However, when I run the app an error appears on the bottom of the simulator saying BOTTOM OVERFLOWED BY Infinity PIXELS. The app appears to be functioning, but the error at the bottom is an eyesore. How can I resolve it?

Technicolor
  • 1,589
  • 2
  • 17
  • 31
  • 3
    Cannot reproduce. I copy pasted your code, and I don't get any overflow. – Rémi Rousselet Mar 10 '18 at 14:20
  • @Darky my mistake - the code wasn't totally up-to-date. It's been difficult to debug this error, though, because it appears intermittently and it takes several rounds of rebuilding/re-running to get the app running on the simulator to actually update. – Technicolor Mar 10 '18 at 16:08
  • 1
    I had a similar problem (yet not with infinity pixels). I ended up here https://stackoverflow.com/a/49614547/5766375 and used this solution, I wrapped my column with a SingleChildScrollView. – eseuteo Jun 19 '18 at 07:18
  • maybe you can check this [link](https://stackoverflow.com/a/53809904/6224209) :) – Vanya Rachel Jan 02 '19 at 03:31
  • I have tried your code and it seems working fine. Everything I tap into the screen returns the `x` and `y` coordinate correctly. If ever you got a chance, could you provide a screenshot and at least highlight the area in the screen where you get that error? – MαπμQμαπkγVπ.0 Sep 11 '20 at 21:43

1 Answers1

0

The issue seems to be unreproducible. The child widget defined on GestureDetector should set its height and prevent pixel overflows.

But if you need to define the height of your screen to avoid bottom overflows, you can either use LayoutBuilder or MediaQuery to fetch the screen dimensions.

Omatt
  • 8,564
  • 2
  • 42
  • 144