line_chart.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.addAll(this.children.map((e) {
  51. final row = <Widget>[];
  52. for (var (i, one) in e.cols.indexed) {
  53. final t = titles.elementAt(i);
  54. row.add(t.width != null
  55. ? Container(
  56. width: t.width!, alignment: Alignment.center, child: one)
  57. : Expanded(flex: t.flex!, child: Center(child: one)));
  58. if (i != e.cols.length - 1) {
  59. row.add(SizedBox(
  60. height: 17.78, child: VerticalDivider(width: dividerWidth)));
  61. }
  62. }
  63. return Container(
  64. margin: const EdgeInsets.only(top: 2),
  65. decoration: BoxDecoration(
  66. color: Colors.white,
  67. border: Border.all(
  68. color: const Color(0xffe9e9e9), width: borderWidth),
  69. boxShadow: const [
  70. BoxShadow(color: Color(0x4d000000), blurRadius: 0.71)
  71. ]),
  72. height: lineHeight,
  73. child: Row(
  74. children: row,
  75. ));
  76. }));
  77. return Column(
  78. mainAxisSize: MainAxisSize.min,
  79. children: children,
  80. );
  81. }
  82. }