flutter_k_chart基本实现了火币里面各项指标线以及深度线,Flutter目前没有比较好的开源组件,就自己写了一个,声明式ui自定义起来有点怪怪的,但大致与android差不多,先上效果图
k_chart.2019-09-01 10_19_56.gif
depth.2019-09-01 10_21_31.gif
screenshots.png
使用方式
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;
SecondaryState _secondaryState = SecondaryState.MACD;
bool isLine = true;
List<DepthEntity> _bids, _asks;
@override
void initState() {
super.initState();
getData('1day');
rootBundle.loadString('assets/depth.json').then((result) {
final parseJson = json.decode(result);
Map tick = parseJson['tick'];
var bids = tick['bids'].map((item) => DepthEntity(item[0], item[1])).toList().cast<DepthEntity>();
var asks = tick['asks'].map((item) => DepthEntity(item[0], item[1])).toList().cast<DepthEntity>();
initDepth(bids, asks);
});
}
void initDepth(List<DepthEntity> bids, List<DepthEntity> asks) {
if (bids == null || asks == null || bids.isEmpty || asks.isEmpty) return;
_bids = List();
_asks = List();
double amount = 0.0;
bids?.sort((left, right) => left.price.compareTo(right.price));
//倒序循环 //累加买入委托量
bids.reversed.forEach((item) {
amount += item.amount;
item.amount = amount;
_bids.insert(0, item);
});
amount = 0.0;
asks?.sort((left, right) => left.price.compareTo(right.price));
//倒序循环 //累加买入委托量
asks?.forEach((item) {
amount += item.amount;
item.amount = amount;
_asks.add(item);
});
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff17212F),
// appBar: AppBar(title: Text(widget.title)),
body: ListView(
children: <Widget>[
Stack(children: <Widget>[
Container(
height: 450,
width: double.infinity,
child: KChartWidget(
datas,
isLine: isLine,
mainState: _mainState,
secondaryState: _secondaryState,
),
),
if (showLoading)
Container(
width: double.infinity,
height: 450,
alignment: Alignment.center,
child: CircularProgressIndicator()),
]),
buildButtons(),
Container(
height: 230,
width: double.infinity,
child: DepthChart(_bids, _asks),
)
],
),
);
}
Widget buildButtons() {
return Wrap(
alignment: WrapAlignment.spaceEvenly,
children: <Widget>[
button("分时", onPressed: () => isLine = true),
button("k线", onPressed: () => isLine = false),
button("MA", onPressed: () => _mainState = MainState.MA),
button("BOLL", onPressed: () => _mainState = MainState.BOLL),
button("隐藏", onPressed: () => _mainState = MainState.NONE),
button("MACD", onPressed: () => _secondaryState = SecondaryState.MACD),
button("KDJ", onPressed: () => _secondaryState = SecondaryState.KDJ),
button("RSI", onPressed: () => _secondaryState = SecondaryState.RSI),
button("WR", onPressed: () => _secondaryState = SecondaryState.WR),
button("隐藏副视图", onPressed: () => _secondaryState = SecondaryState.NONE),
],
);
}
Widget button(String text, {VoidCallback onPressed}) {
return FlatButton(
onPressed: () {
if (onPressed != null) {
onPressed();
setState(() {});
}
},
child: Text("$text"),
color: Colors.blue);
}
void getData(String period) {
Future<String> future = getIPAddress('$period');
future.then((result) {
Map parseJson = json.decode(result);
List list = parseJson['data'];
datas = list.map((item) => KLineEntity.fromJson(item)).toList().reversed.toList().cast<KLineEntity>();
DataUtil.calculate(datas);
showLoading = false;
setState(() {});
}).catchError((_) {
showLoading = false;
setState(() {});
print('获取数据失败');
});
}
Future<String> getIPAddress(String period) async {
var url =
'https://api.huobi.br.com/market/history/kline?period=${period ?? '1day'}&size=300&symbol=btcusdt';
String result;
var response = await http.get(url);
if (response.statusCode == 200) {
result = response.body;
} else {
print('Failed getting IP address');
}
return result;
}
}
GitHub地址:https://github.com/gwhcn/flutter_k_chart
后期会持续完善,欢迎指出问题以及star
网友评论