flutter - 根据条件显示 ListTile 的数量
问题描述
我想开发一个关于五本书的应用程序,它们都有不同的章节数,例如
- 书 A - 20 章
- 书 B - 14 章
- 书 C - 23 章
- 书 D - 18 章
- 书 E - 19 章
我做过:
the signIn page
the signUp page
和_the choose book page
我想做:
the choose chapter page
注意:两者都使用the choose book page
LISTTILEthe choose chapter page
当涉及到选择章节页面时,我无法根据书籍类型显示 ListTiles 的数量,我应该为每本书创建不同的选择章节页面(我害怕重复代码)还是 Flutter 中有一个功能让您根据特定条件(如果可用)显示 ListTiles 的数量,在我的情况下,条件是书籍类型
我需要一个指南来判断我是否应该:
- 为每本书创建不同的选择章节页面。
- 使用 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);
},
),`
解决方案
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
);
}
}
推荐阅读
- python - split_on_silence 修改原始音频
- vector - \下划线在 MathJax 中显得太低
- angular6 - 当我尝试使用角度防护根据用户角色进行路由时,浏览器冻结
- javascript - 如果没有数据,如何隐藏 DataTables 导出按钮?
- json - JOLT - 将嵌套子项合并到父项中
- wpf - TabItem 的内容在设置为折叠/隐藏时保持可见
- php - 使用 lavary/laravel-menu 来自 laravel 数据库的动态菜单
- c++ - 如何在 protoc 插入点(Protobuf)中插入包含?
- azure - Azure API 管理自动化
- lwjgl - LWJGL glDrawArrays 不绘制