Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GestureDetector onTap Card

Tags:

flutter

dart

new Expanded(

        child: _searchResult.length != 0 || controller.text.isNotEmpty
            ? new ListView.builder(
                itemCount: _searchResult.length,
                itemBuilder: (context, int i) {

                  return new Card(

                      child: new Column(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                        new Row(children: <Widget>[
                          //new GestureDetector(),

                          new Container(

                              width: 45.0,
                              height: 45.0,
                              decoration: new BoxDecoration(
                                  shape: BoxShape.circle,
                                  image: new DecorationImage(
                                      fit: BoxFit.fill,
                                      image: new NetworkImage(
                                          "https://raw.githubusercontent.com/flutter/website/master/_includes/code/layout/lakes/images/lake.jpg")))),
                          new Text(
                              " " +
                                  userDetails[returnTicketDetails[i]
                                      ["user_id"]]["first_name"] +
                                  " " +
                                  (userDetails[returnTicketDetails[i]
                                      ["user_id"]]["last_name"]),
                              style: const TextStyle(
                                  fontFamily: 'Poppins', fontSize: 20.0)),
                        ]),
                        new Column(
                          children: <Widget>[
                            new Align(
                                alignment: FractionalOffset.topRight,
                                child: new FloatingActionButton(
                                  onPressed: () {

                                    groupId = returnTicketDetails[i]["id"];

                                    print(returnTicketDetails[i]["id"]);
                                    print(widget.id);

                                    Navigator.push(
                                        context,
                                        new MaterialPageRoute(
                                            builder: (context) => new Tickets(groupId,widget.id)));

                                  },
                                  heroTag: null,
                                  backgroundColor: Color(0xFF53DD6C),
                                  child: new Icon(Icons.arrow_forward),
                                )),
                            new Padding(padding: new EdgeInsets.all(3.0)),
                          ],
                        )
                      ]));
                },
              )
            : new ListView.builder(
                itemCount: _searchResult.length,
                itemBuilder: (context, int i) {
                  return new Card(
                    child: new ListTile(
                        //title: new Text(userDetails[returnTicketDetails[i]["user_id"]]["first_name"]),
                        ),
                    margin: const EdgeInsets.all(0.0),
                  );
                },
              ),
      ),

Hi everyone! As I am building dynamically a Card in a ListView, I was thinking rather than keep the FloatingActionButton in each of them as I already do, to implement a onTap method in each card and trigger something. In other words, I would like to keep the card as simple as possible without many widget around. Thank you in advance!

like image 446
heyr Avatar asked Jun 03 '18 00:06

heyr


People also ask

Why is GestureDetector used?

Gesture Detector in Flutter is used to detect the user's gestures on the application. It is a non-visual widget. Inside the gesture detector, another widget is placed and the gesture detector will capture all these events (gestures) and execute the tasks accordingly.

What is the difference between InkWell and GestureDetector in Flutter?

They both provide many common features like onTap , onLongPress etc. The main difference is GestureDetector provides more controls like dragging etc. on the other hand it doesn't include ripple effect tap, which InkWell does.

Are cards clickable in Flutter?

You can make widgets like Container, Card, Text, or any widget clickable in Flutter with the help of InkWell and GestureDetector widgets.


2 Answers

As Card is "a sheet of Material", you probably want to use InkWell, which includes Material highlight and splash effects, based on the closest Material ancestor.

return Card(
  child: InkWell(
    onTap: () {
        // Function is executed on tap.
    },
    child: ..,
  ),
);
like image 172
creativecreatorormaybenot Avatar answered Sep 19 '22 05:09

creativecreatorormaybenot


Just wrap the Card with GestureDetector as below,

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new ListView.builder(
      itemBuilder: (context, i) {
        new GestureDetector(
          child: new Card(
          ....    
          ),
          onTap: onCardTapped(i),
        );
      },
    );
  }

  onCardTapped(int position) {
    print('Card $position tapped');
  }
}
like image 28
Vinoth Kumar Avatar answered Sep 21 '22 05:09

Vinoth Kumar