【问题标题】:How to make padding of container tap-able?如何使容器的填充可以点击?
【发布时间】:2019-04-15 12:39:24
【问题描述】:

我希望能够单击 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');
        },
      ),
    );
  }
}

如何在颤振中实现这一点?

【问题讨论】:

  • 只需将容器设置为手势检测器的子容器。但是有更好的方法来创建带有图标的按钮,例如:RawMaterialButtonIconButton
  • 我实际上打算使用 IconButton 但它在图标周围设置了 48 像素的默认值,以符合材料设计指南。但我需要自定义填充大小。

标签: flutter


【解决方案1】:

只需将Container 包含为GestureDetector 的子代而不是Icon 并将GestureDetectorbehavior 属性设置为HitTestBehavior.opaque 就像这样

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

【讨论】:

  • 我实际上需要在您的代码中将 GestureDetector 的行为字段设置为 HitTestBehavior.opaque 以使其按预期工作
  • 请更改您的代码,以便我接受它作为正确答案
  • 没有behaviour 属性是否也能正常工作?
  • 不幸的是没有
  • 我刚刚检查过,它对我有用,您不需要behaviour。您之前也说过它有效,然后发生了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多