我正在try 制作一款记事本应用程序.我正在通过NoteWidget在主页上显示我用StreamBuilder从Firebase中提取的数据.问题是,我想在NoteWidget中创建一个类似"data[‘note Title’]"的值,并将该值传输到备注编辑页面,这样当我单击主页上的备注名称时,我就可以转到正确的页面.
ListView(
children: [
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('notes').where('user', isEqualTo: user?.uid).where('isDeleted', isEqualTo: false).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return const Text('');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text('');
}
return ExpansionTile(
trailing: Row(
mainAxisSize: MainAxisSize.min,
),
title: const Text(
'Notes',
style: TextStyle(
color: Colors.white,
fontSize: 18,
),
),
children: snapshot.data!.docs
.map((DocumentSnapshot document) {
Map<String, dynamic> data = document.data()! as Map<String, dynamic>;
return NoteWidget(data: data);
})
.toList()
.cast(),
);
}),
class NoteWidget extends StatelessWidget {
const NoteWidget({
super.key,
required this.data,
});
final Map<String, dynamic> data;
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(color: Colors.transparent),
width: double.infinity,
height: 50,
child: Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: EdgeInsets.only(
left: 20.0,
right: 0,
top: 10.0,
bottom: 10.0,
),
child: Icon(
Icons.circle,
size: 5,
color: Colors.white,
),
),
),
Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.only(
left: 10.0,
right: 10.0,
top: 4.0,
bottom: 4.0,
),
child: TextButton(
onPressed: () {
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: ((context) => const EditNotesPage())));
},
child: Text(
data['noteTitle'],
style: const TextStyle(
color: Colors.white,
fontSize: 18,
),
),
),
),
),
const Expanded(
child: SizedBox(
height: 50,
width: 50,
),
),
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(
left: 20.0,
right: 20.0,
top: 10.0,
bottom: 10.0,
),
child: PopupMenuButton<int>(
tooltip: '',
icon: const Icon(
Icons.more_vert_rounded,
size: 24,
color: Colors.white,
),
itemBuilder: (context) => [
// PopupMenuItem 1
PopupMenuItem(
value: 1,
// row with 2 children
child: Row(
children: const [
SizedBox(
width: 10,
),
Text(
'Pin note',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
)
],
),
),
// PopupMenuItem 2
PopupMenuItem(
value: 2,
// row with two children
child: Row(
children: const [
SizedBox(
width: 10,
),
Text(
'Delete note',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
)
],
),
),
],
offset: const Offset(0, 10),
color: const Color(0xff242424),
elevation: 1,
onSelected: (value) async {
if (value == 1) {}
if (value == 2) {
QuerySnapshot querySnap = await FirebaseFirestore.instance.collection('notes').where('docId', isEqualTo: data['docId']).get();
QueryDocumentSnapshot doc = querySnap.docs[0];
DocumentReference docRef = doc.reference;
return docRef.update({
'isDeleted': true
});
}
},
),
),
),
],
),
);
}
}
StreamBuilder和NoteWidget以我想要的方式工作.我只是不知道如何从NoteWidget导入Firebase数据来编辑页面.我是新手,如果你能详细解释解决方案,我会很高兴的.