使用GestureDetector包裹Container,发现在Container内容为空的区域点击时,捕捉不到onTap点击事件。
解决方案:在GestureDetector里面添加属性:behavior: HitTestBehavior.opaque,即可:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: Center(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: (){
print("hehe");
},
child: Padding(
padding: EdgeInsets.only(left: 150.0,top: 150.0),
child: Text('Hello World')),
),
),
),
);
}
}
解决方式是添加:behavior: HitTestBehavior.opaque,属性,可以让点击事件透过这个Text的区域。如果不添加这个属性,那么只能点击到文字时才会有响应。如下图所示: