/
main.dart
272 lines (254 loc) · 8.86 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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_translations.dart';
import 'package:k_chart/flutter_k_chart.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<KLineEntity>? datas;
bool showLoading = true;
MainState _mainState = MainState.MA;
bool _volHidden = false;
SecondaryState _secondaryState = SecondaryState.MACD;
bool isLine = true;
bool isChinese = true;
bool _hideGrid = false;
bool _showNowPrice = true;
List<DepthEntity>? _bids, _asks;
bool isChangeUI = false;
bool _isTrendLine = false;
bool _priceLeft = true;
VerticalTextAlignment _verticalTextAlignment = VerticalTextAlignment.left;
ChartStyle chartStyle = ChartStyle();
ChartColors chartColors = ChartColors();
@override
void initState() {
super.initState();
getData('1day');
rootBundle.loadString('assets/depth.json').then((result) {
final parseJson = json.decode(result);
final tick = parseJson['tick'] as Map<String, dynamic>;
final List<DepthEntity> bids = (tick['bids'] as List<dynamic>)
.map<DepthEntity>(
(item) => DepthEntity(item[0] as double, item[1] as double))
.toList();
final List<DepthEntity> asks = (tick['asks'] as List<dynamic>)
.map<DepthEntity>(
(item) => DepthEntity(item[0] as double, item[1] as double))
.toList();
initDepth(bids, asks);
});
}
void initDepth(List<DepthEntity>? bids, List<DepthEntity>? asks) {
if (bids == null || asks == null || bids.isEmpty || asks.isEmpty) return;
_bids = [];
_asks = [];
double amount = 0.0;
bids.sort((left, right) => left.price.compareTo(right.price));
//累加买入委托量
bids.reversed.forEach((item) {
amount += item.vol;
item.vol = amount;
_bids!.insert(0, item);
});
amount = 0.0;
asks.sort((left, right) => left.price.compareTo(right.price));
//累加卖出委托量
asks.forEach((item) {
amount += item.vol;
item.vol = amount;
_asks!.add(item);
});
setState(() {});
}
@override
Widget build(BuildContext context) {
return ListView(
shrinkWrap: true,
children: <Widget>[
Stack(children: <Widget>[
Container(
height: 450,
width: double.infinity,
child: KChartWidget(
datas,
chartStyle,
chartColors,
isLine: isLine,
onSecondaryTap: () {
print('Secondary Tap');
},
isTrendLine: _isTrendLine,
mainState: _mainState,
volHidden: _volHidden,
secondaryState: _secondaryState,
fixedLength: 2,
timeFormat: TimeFormat.YEAR_MONTH_DAY,
translations: kChartTranslations,
showNowPrice: _showNowPrice,
//`isChinese` is Deprecated, Use `translations` instead.
isChinese: isChinese,
hideGrid: _hideGrid,
isTapShowInfoDialog: false,
verticalTextAlignment: _verticalTextAlignment,
maDayList: [1, 100, 1000],
),
),
if (showLoading)
Container(
width: double.infinity,
height: 450,
alignment: Alignment.center,
child: const CircularProgressIndicator()),
]),
buildButtons(),
if (_bids != null && _asks != null)
Container(
height: 230,
width: double.infinity,
child: DepthChart(_bids!, _asks!, chartColors),
)
],
);
}
Widget buildButtons() {
return Wrap(
alignment: WrapAlignment.spaceEvenly,
children: <Widget>[
button("Time Mode", onPressed: () => isLine = true),
button("K Line Mode", onPressed: () => isLine = false),
button("TrendLine", onPressed: () => _isTrendLine = !_isTrendLine),
button("Line:MA", onPressed: () => _mainState = MainState.MA),
button("Line:BOLL", onPressed: () => _mainState = MainState.BOLL),
button("Hide Line", onPressed: () => _mainState = MainState.NONE),
button("Secondary Chart:MACD",
onPressed: () => _secondaryState = SecondaryState.MACD),
button("Secondary Chart:KDJ",
onPressed: () => _secondaryState = SecondaryState.KDJ),
button("Secondary Chart:RSI",
onPressed: () => _secondaryState = SecondaryState.RSI),
button("Secondary Chart:WR",
onPressed: () => _secondaryState = SecondaryState.WR),
button("Secondary Chart:CCI",
onPressed: () => _secondaryState = SecondaryState.CCI),
button("Secondary Chart:Hide",
onPressed: () => _secondaryState = SecondaryState.NONE),
button(_volHidden ? "Show Vol" : "Hide Vol",
onPressed: () => _volHidden = !_volHidden),
button("Change Language", onPressed: () => isChinese = !isChinese),
button(_hideGrid ? "Show Grid" : "Hide Grid",
onPressed: () => _hideGrid = !_hideGrid),
button(_showNowPrice ? "Hide Now Price" : "Show Now Price",
onPressed: () => _showNowPrice = !_showNowPrice),
button("Customize UI", onPressed: () {
setState(() {
this.isChangeUI = !this.isChangeUI;
if (this.isChangeUI) {
chartColors.selectBorderColor = Colors.red;
chartColors.selectFillColor = Colors.red;
chartColors.lineFillColor = Colors.red;
chartColors.kLineColor = Colors.yellow;
} else {
chartColors.selectBorderColor = Color(0xff6C7A86);
chartColors.selectFillColor = Color(0xff0D1722);
chartColors.lineFillColor = Color(0x554C86CD);
chartColors.kLineColor = Color(0xff4C86CD);
}
});
}),
button("Change PriceTextPaint",
onPressed: () => setState(() {
_priceLeft = !_priceLeft;
if (_priceLeft) {
_verticalTextAlignment = VerticalTextAlignment.left;
} else {
_verticalTextAlignment = VerticalTextAlignment.right;
}
})),
],
);
}
Widget button(String text, {VoidCallback? onPressed}) {
return TextButton(
onPressed: () {
if (onPressed != null) {
onPressed();
setState(() {});
}
},
child: Text(text),
style: TextButton.styleFrom(
primary: Colors.white,
minimumSize: const Size(88, 44),
padding: const EdgeInsets.symmetric(horizontal: 16.0),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2.0)),
),
backgroundColor: Colors.blue,
),
);
}
void getData(String period) {
/*
* 可以翻墙使用方法1加载数据,不可以翻墙使用方法2加载数据,默认使用方法1加载最新数据
*/
final Future<String> future = getChatDataFromInternet(period);
//final Future<String> future = getChatDataFromJson();
future.then((String result) {
solveChatData(result);
}).catchError((_) {
showLoading = false;
setState(() {});
print('### datas error $_');
});
}
//获取火币数据,需要翻墙
Future<String> getChatDataFromInternet(String? period) async {
var url =
'https://api.huobi.br.com/market/history/kline?period=${period ?? '1day'}&size=300&symbol=btcusdt';
late String result;
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
result = response.body;
} else {
print('Failed getting IP address');
}
return result;
}
// 如果你不能翻墙,可以使用这个方法加载数据
Future<String> getChatDataFromJson() async {
return rootBundle.loadString('assets/chatData.json');
}
void solveChatData(String result) {
final Map parseJson = json.decode(result) as Map<dynamic, dynamic>;
final list = parseJson['data'] as List<dynamic>;
datas = list
.map((item) => KLineEntity.fromJson(item as Map<String, dynamic>))
.toList()
.reversed
.toList()
.cast<KLineEntity>();
DataUtil.calculate(datas!);
showLoading = false;
setState(() {});
}
}