Mfreeman
Mfreeman

Reputation: 3958

Wrapping Gesture detector around a card

Right now i have a button on the body of the page but cannot implement this button (that routes to a different page) to just encapsulate just the card that is

child: new Text(data["data"][index]["title"]),

it is inside of an itemBuilder so i thought i had to do a GestureDetector. Ive tried to put that child into the GestureDetector method but cannot get it to work unless its the whole body.

@override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text("Listviews"),
        ),
        body: new GestureDetector(
          onTap: () {
            Navigator.push(context,
              MaterialPageRoute(builder:  (context) => StandardsControlPage()),
            );
          },
          child: Container(
            padding: EdgeInsets.all(16.0),
            child: new ListView.builder(
              itemCount: data == null ? 0 : data["data"].length,
              itemBuilder: (BuildContext context, int index) {
                return new Card(
                  child: new Text(data["data"][index]["title"]),
                );},
            ),
          ),
        ),
      );
    }}

This example wont work if i have multiple buttons to press with different routes, and was wondering If there is anyway to implement a button with that route to just that child, how would i do it?

Upvotes: 0

Views: 1475

Answers (1)

Dhaval
Dhaval

Reputation: 2874

return new Scaffold(
    appBar: new AppBar(
      title: new Text("Listviews"),
    ),
    body: Container(
      padding: EdgeInsets.all(16.0),
      child: new ListView.builder(
        itemCount: data == null ? 0 : data["data"].length,
        itemBuilder: (BuildContext context, int index) {
          return new GestureDetector(
            child: new Card(
              child: new Text(data["data"][index]["title"]),
            ),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => StandardsControlPage()),
              );
            },
          );
        },
      ),
    ));

I think this will work.

Upvotes: 2

Related Questions