Flutter 给控件添加点击事件

需求个人中心item 需要点击,但是它的最外层是Column 布局,这时候只需要外层加一个InkWell 就能实现了(在flutter 开发中用InkWell或者GestureDetector将某个组件包起来,可添加点击事件)
代码如下:

_itemSection(String title, Widget image){
    return InkWell(
      onTap: (){
        print("点击了 $title");

      },
      child: Column(
        children: [
          Container(
            alignment: Alignment.centerLeft,
            height: 40,
            padding: EdgeInsets.only(left: 17,right: 17),
            decoration: new BoxDecoration(
                color:Color(0xFF0D1020),
                borderRadius: BorderRadius.circular(5)
            ),
            child: Row(
              children: [
                image,
                Expanded(
                  child: Container(
                    padding: EdgeInsets.only(left: 14,),
                    child: Text(
                      title,
                      style: TextStyle(
                          color: Colors.white
                      ),
                    ),
                  ),
                ),
                LoadAssetImage("my/my_right_arrow",width: 6,height: 10,)
              ],
            ),
          )
        ],
      ),
    );
  }

注意事项:

  1. 使用 InkWell 时内外层均不建议添加背景色,InkWell 默认的水波纹颜色很浅,背景色会遮挡波纹效果;
  2. 通过修改 splashColor: Colors.greenAccent, 属性可以动态修改水波纹的波纹颜色,但如果修改高亮色属性 highlightColor,则相当于修改背景色;
  3. 请一定添加 InWell 手势触发事件,如 onTap 等。

你可能感兴趣的:(Flutter 给控件添加点击事件)