簡體   English   中英

是否可以在 Flutter 中同時將點擊事件傳遞給多個小部件?

[英]Is it possible to pass a tap event to multiple widgets at the same time in Flutter?

當我點擊一個小部件時,我希望該小部件和它后面的小部件同時識別我的點擊。 但發生的只是最前面的小部件識別水龍頭。

我已經以多種方式嘗試了這個,與HitTestBehavior的所有組合的簡單父/子關系:

Center(
  child: GestureDetector(
    behavior: HitTestBehavior.opaque,
    onTap: () => print('red'),
    child: Container(
      color: Colors.red,
      width: 200,
      height: 200,
      child: Center(
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () => print('green'),
          child: Container(
            color: Colors.green,
            width: 100,
            height: 100,
          ),
        ),
      ),
    ),
  ),
);

還嘗試使用Stack 同樣,所有可能的行為:

Stack(
  alignment: Alignment.center,
  children: [
    GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => print('red'),
      child: Container(
        color: Colors.red,
        width: 200,
        height: 200,
      ),
    ),
    GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => print('green'),
      child: Container(
        color: Colors.green,
        width: 100,
        height: 100,
      ),
    ),
  ],
);

我知道我可以簡單地為兩個檢測器使用相互的 function,但我想要實現這一點的主要原因是在使用嵌套小部件時,需要添加不必要的實現來訪問該功能。

您可以使用onPanDown

GestureDetector(
  behavior: HitTestBehavior.translucent,
  onTap: () => print('green'),//doesnt work
  onPanDown: (details) => print('tdgreen'),
  child: Container(
    color: Colors.green,
    width: 100,
    height: 100,
  ),
),

您可以查看此線程以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM