使容器可点击

时间:2019-11-25 15:20:39

标签: flutter dart flutter-layout

我想使整个行都可单击,但是仅当我点击TextIcon时才会显示打印内容。

 Padding(
            padding: EdgeInsets.only(top: 15.0, left: 10.0, right: 10.0),
            child: Container(
                width: double.infinity,
                padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
                decoration: BoxDecoration(
                    border: Border.all(width: 0.5),
                    borderRadius: BorderRadius.all(Radius.circular(15.0))),
                child: GestureDetector(
                  onTap: () {
                    print('click');
                  },
                  child: Row(
                    children: <Widget>[
                      Padding(
                        child: Text(
                          Localization.of(context).location,
                          style: TextStyle(fontSize: 18),
                        ),
                        padding: EdgeInsets.only(left: 10.0),
                      ),
                      Spacer(),
                      Padding(
                        child: Container(
                          child: Icon(Icons.locations),
                          height: 25.0,
                        ),
                        padding: EdgeInsets.only(right: 15.0),
                      )
                    ],
                  ),
                )))

3 个答案:

答案 0 :(得分:3)

@John Joe,我认为使容器可点击的最简单方法是使用 InkWell

InkWell(
  child: Container(
    padding: const EdgeInsets.all(10.0),
    decoration: BoxDecoration(
      border: Border.all(width: 1.0, color: Colors.grey),
      borderRadius: BorderRadius.all(Radius.circular(5.0))
    ),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text('Locations', style: TextStyle(color: Colors.grey)),
        Icon(Icons.edit_location, color: Colors.grey)
      ])),
  onTap: () {
    setState(() {
      text = 'Locations Tapped!';
    });
  },
) 

enter image description here

希望这会有所帮助。

答案 1 :(得分:2)

尝试一下:

Padding(
  padding: EdgeInsets.only(top: 15.0, left: 10.0, right: 10.0),
  child: GestureDetector(
    onTap: () {
      print('click');
    },
    child: Container(
      width: double.infinity,
      padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
      decoration: BoxDecoration(border: Border.all(width: 0.5), borderRadius: BorderRadius.all(Radius.circular(15.0))),
      child: Row(
        children: <Widget>[
          Padding(
            child: Text(
              Localization.of(context).location,
              style: TextStyle(fontSize: 18),
            ),
            padding: EdgeInsets.only(left: 10.0),
          ),
          Spacer(),
          Padding(
            child: Container(
              child: Icon(Icons.locations),
              height: 25.0,
            ),
            padding: EdgeInsets.only(right: 15.0),
          )
        ],
      ),
    ),
  ),
);

答案 2 :(得分:1)

在GestureDetector中将Widget(可能是代码段中的Padding Widget)封装起来。基本上,将GestureDetector从当前级别移至要检测其轻敲的小部件。