feat: K线数据使用Decimal类型,防止数值精度丢失和溢出
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_chen_kchart/k_chart.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shadcn_ui/shadcn_ui.dart';
|
||||
import 'package:decimal/decimal.dart';
|
||||
import '../../../core/theme/app_spacing.dart';
|
||||
import '../../../core/theme/app_theme.dart';
|
||||
import '../../../core/theme/app_theme_extension.dart';
|
||||
@@ -144,12 +145,14 @@ class ChartPage extends StatelessWidget {
|
||||
final lastCandle = candles.last;
|
||||
final firstCandle = candles.first;
|
||||
final change = lastCandle.close - firstCandle.open;
|
||||
final changePercent = firstCandle.open != 0 ? (change / firstCandle.open) * 100 : 0;
|
||||
final isUp = change >= 0;
|
||||
final changePercent = firstCandle.open == Decimal.zero
|
||||
? 0.0
|
||||
: (change.toDouble() / firstCandle.openDouble * 100);
|
||||
final isUp = change >= Decimal.fromInt(0);
|
||||
|
||||
double high24h = 0;
|
||||
double low24h = double.infinity;
|
||||
double volume24h = 0;
|
||||
Decimal high24h = Decimal.fromInt(0);
|
||||
Decimal low24h = Decimal.parse('999999999999');
|
||||
Decimal volume24h = Decimal.fromInt(0);
|
||||
for (var c in candles) {
|
||||
if (c.high > high24h) high24h = c.high;
|
||||
if (c.low < low24h) low24h = c.low;
|
||||
@@ -171,7 +174,7 @@ class ChartPage extends StatelessWidget {
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(
|
||||
'\$${lastCandle.close.toStringAsFixed(2)}',
|
||||
'\$${lastCandle.closeDouble.toStringAsFixed(2)}',
|
||||
style: AppTextStyles.displaySmall(context).copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: isUp ? context.appColors.up : context.appColors.down,
|
||||
@@ -197,11 +200,11 @@ class ChartPage extends StatelessWidget {
|
||||
const SizedBox(height: AppSpacing.sm),
|
||||
Row(
|
||||
children: [
|
||||
_buildDataItem(context, '24h高', '\$${high24h.toStringAsFixed(2)}'),
|
||||
_buildDataItem(context, '24h高', '\$${high24h.toDouble().toStringAsFixed(2)}'),
|
||||
const SizedBox(width: AppSpacing.lg),
|
||||
_buildDataItem(context, '24h低', '\$${low24h.toStringAsFixed(2)}'),
|
||||
_buildDataItem(context, '24h低', '\$${low24h.toDouble().toStringAsFixed(2)}'),
|
||||
const SizedBox(width: AppSpacing.lg),
|
||||
_buildDataItem(context, '24h量', '${(volume24h / 1000).toStringAsFixed(1)}K'),
|
||||
_buildDataItem(context, '24h量', '${(volume24h.toDouble() / 1000).toStringAsFixed(1)}K'),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user