popup_info_view.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:flutter/material.dart';
  2. import 'package:k_chart_plus/chart_style.dart';
  3. import 'package:k_chart_plus/chart_translations.dart';
  4. import '../entity/k_line_entity.dart';
  5. import '../utils/date_format_util.dart';
  6. import '../utils/number_util.dart';
  7. class PopupInfoView extends StatelessWidget {
  8. final KLineEntity entity;
  9. final double width;
  10. final ChartColors chartColors;
  11. final ChartTranslations chartTranslations;
  12. final bool materialInfoDialog;
  13. final List<String> timeFormat;
  14. final int fixedLength;
  15. const PopupInfoView({
  16. Key? key,
  17. required this.entity,
  18. required this.width,
  19. required this.chartColors,
  20. required this.chartTranslations,
  21. required this.materialInfoDialog,
  22. required this.timeFormat,
  23. required this.fixedLength,
  24. }) : super(key: key);
  25. @override
  26. Widget build(BuildContext context) {
  27. return DecoratedBox(
  28. decoration: BoxDecoration(
  29. color: chartColors.selectFillColor,
  30. border: Border.all(color: chartColors.selectBorderColor, width: 0.5),
  31. ),
  32. child: SizedBox(
  33. width: width,
  34. child: Padding(
  35. padding: EdgeInsets.fromLTRB(6.0, 6.0, 6.0, 0.0),
  36. child: _buildBody(context),
  37. ),
  38. ),
  39. );
  40. }
  41. Widget _buildBody(BuildContext context) {
  42. double upDown = entity.change ?? entity.close - entity.open;
  43. double upDownPercent = entity.ratio ?? (upDown / entity.open) * 100;
  44. final double? entityAmount = entity.amount;
  45. return Column(
  46. crossAxisAlignment: CrossAxisAlignment.start,
  47. mainAxisAlignment: MainAxisAlignment.start,
  48. mainAxisSize: MainAxisSize.min,
  49. children: [
  50. _buildItem(chartTranslations.date, getDate(entity.time)),
  51. _buildItem(
  52. chartTranslations.open, entity.open.toStringAsFixed(fixedLength)),
  53. _buildItem(
  54. chartTranslations.high, entity.high.toStringAsFixed(fixedLength)),
  55. _buildItem(
  56. chartTranslations.low, entity.low.toStringAsFixed(fixedLength)),
  57. _buildItem(
  58. chartTranslations.close, entity.close.toStringAsFixed(fixedLength)),
  59. _buildColorItem(chartTranslations.changeAmount,
  60. upDown.toStringAsFixed(fixedLength), upDown > 0),
  61. _buildColorItem(chartTranslations.change,
  62. '${upDownPercent.toStringAsFixed(2)}%', upDownPercent > 0),
  63. _buildItem(chartTranslations.vol, NumberUtil.format(entity.vol)),
  64. if (entityAmount != null)
  65. _buildItem(chartTranslations.amount, entityAmount.toInt().toString()),
  66. ],
  67. );
  68. }
  69. Widget _buildColorItem(String label, String info, bool isUp) {
  70. if (isUp) {
  71. return _buildItem(label, '+$info',
  72. textColor: chartColors.infoWindowUpColor);
  73. }
  74. return _buildItem(label, info, textColor: chartColors.infoWindowDnColor);
  75. }
  76. Widget _buildItem(String label, String info, {Color? textColor}) {
  77. final infoWidget = Padding(
  78. padding: const EdgeInsets.only(bottom: 3.0),
  79. child: Row(
  80. mainAxisAlignment: MainAxisAlignment.start,
  81. crossAxisAlignment: CrossAxisAlignment.center,
  82. children: <Widget>[
  83. Text(
  84. label,
  85. style: TextStyle(
  86. color: chartColors.infoWindowTitleColor,
  87. fontSize: 10.0,
  88. ),
  89. ),
  90. Expanded(
  91. child: Text(
  92. info,
  93. style: TextStyle(
  94. color: textColor ?? chartColors.infoWindowNormalColor,
  95. fontSize: 10.0),
  96. textAlign: TextAlign.right,
  97. ),
  98. ),
  99. ],
  100. ),
  101. );
  102. return materialInfoDialog
  103. ? Material(color: Colors.transparent, child: infoWidget)
  104. : infoWidget;
  105. }
  106. String getDate(int? date) => dateFormat(
  107. DateTime.fromMillisecondsSinceEpoch(
  108. date ?? DateTime.now().millisecondsSinceEpoch),
  109. timeFormat,
  110. );
  111. }