【发布时间】:2022-11-23 11:18:06
【问题描述】:
学习目的我创建了一个显示基于事务列表的 firebase 数据的屏幕。这里我添加了三个文本按钮来过滤数据,例如
All : 显示所有记录 收入:仅显示收入交易 费用:仅显示费用交易。
这是否意味着我必须制定 3 种未来的方法?但是如果我有很多过滤条件怎么办……还有其他方法吗……此外,我将根据每年的周月过滤日期……
class _HomeScreen2State extends State<HomeScreen2> {
Stream _getAllEntries() {
return FirebaseFirestore.instance
.collection('users')
.doc(widget.loggeduser.userid)
.collection('expenses')
.orderBy("date", descending: true)
.snapshots();
}
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Scaffold(
appBar: showAppbar(),
body: SafeArea(
child: Column(
children: [
Padding(
padding: EdgeInsets.all(10),
child: Align(
alignment: Alignment.centerLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(onPressed: () {}, child: Text('All')),
TextButton(onPressed: () {}, child: Text('Expenses')),
TextButton(onPressed: () {}, child: Text('Income')),
],
),
),
),
SizedBox(
height: 10,
),
showTransactions(size),
],
)),
floatingActionButton: buildfloatingactionbutton(),
);
}
Widget showTransactions(Size size) {
return Container(
height: size.height * .65,
// color: Colors.red,
child: StreamBuilder(
stream: _getAllEntries(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.hasData) {
QuerySnapshot querysnapshot = snapshot.data as QuerySnapshot;
if (querysnapshot.docs.length > 0) {
return ListView.builder(
padding: EdgeInsets.symmetric(vertical: 10),
itemCount: querysnapshot.docs.length,
itemBuilder: (context, index) {
final trans = TransactionModel.fromjson(
querysnapshot.docs[index].data()
as Map<String, dynamic>);
return TransactionCard(
ontap: () async {},
amount: trans.amount.toStringAsFixed(2),
datetime: trans.date.toString(),
paymentby: trans.paymentmode,
category: trans.category.title,
categoryicon: trans.category.iconurl,
isexpense: trans.isexpense,
);
}); //listview end
} else {
return Container(
child: Center(child: Text('No Transaction Found...')));
}
} else {
if (snapshot.hasError) {
return Text('error found');
} else {
return Text('empty..');
}
}
} else {
return Center(child: CircularProgressIndicator());
}
}),
);
}
【问题讨论】:
标签: flutter