dialog_table.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import 'package:flutter/material.dart';
  2. import 'alert_dialog.dart';
  3. class VDialogTable extends StatelessWidget {
  4. const VDialogTable({
  5. super.key,
  6. this.title,
  7. required this.columnNames,
  8. required this.tableData,
  9. this.diagnosisTime,
  10. });
  11. final List<List<String>> tableData;
  12. final List<String> columnNames;
  13. final String? title;
  14. final String? diagnosisTime;
  15. Future<DateTime?> show<DateTime>() => VAlertDialog.showDialog<DateTime>(this);
  16. @override
  17. Widget build(BuildContext context) {
  18. return VAlertDialog(
  19. width: 800,
  20. contentPadding: const EdgeInsets.only(left: 16, right: 8),
  21. content: _buildTable(),
  22. showCancel: true,
  23. );
  24. }
  25. Widget _buildTable() {
  26. return SizedBox(
  27. height: 500,
  28. child: Column(
  29. children: [
  30. const SizedBox(
  31. height: 10,
  32. ),
  33. if (title != null)
  34. Text(
  35. title!,
  36. style: const TextStyle(
  37. fontSize: 22,
  38. ),
  39. ),
  40. const SizedBox(
  41. height: 10,
  42. ),
  43. Table(
  44. children: [
  45. TableRow(
  46. decoration: const BoxDecoration(
  47. color: Color.fromRGBO(215, 234, 255, 1),
  48. ),
  49. children: columnNames
  50. .map((columnName) => _buildHeaderCell(columnName))
  51. .toList(),
  52. ),
  53. ],
  54. ),
  55. Expanded(
  56. child: Scrollbar(
  57. thumbVisibility: true,
  58. child: SingleChildScrollView(
  59. child: Table(
  60. children: _buildRow(tableData),
  61. ),
  62. ),
  63. ),
  64. ),
  65. const SizedBox(
  66. height: 10,
  67. ),
  68. Container(
  69. alignment: Alignment.centerLeft,
  70. child: Text(
  71. "检测时间:${diagnosisTime ?? ''}",
  72. style: const TextStyle(fontSize: 18),
  73. )),
  74. const SizedBox(
  75. height: 10,
  76. ),
  77. Container(
  78. alignment: Alignment.centerLeft,
  79. child: const Text(
  80. '注:本结果仅对该检验样本负责!',
  81. style: TextStyle(fontSize: 18),
  82. ))
  83. ],
  84. ),
  85. );
  86. }
  87. List<TableRow> _buildRow(List<List<String>> tableData) {
  88. var tableRows = <TableRow>[];
  89. for (var i = 0; i < tableData.length; i++) {
  90. tableRows.add(TableRow(
  91. decoration: i % 2 == 0
  92. ? null
  93. : const BoxDecoration(
  94. color: Color.fromRGBO(233, 244, 255, 1),
  95. ),
  96. children: tableData[i]
  97. .map(
  98. (cellData) => _buildDataCell(cellData),
  99. )
  100. .toList()));
  101. }
  102. return tableRows;
  103. }
  104. TableCell _buildHeaderCell(String title) {
  105. return TableCell(
  106. child: Container(
  107. padding: const EdgeInsets.symmetric(horizontal: 14),
  108. alignment: Alignment.centerLeft,
  109. height: 37,
  110. child: Text(
  111. title,
  112. style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
  113. ),
  114. ),
  115. );
  116. }
  117. TableCell _buildDataCell(String title) {
  118. return TableCell(
  119. child: Container(
  120. padding: const EdgeInsets.symmetric(horizontal: 14),
  121. alignment: Alignment.centerLeft,
  122. height: 37,
  123. child: Text(
  124. title,
  125. style: const TextStyle(fontSize: 14),
  126. ),
  127. ),
  128. );
  129. }
  130. }