Home > Back-end >  Pass data between screen when pressed on details inside Listview (flutter_bloc)
Pass data between screen when pressed on details inside Listview (flutter_bloc)

Time:12-22

I have Listview and I want to pass the data when I pressed it and show the detail page, this is my listview:

 Widget _buildCard(BuildContext context,List<HospitalListModel> model) {
    return ListView.builder(
      itemCount: model.length,
      itemBuilder: (context, index) {
        return Container(
          margin: EdgeInsets.all(8.0),
          child: Card(
            child: GestureDetector(
                onTap: (){
                  HospitalDetailPage(
                    title: model[index].title,
                    content: model[index].content,
                    image: model[index].image,
                    phone: model[index].phone,
                    coordinates: model[index].coordinates,
                    website: model[index].website);
                },
                child: Container(
              margin: EdgeInsets.all(8.0),
              child: Column(
                children: <Widget>[
                  Image.network(model[index].image),
                  Text("${model[index].title}", style: GoogleFonts.roboto(
                    textStyle: TextStyle(color: Colors.black, letterSpacing: .5, fontWeight: FontWeight.w400),
                  ),),
                ],
              ),
            ),
          ),
        ));
      },
    );
  }

and I made the screen detail, i pass it with required on detail page, and i call it on listview page and pass the data

HospitalDetailPage({
    Key? key,
    required this.title,
    required this.content,
    required this.image,
    required this.phone,
    required this.coordinates,
    required this.website,
  }) : super(key: key);

but somehow it didn't work and when i click the listview, it didn't bring me to detail screen, it stuck in the same screen and im questioning whether im wrong to put the GestureDetector or something, do you know where the problem is?

CodePudding user response:

You aren't using navigator Try this code...

Widget _buildCard(BuildContext context,List<HospitalListModel> model) {
  return ListView.builder(
    itemCount: model.length,
    itemBuilder: (context, index) {
      return Container(
        margin: EdgeInsets.all(8.0),
        child: Card(
          child: GestureDetector(
              onTap: (){
                Navigator.push(context,
                  MaterialPageRoute(builder: (context) =>  HospitalDetailPage(
                    title: model[index].title,
                    content: model[index].content,
                    image: model[index].image,
                    phone: model[index].phone,
                    coordinates: model[index].coordinates,
                    website: model[index].website)
                  ),
                );
              },
              child: Container(
            margin: EdgeInsets.all(8.0),
            child: Column(
              children: <Widget>[
                Image.network(model[index].image),
                Text("${model[index].title}", style: GoogleFonts.roboto(
                  textStyle: TextStyle(color: Colors.black, letterSpacing: .5, fontWeight: FontWeight.w400),
                ),),
              ],
            ),
          ),
        ),
      ));
    },
  );
}
  • Related