我有一個堆疊,首先包含一個ListView,然后是一個帶有GestureDetector的透明Widget來點擊它。一切運行正常,但是當我將滑鼠放在GestureDetector內時,我無法再滾動ListView,即使將GestureDetector的behavior屬性設定為HitTestBehavior.translucent,
我知道這是因為GestureDetector正在吸收它,也許是作為一個拖動事件,但我希望它只檢測到點擊事件,并讓滾動事件 "通過"。我如何才能在 Flutter 中實作這一行為呢?
請注意,這只發生在使用觸控板(以及我猜測的滾輪)進行滾動時,但如果您使用拖放手勢滾動串列,那么即使您的滑鼠懸停在頂部小部件上,滾動也不會停止。
我做了一個DartPad,你可以在這個鏈接中自己測驗一下。https://dartpad.dev/8d68891da69d661a8129d5adc7727e4c
代碼也粘貼在下面 :
import 'package:flutter/material.dart'/span>。
void main() {
runApp(
MaterialApp(
首頁。StackOverflowExample()
));
}
class StackOverflowExample extends StatelessWidget {
小工具 _buildListItem() {
return Container(
margin。const EdgeInsets.symmetric(vertical: 10)。
高度。100。
color: Colors.blue[100] 。
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
體。堆疊(
children: [
ListView.builder(
itemCount: 20,
itemBuilder: (_, __) => _buildListItem(),
),
中心(
孩子。GestureDetector(
行為。HitTestBehavior.translucent,
onTap: () => print('tap'/span>)。
孩子。const Text(
'Hover me and
試著滾動
串列視圖'。
風格。TextStyle(fontSize: 50, fontWeight: FontWeight.bold) 。
),
),
),
],
),
);
}
}
uj5u.com熱心網友回復:
問題不在于GestureDetector。實際上,是Text widget阻止ListView接收指標事件。盡管如此,你可以使用IgnorePointer輕松地解決它。
IgnorePointer
一個在點擊測驗期間不可見的小部件。
這將使文本部件忽略指標事件,而讓ListView接收這些事件:
這將使文本部件忽略指標事件,而讓ListView接收這些事件。
GestureDetector(
行為。HitTestBehavior.translucent,
onTap: () => print('tap'/span>)。
孩子。IgnorePointer( //You insert it right here above the Text widget)
孩子。const Text(
'Hover me and
試著滾動
串列視圖'。
風格。TextStyle(fontSize: 50, fontWeight: FontWeight.bold) 。
),
),
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/315721.html
標籤:
