Skip to content

Commit

Permalink
feat: migrate to null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
mdat31 committed Jun 3, 2021
1 parent 2136aac commit 761e5a5
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 80 deletions.
10 changes: 5 additions & 5 deletions lib/entity/candle_entity.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// ignore_for_file: non_constant_identifier_names,library_prefixes,unused_import,camel_case_types
mixin CandleEntity {
double? open;
double? high;
double? low;
double? close;
late double open;
late double high;
late double low;
late double close;

late List<double> maValueList;

// 上轨线
double? up;

// 中轨线
double? mb;
late double mb;

// 下轨线
double? dn;
Expand Down
2 changes: 1 addition & 1 deletion lib/entity/cci_entity.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mixin CCIEntity {
double? cci;
late double cci;
}
20 changes: 10 additions & 10 deletions lib/entity/k_line_entity.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import '../entity/k_entity.dart';

class KLineEntity extends KEntity {
double? open;
double? high;
double? low;
double? close;
double? vol;
late double open;
late double high;
late double low;
late double close;
late double vol;
double? amount;
double? change;
double? ratio;
int? time;

KLineEntity.fromCustom({
this.amount,
this.open,
this.close,
required this.open,
required this.close,
this.change,
this.ratio,
this.time,
this.high,
this.low,
this.vol,
required this.high,
required this.low,
required this.vol,
});

KLineEntity.fromJson(Map<String, dynamic> json) {
Expand Down
6 changes: 3 additions & 3 deletions lib/entity/volume_entity.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ignore_for_file: non_constant_identifier_names,library_prefixes,unused_import,camel_case_types
mixin VolumeEntity {
double? open;
double? close;
double? vol;
late double open;
late double close;
late double vol;
double? MA5Volume;
double? MA10Volume;
}
20 changes: 10 additions & 10 deletions lib/k_chart_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class KChartWidget extends StatefulWidget {
this.onSecondaryTap,
this.volHidden = false,
this.isLine = false,
this.isChinese = true,
this.isChinese = false,
this.timeFormat = TimeFormat.YEAR_MONTH_DAY,
this.onLoadMore,
this.bgColor,
Expand All @@ -77,7 +77,7 @@ class KChartWidget extends StatefulWidget {
class _KChartWidgetState extends State<KChartWidget>
with TickerProviderStateMixin {
double mScaleX = 1.0, mScrollX = 0.0, mSelectX = 0.0;
StreamController<InfoWindowEntity>? mInfoWindowStream;
StreamController<InfoWindowEntity?>? mInfoWindowStream;
double mWidth = 0;
AnimationController? _controller;
Animation<double>? aniX;
Expand Down Expand Up @@ -181,7 +181,7 @@ class _KChartWidgetState extends State<KChartWidget>
},
onLongPressEnd: (details) {
isLongPress = false;
mInfoWindowStream?.sink.add(InfoWindowEntity(KLineEntity.fromCustom()));
mInfoWindowStream?.sink.add(null);
notifyChanged();
},
child: Stack(
Expand Down Expand Up @@ -271,22 +271,22 @@ class _KChartWidgetState extends State<KChartWidget>
late List<String> infos;

Widget _buildInfoDialog() {
return StreamBuilder<InfoWindowEntity>(
return StreamBuilder<InfoWindowEntity?>(
stream: mInfoWindowStream?.stream,
builder: (context, snapshot) {
if (!isLongPress ||
widget.isLine == true ||
!snapshot.hasData ||
snapshot.data?.kLineEntity == null) return Container();
KLineEntity entity = snapshot.data!.kLineEntity;
double upDown = entity.change ?? entity.close! - entity.open!;
double upDownPercent = entity.ratio ?? (upDown / entity.open!) * 100;
double upDown = entity.change ?? entity.close - entity.open;
double upDownPercent = entity.ratio ?? (upDown / entity.open) * 100;
infos = [
getDate(entity.time!),
entity.open!.toStringAsFixed(widget.fixedLength),
entity.high!.toStringAsFixed(widget.fixedLength),
entity.low!.toStringAsFixed(widget.fixedLength),
entity.close!.toStringAsFixed(widget.fixedLength),
entity.open.toStringAsFixed(widget.fixedLength),
entity.high.toStringAsFixed(widget.fixedLength),
entity.low.toStringAsFixed(widget.fixedLength),
entity.close.toStringAsFixed(widget.fixedLength),
"${upDown > 0 ? "+" : ""}${upDown.toStringAsFixed(widget.fixedLength)}",
"${upDownPercent > 0 ? "+" : ''}${upDownPercent.toStringAsFixed(2)}%",
entity.amount!.toInt().toString()
Expand Down
32 changes: 16 additions & 16 deletions lib/renderer/base_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,29 @@ abstract class BaseChartPainter extends CustomPainter {

void getMainMaxMinValue(KLineEntity item, int i) {
if (isLine == true) {
mMainMaxValue = max(mMainMaxValue, item.close!);
mMainMinValue = min(mMainMinValue, item.close!);
mMainMaxValue = max(mMainMaxValue, item.close);
mMainMinValue = min(mMainMinValue, item.close);
} else {
double maxPrice, minPrice;
if (mainState == MainState.MA) {
maxPrice = max(item.high!, _findMaxMA(item.maValueList));
minPrice = min(item.low!, _findMinMA(item.maValueList));
maxPrice = max(item.high, _findMaxMA(item.maValueList));
minPrice = min(item.low, _findMinMA(item.maValueList));
} else if (mainState == MainState.BOLL) {
maxPrice = max(item.up ?? 0, item.high!);
minPrice = min(item.dn ?? 0, item.low!);
maxPrice = max(item.up ?? 0, item.high);
minPrice = min(item.dn ?? 0, item.low);
} else {
maxPrice = item.high!;
minPrice = item.low!;
maxPrice = item.high;
minPrice = item.low;
}
mMainMaxValue = max(mMainMaxValue, maxPrice);
mMainMinValue = min(mMainMinValue, minPrice);

if (mMainHighMaxValue < item.high!) {
mMainHighMaxValue = item.high!;
if (mMainHighMaxValue < item.high) {
mMainHighMaxValue = item.high;
mMainMaxIndex = i;
}
if (mMainLowMinValue > item.low!) {
mMainLowMinValue = item.low!;
if (mMainLowMinValue > item.low) {
mMainLowMinValue = item.low;
mMainMinIndex = i;
}
}
Expand All @@ -222,9 +222,9 @@ abstract class BaseChartPainter extends CustomPainter {

void getVolMaxMinValue(KLineEntity item) {
mVolMaxValue = max(mVolMaxValue,
max(item.vol!, max(item.MA5Volume ?? 0, item.MA10Volume ?? 0)));
max(item.vol, max(item.MA5Volume ?? 0, item.MA10Volume ?? 0)));
mVolMinValue = min(mVolMinValue,
min(item.vol!, min(item.MA5Volume ?? 0, item.MA10Volume ?? 0)));
min(item.vol, min(item.MA5Volume ?? 0, item.MA10Volume ?? 0)));
}

void getSecondaryMaxMinValue(KLineEntity item) {
Expand All @@ -249,8 +249,8 @@ abstract class BaseChartPainter extends CustomPainter {
mSecondaryMaxValue = 0;
mSecondaryMinValue = -100;
} else if (secondaryState == SecondaryState.CCI) {
mSecondaryMaxValue = max(mSecondaryMaxValue, item.cci!);
mSecondaryMinValue = min(mSecondaryMinValue, item.cci!);
mSecondaryMaxValue = max(mSecondaryMaxValue, item.cci);
mSecondaryMinValue = min(mSecondaryMinValue, item.cci);
} else {
mSecondaryMaxValue = 0;
mSecondaryMinValue = 0;
Expand Down
10 changes: 5 additions & 5 deletions lib/renderer/chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'vol_renderer.dart';
class ChartPainter extends BaseChartPainter {
static get maxScrollX => BaseChartPainter.maxScrollX;
BaseChartRenderer? mMainRenderer, mVolRenderer, mSecondaryRenderer;
StreamSink<InfoWindowEntity>? sink;
StreamSink<InfoWindowEntity?>? sink;
Color? upColor, dnColor;
Color? ma5Color, ma10Color, ma30Color;
Color? volColor;
Expand Down Expand Up @@ -73,7 +73,7 @@ class ChartPainter extends BaseChartPainter {
} else {
var t = datas[0];
fixedLength =
NumberUtil.getMaxDecimalLength(t.open!, t.close!, t.high!, t.low!);
NumberUtil.getMaxDecimalLength(t.open, t.close, t.high, t.low);
}
}
mMainRenderer ??= MainRenderer(
Expand Down Expand Up @@ -216,7 +216,7 @@ class ChartPainter extends BaseChartPainter {
double w1 = 5;
double w2 = 3;
double r = textHeight / 2 + w2;
double y = getMainY(point.close!);
double y = getMainY(point.close);
double x;
bool isLeft = false;
if (translateXtoX(getX(index)) < mWidth! / 2) {
Expand Down Expand Up @@ -322,7 +322,7 @@ class ChartPainter extends BaseChartPainter {
void drawNowPrice(Canvas canvas) {
if (isLine == true) return;
double x = translateXtoX(getX(datas.length - 1));
double value = datas[datas.length - 1].close!;
double value = datas[datas.length - 1].close;
double y = getMainY(value);
if (x < mWidth! / 2) {
//画右边
Expand All @@ -347,7 +347,7 @@ class ChartPainter extends BaseChartPainter {
..strokeWidth = this.chartStyle.vCrossWidth
..isAntiAlias = true;
double x = getX(index);
double y = getMainY(point.close!);
double y = getMainY(point.close);
// k线图竖线
canvas.drawLine(Offset(x, mTopPadding),
Offset(x, size.height - mBottomPadding), paintY);
Expand Down
Loading

0 comments on commit 761e5a5

Please sign in to comment.