line_chart.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'package:flutter/material.dart';
  2. class LineChartTitle {
  3. const LineChartTitle({required this.title, this.width, this.flex});
  4. final Widget title;
  5. final double? width;
  6. final int? flex;
  7. }
  8. class LineChartElem {
  9. const LineChartElem(this.cols);
  10. final Iterable<Widget> cols;
  11. }
  12. class LineChart extends StatelessWidget {
  13. const LineChart(
  14. {super.key,
  15. required this.titles,
  16. required this.children,
  17. this.lineHeight});
  18. final Iterable<LineChartTitle> titles;
  19. final Iterable<LineChartElem> children;
  20. final double? lineHeight;
  21. final dividerWidth = 0.71;
  22. final borderWidth = 0.71;
  23. @override
  24. Widget build(BuildContext context) {
  25. final lineHeight = this.lineHeight ?? 38.5;
  26. final titlesChildren = <Widget>[];
  27. for (var (i, e) in titles.indexed) {
  28. titlesChildren.add(e.width != null
  29. ? Container(
  30. alignment: Alignment.center, width: e.width!, child: e.title)
  31. : Expanded(
  32. flex: e.flex!,
  33. child: Center(child: e.title),
  34. ));
  35. if (i < titles.length - 1) {
  36. titlesChildren.add(SizedBox(
  37. width: dividerWidth,
  38. ));
  39. }
  40. }
  41. final title = Container(
  42. margin: EdgeInsets.all(borderWidth),
  43. height: lineHeight,
  44. child: Row(
  45. children: titlesChildren,
  46. ));
  47. final children = <Widget>[
  48. title,
  49. ];
  50. children.add(Expanded(
  51. child: ListView(
  52. children: this.children.map((e) {
  53. final row = <Widget>[];
  54. for (var (i, one) in e.cols.indexed) {
  55. final t = titles.elementAt(i);
  56. row.add(t.width != null
  57. ? Container(
  58. width: t.width!, alignment: Alignment.center, child: one)
  59. : Expanded(flex: t.flex!, child: Center(child: one)));
  60. if (i != e.cols.length - 1) {
  61. row.add(SizedBox(
  62. height: 17.78, child: VerticalDivider(width: dividerWidth)));
  63. }
  64. }
  65. return Container(
  66. margin: const EdgeInsets.only(top: 2),
  67. decoration: BoxDecoration(
  68. color: Colors.white,
  69. border: Border.all(
  70. color: const Color(0xffe9e9e9), width: borderWidth),
  71. boxShadow: const [
  72. BoxShadow(color: Color(0x4d000000), blurRadius: 0.71)
  73. ]),
  74. height: lineHeight,
  75. child: Row(
  76. children: row,
  77. ));
  78. }).toList())));
  79. return Column(
  80. mainAxisSize: MainAxisSize.min,
  81. children: children,
  82. );
  83. }
  84. }