【问题标题】:Dynamic Listview Builder Navigation动态列表视图生成器导航
【发布时间】:2020-08-11 23:02:39
【问题描述】:

您好,我想知道如何在列表视图构建器中实现导航,这是我尝试过的。

 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),
                            ),
                          ),
                        ),
                      ),
                    ));
              },
            ),
          ),
        )
      ],
    ),
  ),

【问题讨论】:

  • 你能再具体一点吗?什么样的导航,在这段代码中什么不起作用?
  • 嗨 @TiagoRossi 我想根据 api 名称导航到不同的屏幕:例如:menuList[index].name。它应该与参数一起导航到该页面。 Navigator.push(context, MaterialPageRoute(builder: (context) => menuList[index].name(menuList[index].children,widget.name,widget.code)), );

标签: flutter flutter-layout flutter-navigation


【解决方案1】:

我对您的问题的理解是,您希望针对每个索引上的不同 API 值打开不同的活动。为什么不在 onTap() 中放置一个 IF 条件来检查每个元素的某个值,该值指示导航到哪里。例如:

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, ), ), ); }

【讨论】:

  • 嗨@AnshaySIngh 这个答案对我有用,但是如果我有超过 10 条路线,我能知道怎么办吗?这可行吗?
  • 在这种情况下使用 else-if 就可以了。虽然我自己没有尝试过超过 4 条路线。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多