flutter的GestureDetector 点击区域小

使用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的区域。如果不添加这个属性,那么只能点击到文字时才会有响应。如下图所示:

发布了189 篇原创文章 · 获赞 81 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/cpcpcp123/article/details/94397609