flutter - 在 Flutter 的磁贴中有条件地显示/隐藏复选框
问题描述
我有一个从 API 调用动态生成的图块列表。我在 AppBar 中有一个共享按钮,当我单击该按钮时,我想让复选框显示为列表图块中的尾随属性。例子:
ListTile(
title: Text('fetchedData.title'),
trailing: Checkbox()
)
我的方法是有条件地渲染图块,但我不确定我是否在正确的位置进行渲染。我正在使用布尔状态来确定复选框是否应显示在磁贴旁边。
我尝试过的其他方法是创建帮助函数来为我构建列表图块,但不确定它应该返回什么,如果您对如何制作这些排序函数有任何提示,请包括在内!
这是我的代码:
body: Center(
child: FutureBuilder(
future: pdfSnippets(),
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
if (snapshot.hasData) {
return ListView.builder(
// will have to hide the eccess snippet with overflow
// and have a max width to fit the trailing checkbox
itemCount: snippets.length,
itemBuilder: (BuildContext context, int index) {
// if you don't like card view, remove Card and just return ListTile
return checkboxStatus ?
Card (
child: ListTile(
// not referencing properly
title: Text('${snapshot[index].body}'),
subtitle: Text('${snapshot[index].title}'),
trailing: Checkbox(
value: false,
)
)
) :
Card (
child: ListTile(
// not referencing properly
title: Text('${snapshot[index].body}'),
subtitle: Text('${snapshot[index].title}'),
onLongPress: () => setCheckboxStatus(),
)
);
}
);
} else {
throw('nothing to see here');
}
},
),
),
解决方案
您可以CheckBox
像这样制作条件以使您的代码看起来更简单:
ListTile(
// not referencing properly
title: Text('body'),
subtitle: Text('title'),
trailing: checkboxStatus
? Checkbox(
value: false,
)
: null,
)
推荐阅读
- php - 在 PHP 项目中使用 ajax 问题重绘 HTML
- swift - 在 SwiftUI 中使用 CGContext
- javascript - 设计布局排序算法
- r - 部署 Shiny 应用程序时 RStudio 连接失败检索包
- c - 如何使用管道将数据从子进程发送到父进程以进行进程间通信?
- powershell - Powershell Invoke-WebRequest 返回对象数组,但处理时好像只有 1
- excel-formula - Excel:如何根据具有相同值的相邻单元格查找范围内的最大值
- php - 我试图在 laravel 上进行查询,但它返回此错误消息
- facebook - 如何通过 facebook graph api 访问公共页面的报价?
- macos - 无法使用应用 Proxifier 侦听 macOS Big Sur 上的任何 TCP 端口:套接字的协议类型错误