首页 > 解决方案 > 根据条件显示 ListTile 的数量

问题描述

我想开发一个关于五本书的应用程序,它们都有不同的章节数,例如

  1. 书 A - 20 章
  2. 书 B - 14 章
  3. 书 C - 23 章
  4. 书 D - 18 章
  5. 书 E - 19 章

我做过

the signIn pagethe signUp page和_the choose book page

我想做

the choose chapter page

注意:两者都使用the choose book pageLISTTILEthe choose chapter page

当涉及到选择章节页面时,我无法根据书籍类型显示 ListTiles 的数量,我应该为每本书创建不同的选择章节页面(我害怕重复代码)还是 Flutter 中有一个功能让您根据特定条件(如果可用)显示 ListTiles 的数量,在我的情况下,条件是书籍类型

我需要一个指南来判断我是否应该

  1. 为每本书创建不同的选择章节页面。
  2. 使用 Flutter 中的一项功能,该功能可让您根据特定条件更新 ListTiles。

the choose book page其中之一ListTile

到目前为止我尝试过的代码

ListTile(
          leading: CircleAvatar(
            backgroundImage: AssetImage("assets/books/book_a.jpg"),
          ),
          title: Text(
            'Book A',
          ),
          subtitle: Text('2348033980943')
          onTap: ()
          {
            //can i define no.ofchapters here and take the value on to the next screen??
            int number_of_chapters = 20,
            Navigator.pushNamed(context, ChooseChapterScreen.id);
          },
        ),`

标签: flutterdart

解决方案


class Chapters{
   String title;
   List<String> chapters;

   Chapters(this.title,this.chapters);
}

              // Inside your tap method
              onTap: ()
              {
                List<String> chaptersList= ["Chapter 1", "Chapter 2", "Chapter 3"];
                // you can pass arguments with named routes
                Navigator.pushNamed
                       (context, 
                       **Your_Next_Screen**,
                       arguments: Chapters(                               
                       'Book A',                              
                       chaptersList,       
                       ),                                                                                                                 
                );
              },

在Your_Next_Screen的构建方法中检索数据,例如,

@override
  Widget build(BuildContext context) {
    // Extract the arguments from the current ModalRoute settings and cast
    // them as Chapters.
    final Chapters args = ModalRoute.of(context).settings.arguments;

    var title= args.title;
    var list= args.chapters;

    return Scaffold(
      // your widget
    );
  }
}

推荐阅读