firebase - 选择或将 Firestore 项目传递到新页面(路由)
问题描述
我有一个来自我的 Firestore 的文档列表,我可以在其中选中和取消选中尾随复选框。我还有一个带有按钮的底部导航栏,可以将任何“选中”项目包含到新页面或收藏夹部分。我无法想出一种推送和导航所选项目的方法。请参阅下面的链接-谢谢
class _SelectScreenState extends State<SelectScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Select Exercises')),
body: _buildBody(context),
floatingActionButton: FloatingActionButton.extended(
elevation: 4.0,
icon: const Icon(Icons.add),
label: const Text('Add to workout'),
onPressed: () {},
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
child: new Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.menu),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.search),
onPressed: () {},
)
],
),
),
);
}
Widget _buildBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('exercises').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildList(context, snapshot.data.documents);
},
);
}
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
return ListView(
padding: const EdgeInsets.only(top: 20.0),
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
);
}
Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
final record = Record.fromSnapshot(data);
return Padding(
key: ValueKey(record.name),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: Exercise(
title: record.name,
),
),
);
}
}
class Exercise extends StatefulWidget {
final String title;
Exercise({this.title});
@override
_ExerciseState createState() => _ExerciseState();
}
class _ExerciseState extends State<Exercise> {
bool selected = false;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(widget.title),
trailing: Checkbox(
value: selected,
onChanged: (bool val) {
setState(() {
selected = val;
});
}),
);
}
}
class Record {
final String name;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['name'] != null),
name = map['name'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$name:>";
}
解决方案
推荐阅读
- python - 如何在元组列表中组合元组
- tensorflow - Keras中LSTM层的输入维度
- r - pmap purrr 错误:参数 1 必须有名称
- mysql - 以可读的方式计算每个用户的平均事务数
- android-studio - 如何编辑包含已编译代码的 kotlin 文件
- bash - 为什么在'if'语句中'then'必须在bash的下一行?
- tensorflow - Tensorboard 打印的摘要 Precision 和 Accuracy 不显示步数
- mysql - 如何在子查询之外使用有子句?
- java - 什么是热刷新?
- postgresql - 如何为工程数据设计 Timescaledb 模式