Skip to content

Instantly share code, notes, and snippets.

@anoochit
Created June 2, 2022 02:10
Show Gist options
  • Select an option

  • Save anoochit/1a8a14d08e67716728c14fe80db36107 to your computer and use it in GitHub Desktop.

Select an option

Save anoochit/1a8a14d08e67716728c14fe80db36107 to your computer and use it in GitHub Desktop.
sample dropdown in future builder
// get log history
FutureBuilder(
future: FirebaseFirestore.instance.collection('sick').doc(widget.idcard).collection('logs').get(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
if (!snapshot.hasData) {
return Text('Loading...');
}
// get data from firebase
List<DocumentSnapshot> logDocument = snapshot.data!.docs;
return Center(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16),
decoration: BoxDecoration(
color: Color(0xfff29a94),
borderRadius: BorderRadius.circular(32.0),
),
child: DropdownButton(
hint: Text('ประวัติบันทึกข้อมูล'),
items: logDocument.map((doc) {
// get datetime format
DateTime date = DateTime.fromMillisecondsSinceEpoch(int.parse(doc['timestamp']));
final datetimeTitle = DateFormat('dd MMMM yyyy HH:mm').format(date);
return DropdownMenuItem(
child: Text(datetimeTitle),
value: doc['timestamp'],
);
}).toList(),
onChanged: (value) {
// find a doocument with docId = value
// open new page with data
dev.log('goto page with docId = $value');
},
),
),
);
},
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment