datatable.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'dart:async';
  2. import 'package:cloud_firestore/cloud_firestore.dart';
  3. import 'package:flutter/material.dart';
  4. class MyDataTable extends StatefulWidget {
  5. const MyDataTable({required this.name, Key? key}) : super(key: key);
  6. final String name;
  7. @override
  8. State<MyDataTable> createState() => _MyDataTableState();
  9. }
  10. class _MyDataTableState extends State<MyDataTable> {
  11. CollectionReference usersList = FirebaseFirestore.instance.collection("Users");
  12. @override
  13. Widget build(BuildContext context) {
  14. Future<List> userList = getUsersList();
  15. return Container(
  16. height: 450,
  17. alignment: const Alignment(0, -1),
  18. child: Scrollbar(
  19. child: SingleChildScrollView(
  20. child: FutureBuilder<dynamic>(
  21. future: userList,
  22. builder: (context, snapshot) {
  23. if (snapshot.connectionState == ConnectionState.waiting) {
  24. return const Text('Wait a moment');
  25. }
  26. if (snapshot.connectionState == ConnectionState.done) {
  27. return buildDataTable(snapshot.data, widget.name);
  28. }
  29. if (snapshot.hasError) {
  30. return const Text('Something went wrong');
  31. }
  32. return const Text(" ");
  33. },
  34. ),
  35. ),
  36. ),
  37. );
  38. }
  39. @override
  40. void initState() {
  41. super.initState();
  42. }
  43. Future<List> getUsersList() async {
  44. var querySnapshot = await usersList.orderBy(widget.name, descending: true).get();
  45. return querySnapshot.docs;
  46. }
  47. }
  48. Widget buildDataTable(snapshotData, String name) {
  49. final columns = ['Place', 'ID', 'Name', 'points'];
  50. return DataTable(
  51. columnSpacing: 10.0,
  52. columns: getColumns(columns),
  53. rows: getRows(snapshotData, name),
  54. );
  55. }
  56. List<DataColumn> getColumns(List<String> columns) {
  57. return columns.map((String column) {
  58. return DataColumn(
  59. label: Text(column),
  60. );
  61. }).toList();
  62. }
  63. List<DataRow> getRows(dynamic snapshotData, String name) {
  64. List<DataRow> rows = [];
  65. int length = snapshotData.length;
  66. for (int i = 0; i < length; i++) {
  67. dynamic user = snapshotData[i];
  68. bool anonymous = user["anonymous"];
  69. if (anonymous) {
  70. rows.add(DataRow(cells: [
  71. DataCell(Text("${i + 1}")),
  72. const DataCell(
  73. Text("anonym"),
  74. ),
  75. const DataCell(
  76. Text("name"),
  77. ),
  78. DataCell(
  79. Text("${user[name]}"),
  80. ),
  81. ]));
  82. } else {
  83. rows.add(DataRow(cells: [
  84. DataCell(Text("${i + 1}")),
  85. DataCell(
  86. Text("${user["shared_id"]}"),
  87. ),
  88. DataCell(
  89. Text(user["name"]),
  90. ),
  91. DataCell(
  92. Text("${user[name]}"),
  93. ),
  94. ]));
  95. }
  96. }
  97. return rows;
  98. }