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 761e5a5 commit 47d4a0c
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 106 deletions.
38 changes: 24 additions & 14 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:k_chart/chart_style.dart';
import 'package:k_chart/flutter_k_chart.dart';
import 'package:k_chart/k_chart_widget.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

Expand Down Expand Up @@ -65,8 +65,8 @@ class _MyHomePageState extends State<MyHomePage> {

void initDepth(List<DepthEntity> bids, List<DepthEntity> asks) {
if (bids == null || asks == null || bids.isEmpty || asks.isEmpty) return;
_bids = List();
_asks = List();
_bids = [];
_asks = [];
double amount = 0.0;
bids?.sort((left, right) => left.price.compareTo(right.price));
//累加买入委托量
Expand Down Expand Up @@ -144,7 +144,8 @@ class _MyHomePageState extends State<MyHomePage> {
button("WR", onPressed: () => _secondaryState = SecondaryState.WR),
button("CCI", onPressed: () => _secondaryState = SecondaryState.CCI),
button("隐藏副视图", onPressed: () => _secondaryState = SecondaryState.NONE),
button(_volHidden ? "显示成交量" : "隐藏成交量", onPressed: () => _volHidden = !_volHidden),
button(_volHidden ? "显示成交量" : "隐藏成交量",
onPressed: () => _volHidden = !_volHidden),
button("切换中英文", onPressed: () => isChinese = !isChinese),
button("Customize UI", onPressed: () {
setState(() {
Expand All @@ -159,15 +160,24 @@ class _MyHomePageState extends State<MyHomePage> {
}

Widget button(String text, {VoidCallback onPressed}) {
return FlatButton(
onPressed: () {
if (onPressed != null) {
onPressed();
setState(() {});
}
},
child: Text("$text"),
color: Colors.blue);
return TextButton(
onPressed: () {
if (onPressed != null) {
onPressed();
setState(() {});
}
},
child: Text("$text"),
style: TextButton.styleFrom(
primary: Colors.white,
minimumSize: Size(88, 44),
padding: EdgeInsets.symmetric(horizontal: 16.0),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2.0)),
),
backgroundColor: Colors.blue,
),
);
}

void getData(String period) {
Expand All @@ -187,7 +197,7 @@ class _MyHomePageState extends State<MyHomePage> {
}).catchError((_) {
showLoading = false;
setState(() {});
print('获取数据失败');
print('### datas error $_');
});
}

Expand Down
83 changes: 45 additions & 38 deletions lib/depth_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ import 'entity/depth_entity.dart';
class DepthChart extends StatefulWidget {
final List<DepthEntity> bids, asks;
final int fixedLength;
final Color buyPathColor;
final Color sellPathColor;
final Color? buyPathColor;
final Color? sellPathColor;
final ChartColors chartColors;

DepthChart(this.bids, this.asks, this.chartColors,
{this.fixedLength = 2,
required this.buyPathColor,
required this.sellPathColor});
DepthChart(
this.bids,
this.asks,
this.chartColors, {
this.fixedLength = 2,
this.buyPathColor,
this.sellPathColor,
});

@override
_DepthChartState createState() => _DepthChartState();
}

class _DepthChartState extends State<DepthChart> {
late Offset pressOffset;
Offset? pressOffset;
bool isLongPress = false;

@override
Expand Down Expand Up @@ -62,8 +66,8 @@ class _DepthChartState extends State<DepthChart> {

class DepthChartPainter extends CustomPainter {
//买入//卖出
List<DepthEntity> mBuyData, mSellData;
Offset pressOffset;
List<DepthEntity>? mBuyData, mSellData;
Offset? pressOffset;
bool isLongPress;
int? fixedLength;
Color? mBuyPathColor, mSellPathColor;
Expand Down Expand Up @@ -125,9 +129,12 @@ class DepthChartPainter extends CustomPainter {
}

void init() {
if (mBuyData.isEmpty || mSellData.isEmpty) return;
mMaxVolume = mBuyData[0].vol;
mMaxVolume = max(mMaxVolume!, mSellData.last.vol);
if (mBuyData == null ||
mBuyData!.isEmpty ||
mSellData == null ||
mSellData!.isEmpty) return;
mMaxVolume = mBuyData![0].vol;
mMaxVolume = max(mMaxVolume!, mSellData!.last.vol);
mMaxVolume = mMaxVolume! * 1.05;
mMultiple = mMaxVolume! / mLineCount;
fixedLength ??= 2;
Expand All @@ -146,8 +153,8 @@ class DepthChartPainter extends CustomPainter {
void paint(Canvas canvas, Size size) {
if (mBuyData == null ||
mSellData == null ||
mBuyData.isEmpty ||
mSellData.isEmpty) return;
mBuyData!.isEmpty ||
mSellData!.isEmpty) return;
mWidth = size.width;
mDrawWidth = mWidth / 2;
mDrawHeight = size.height - mPaddingBottom;
Expand All @@ -165,25 +172,25 @@ class DepthChartPainter extends CustomPainter {

void drawBuy(Canvas canvas) {
mBuyPointWidth =
(mDrawWidth / (mBuyData.length - 1 == 0 ? 1 : mBuyData.length - 1));
(mDrawWidth / (mBuyData!.length - 1 == 0 ? 1 : mBuyData!.length - 1));
mBuyPath!.reset();
double x;
double y;
for (int i = 0; i < mBuyData.length; i++) {
for (int i = 0; i < mBuyData!.length; i++) {
if (i == 0) {
mBuyPath!.moveTo(0, getY(mBuyData[0].vol));
mBuyPath!.moveTo(0, getY(mBuyData![0].vol));
}
x = mBuyPointWidth! * i;
y = getY(mBuyData[i].vol);
y = getY(mBuyData![i].vol);
if (i >= 1) {
canvas.drawLine(
Offset(mBuyPointWidth! * (i - 1), getY(mBuyData[i - 1].vol)),
Offset(mBuyPointWidth! * (i - 1), getY(mBuyData![i - 1].vol)),
Offset(x, y),
mBuyLinePaint!);
}
if (i != mBuyData.length - 1) {
if (i != mBuyData!.length - 1) {
mBuyPath!.quadraticBezierTo(
x, y, mBuyPointWidth! * (i + 1), getY(mBuyData[i + 1].vol));
x, y, mBuyPointWidth! * (i + 1), getY(mBuyData![i + 1].vol));
} else {
if (i == 0) {
mBuyPath!.lineTo(mDrawWidth, y);
Expand All @@ -201,29 +208,29 @@ class DepthChartPainter extends CustomPainter {

void drawSell(Canvas canvas) {
mSellPointWidth =
(mDrawWidth / (mSellData.length - 1 == 0 ? 1 : mSellData.length - 1));
(mDrawWidth / (mSellData!.length - 1 == 0 ? 1 : mSellData!.length - 1));
mSellPath!.reset();
double x;
double y;
for (int i = 0; i < mSellData.length; i++) {
for (int i = 0; i < mSellData!.length; i++) {
if (i == 0) {
mSellPath!.moveTo(mDrawWidth, getY(mSellData[0].vol));
mSellPath!.moveTo(mDrawWidth, getY(mSellData![0].vol));
}
x = (mSellPointWidth! * i) + mDrawWidth;
y = getY(mSellData[i].vol);
y = getY(mSellData![i].vol);
if (i >= 1) {
canvas.drawLine(
Offset((mSellPointWidth! * (i - 1)) + mDrawWidth,
getY(mSellData[i - 1].vol)),
getY(mSellData![i - 1].vol)),
Offset(x, y),
mSellLinePaint!);
}
if (i != mSellData.length - 1) {
if (i != mSellData!.length - 1) {
mSellPath!.quadraticBezierTo(
x,
y,
(mSellPointWidth! * (i + 1)) + mDrawWidth,
getY(mSellData[i + 1].vol));
getY(mSellData![i + 1].vol));
} else {
if (i == 0) {
mSellPath!.lineTo(mWidth, y);
Expand Down Expand Up @@ -255,12 +262,12 @@ class DepthChartPainter extends CustomPainter {
mWidth - tp.width, mDrawHeight / mLineCount * j + tp.height / 2));
}

var startText = mBuyData.first.price.toStringAsFixed(fixedLength!);
var startText = mBuyData!.first.price.toStringAsFixed(fixedLength!);
TextPainter startTP = getTextPainter(startText);
startTP.layout();
startTP.paint(canvas, Offset(0, getBottomTextY(startTP.height)));

double centerPrice = (mBuyData.last.price + mSellData.first.price) / 2;
double centerPrice = (mBuyData!.last.price + mSellData!.first.price) / 2;

var center = centerPrice.toStringAsFixed(fixedLength!);
TextPainter centerTP = getTextPainter(center);
Expand All @@ -270,13 +277,13 @@ class DepthChartPainter extends CustomPainter {
Offset(
mDrawWidth - centerTP.width / 2, getBottomTextY(centerTP.height)));

var endText = mSellData.last.price.toStringAsFixed(fixedLength!);
var endText = mSellData!.last.price.toStringAsFixed(fixedLength!);
TextPainter endTP = getTextPainter(endText);
endTP.layout();
endTP.paint(
canvas, Offset(mWidth - endTP.width, getBottomTextY(endTP.height)));

var leftHalfText = ((mBuyData.first.price + centerPrice) / 2)
var leftHalfText = ((mBuyData!.first.price + centerPrice) / 2)
.toStringAsFixed(fixedLength!);
TextPainter leftHalfTP = getTextPainter(leftHalfText);
leftHalfTP.layout();
Expand All @@ -285,7 +292,7 @@ class DepthChartPainter extends CustomPainter {
Offset((mDrawWidth - leftHalfTP.width) / 2,
getBottomTextY(leftHalfTP.height)));

var rightHalfText = ((mSellData.last.price + centerPrice) / 2)
var rightHalfText = ((mSellData!.last.price + centerPrice) / 2)
.toStringAsFixed(fixedLength!);
TextPainter rightHalfTP = getTextPainter(rightHalfText);
rightHalfTP.layout();
Expand All @@ -295,20 +302,20 @@ class DepthChartPainter extends CustomPainter {
getBottomTextY(rightHalfTP.height)));

if (isLongPress == true) {
if (pressOffset.dx <= mDrawWidth) {
int index =
_indexOfTranslateX(pressOffset.dx, 0, mBuyData.length - 1, getBuyX);
if (pressOffset!.dx <= mDrawWidth) {
int index = _indexOfTranslateX(
pressOffset!.dx, 0, mBuyData!.length - 1, getBuyX);
drawSelectView(canvas, index, true);
} else {
int index = _indexOfTranslateX(
pressOffset.dx, 0, mSellData.length - 1, getSellX);
pressOffset!.dx, 0, mSellData!.length - 1, getSellX);
drawSelectView(canvas, index, false);
}
}
}

void drawSelectView(Canvas canvas, int index, bool isLeft) {
DepthEntity entity = isLeft ? mBuyData[index] : mSellData[index];
DepthEntity entity = isLeft ? mBuyData![index] : mSellData![index];
double dx = isLeft ? getBuyX(index) : getSellX(index);

double radius = 8.0;
Expand Down
2 changes: 1 addition & 1 deletion lib/entity/candle_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mixin CandleEntity {
double? up;

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

// 下轨线
double? dn;
Expand Down
20 changes: 10 additions & 10 deletions lib/entity/k_line_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ class KLineEntity extends KEntity {
});

KLineEntity.fromJson(Map<String, dynamic> json) {
open = (json['open'] as num).toDouble();
high = (json['high'] as num).toDouble();
low = (json['low'] as num).toDouble();
close = (json['close'] as num).toDouble();
vol = (json['vol'] as num).toDouble();
amount = (json['amount'] as num).toDouble();
time = (json['time'] as num).toInt();
open = json['open']?.toDouble();
high = json['high']?.toDouble();
low = json['low']?.toDouble();
close = json['close']?.toDouble();
vol = json['vol']?.toDouble();
amount = json['amount']?.toDouble();
time = json['time']?.toInt();
//兼容火币数据
if (time == null) {
time = ((json['id'] as num).toInt());
time = (json['id']?.toInt());
if (time != null) {
time = time! * 1000;
}
}
ratio = (json['ratio'] as num).toDouble();
change = (json['change'] as num).toDouble();
ratio = json['ratio']?.toDouble();
change = json['change']?.toDouble();
}

Map<String, dynamic> toJson() {
Expand Down
7 changes: 4 additions & 3 deletions lib/k_chart_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TimeFormat {
}

class KChartWidget extends StatefulWidget {
final List<KLineEntity> datas;
final List<KLineEntity>? datas;
final MainState mainState;
final bool volHidden;
final SecondaryState secondaryState;
Expand Down Expand Up @@ -110,7 +110,7 @@ class _KChartWidgetState extends State<KChartWidget>

@override
Widget build(BuildContext context) {
if (widget.datas.isEmpty) {
if (widget.datas != null && widget.datas!.isEmpty) {
mScrollX = mSelectX = 0.0;
mScaleX = 1.0;
}
Expand Down Expand Up @@ -218,7 +218,8 @@ class _KChartWidgetState extends State<KChartWidget>
duration: Duration(milliseconds: widget.flingTime), vsync: this);
aniX = null;
aniX = Tween<double>(begin: mScrollX, end: x * widget.flingRatio + mScrollX)
.animate(CurvedAnimation(parent: aniX!, curve: widget.flingCurve));
.animate(CurvedAnimation(
parent: _controller!.view, curve: widget.flingCurve));
aniX!.addListener(() {
mScrollX = aniX!.value;
if (mScrollX <= 0) {
Expand Down
Loading

0 comments on commit 47d4a0c

Please sign in to comment.