Search code examples
flutterflutter-layoutflutter-navigation

Dynamic Listview Builder Navigation


Hi I would want to know how to implement a navigation in a listview builder and here is what I have tried.

 Widget build(BuildContext context) {
return Scaffold(
body:Container(
    padding: EdgeInsets.all(10.0),
    color: AppColors.whiteColor,
    child: Column(
      children: <Widget>[

        Flexible(
          child: AnimationLimiter(
            child: ListView.builder(
              shrinkWrap: false,
              padding: const EdgeInsets.all(8.0),
              itemCount: menuList.length,
              itemBuilder: (BuildContext context, int index) {
                return AnimationConfiguration.staggeredList(
                    position: index,
                    duration: const Duration(milliseconds: 375),
                    child: SlideAnimation(
                      verticalOffset: 44.0,
                      child: FadeInAnimation(
                        child: GestureDetector(
                          onTap: (){
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(builder: (context) => menuList[index].name(menuList[index].children,widget.name,widget.code)),
                                );


                          },
                          child: Card(
                            margin: EdgeInsets.only(bottom: SizeConfig.blockSizeVertical*2),
                            child: ListTile(
                              leading: Image.network(menuList[index].icon),
                              title: Text(menuList[index].name),
                            ),
                          ),
                        ),
                      ),
                    ));
              },
            ),
          ),
        )
      ],
    ),
  ),

Solution

  • What I understood by your question is that you want to open different activity with respect to different API values on each indices. Why not put an IF condition in your onTap() to check a certain value at each element that indicates where to navigate. For example:

    if (widget.studentTestInfo.content[index].canSeeResultTr == true) { Navigator.push( context, MaterialPageRoute( builder: (context) => ResultPageLook( studentProfile: widget.studentProfile, studentTestInfo: widget.studentTestInfo, ), ) ); } else { Navigator.push( context, MaterialPageRoute( builder: (context) => TestSyllabus( studentProfile: widget.studentProfile, studentTestInfo: widget.studentTestInfo, ), ), ); }