Flutter GestureDetector not working inside Stack with TextField

故事扮演 提交于 2021-01-29 12:53:52

问题


I wrote a non-modal popup dialog that works fine UNTIL I put it inside a Stack with a TextField. Works fine without the TextField and even when I put two dialogs in the Stack (they both work).

I saw a similar post here, but I don't think it applies because it involves two GestureDetectors in a Stack, not the problem I'm seeing of a TextField breaking my GestureDetector.

As requested, I created a dartpad version of my modeless dialog using a Stack:

Dartpad version

EDIT

I see what's happening now. The Stack only processes gestures within its bounds, even though Overflow.visible is enabled. Same issue as this question. I'll delete this question or update it when I have a solution.

Here's what the Dartpad version looks like:

Here's the new Dartpad code. Only the ... Icon is GestureDetecting

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          //child: TestDialog(), // <-- works without stack
          child: SurveyEditorItem(),
        ),
      ),
    );
  }
}

class TestDialog extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 280,
      padding: EdgeInsets.all(15),
      color: Colors.grey,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Icon(Icons.done),
          Icon(Icons.movie),
          Icon(Icons.undo),
          GestureDetector(
            child: Icon(Icons.more_horiz),
            onTap: () {
              print('Success!'); // <-- this line not reached
            },
          ),
        ],
      ),
    );
  }
}

class SurveyEditorItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Stack(
      overflow: Overflow.visible,
      children: <Widget>[
        Positioned(
          top: -45,
          left: 40,
          child: TestDialog(),
        ),
        TextField(
          onTap: () {
            print('TextField tapped\n');
          },
        ),
      ],
    );
  }
}

来源:https://stackoverflow.com/questions/60788538/flutter-gesturedetector-not-working-inside-stack-with-textfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!