如何使容器的填充可轻敲?

时间:2019-04-15 12:39:24

标签: flutter

我希望能够单击GestureDetector小部件周围的填充并触发其onTap方法。我不想更改图标本身的大小。这是到目前为止的代码。

class InfoButton extends StatelessWidget {
  final String text;
  final double padding;
  static const double defaultPadding = 5;

  const InfoButton({Key key, @required this.text, this.padding = defaultPadding}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(padding),
      decoration: BoxDecoration(border: Border.all()),
      child: GestureDetector(
        child: Icon(
          Icons.info_outline,
          color: Colors.blue,
          size: 20,
        ),
        onTap: () {
          print('clicked');
        },
      ),
    );
  }
}

如何在颤抖中实现这一目标?

1 个答案:

答案 0 :(得分:2)

只需将Container作为GestureDetector的子元素而不是Icon,然后将behavior的{​​{1}}属性设置为GestureDetector,如下所示< / p>

HitTestBehavior.opaque