Flutter - Container onPressed?

后端 未结 6 1715
孤街浪徒
孤街浪徒 2021-01-30 20:01

I have this container:

  new Container(
    width: 500.0,
    padding: new EdgeInsets.fromLTRB(20.0, 40.0, 20.0, 40.0),
    color: Colors.green,
    child: new C         


        
6条回答
  •  野性不改
    2021-01-30 20:14

    The container itself doesn't have any click event, so to do that there are two ways

    1. InkWell widget
    2. GestureDetector

    In Flutter, InkWell is a material widget that responds to touch action.

    InkWell(
        child: Container(......),
        onTap: () { 
            print("Click event on Container"); 
        },
    );
    

    GestureDetector is a widget that detects the gestures.

    GestureDetector(
        onTap: () { 
            print("Click event on Container"); 
        },
        child: Container(.......),
    )
    

    Difference

    InkWell is a material widget and it can show you a Ripple Effect whenever a touch was received.

    GestureDetector is more general-purpose, not only for touch but also for other gestures.

提交回复
热议问题