how's it going? Since I'm new to Flutter and there aren't many examples, I wanted to know how I could select an item from my ListView and pass the data to a new screen to profile the selected object.
The code of my ListView is:
class Page extends StatelessWidget {
@required
final Text title; //Titulo a mostrar
@required
final Future<List<Negocio>> list;
Page({this.title, this.list});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: this.title,
),
body: FutureBuilder<List<Negocio>>(
future: list,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
ListTile(
title: Text(snapshot.data[index].title),
onTap: () {
//ACA SUPONGO QUE IRIA ITEM SELECCIONADO.
},
);
Negocio negocio = snapshot.data[index];
return NegocioItem(
negocio: negocio,
);
});
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner
return Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
class NegocioItem extends StatelessWidget {
@required
final Negocio negocio;
NegocioItem({this.negocio});
@override
Widget build(BuildContext context) {
return GestureDetector(
//onTap: _launchURL,
child: Card(
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Image.network(
negocio.imageUrl,
width: 90,
),
Expanded(
child: Text(
negocio.title,
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 22),
textAlign: TextAlign.center,
),
),
],
),
Text(negocio.description),
/* Align(
alignment: Alignment.centerRight,
child: Text(
negocio.fecha,
textAlign: TextAlign.right,
style: TextStyle(fontSize: 15, color: Colors.green),
),
)*/
],
),
),
),
);
}
}
The Widget
ListTile
is not being used, you must remove it. You simply make a return to the widgetNegocioItem
, this is where you can do the action of calling another screen and sending the data, for example:page
BusinessItem
For the example I am going to create a
DetailPage
that receives the objectnegocio
as an attribute.