在寫Flutter過程當中,藉助了一些給力的第三方包,欣賞了一些源碼,頗有吸引力,那麼本篇章就來帶你們夥梳理一下性價比高,耐用的packages。Flutter 之旅會持續Update。但願你在下班上班的路上看到。git
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import './show_detail_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class Test extends StatefulWidget {
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
String year;
int sales;
bool flag = false; // 定義是否點擊
//點擊柱狀圖觸發的函數
_onSelectionChanged(charts.SelectionModel model) {
final selectedDatum = model.selectedDatum;
print(selectedDatum.first.datum.year);
print(selectedDatum.first.datum.sales);
print(selectedDatum.first.series.displayName);
setState(() {
// 改變標記狀態
flag = true;
//改變兩個顯示的數值
year = selectedDatum.first.datum.year;
sales = selectedDatum.first.datum.sales;
});
}
@override
Widget build(BuildContext context) {
return Container(
width: ScreenUtil.getInstance().setWidth(700),
height: ScreenUtil.getInstance().setHeight(500),
decoration:
BoxDecoration(border: Border.all(width: 1, color: Colors.red)),
child: Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(width: 1, color: Colors.blueAccent)),
width: double.infinity,
height: ScreenUtil.getInstance().setHeight(500),
child: charts.BarChart(
//經過下面獲取數據傳入
ChartFlutterBean.createSampleData(),
//配置項,以及設置觸發的函數
selectionModels: [
charts.SelectionModelConfig(
type: charts.SelectionModelType.info,
changedListener: _onSelectionChanged,
)
],
),
),
_showMask(flag, 4.0)
],
),
);
}
// 自定義方式顯示蒙層
_showMask(bool flag, double index) {
var widthOffset = ScreenUtil.getInstance().setWidth(63);
var initWidth = ScreenUtil.getInstance().setWidth(28);
return flag == true
? Positioned(
bottom: 22,
left: initWidth * index + widthOffset,
child: Container(
color: Color.fromRGBO(0, 0, 0, 0.2),
width: ScreenUtil.getInstance().setWidth(90),
height: ScreenUtil.getInstance().setHeight(410),
),
)
: Container();
}
}
//一下爲組合柱狀圖數據部分
class OrdinalSales {
final String year;
final int sales;
OrdinalSales(this.year, this.sales);
}
class ChartFlutterBean {
static List<charts.Series<OrdinalSales, String>> createSampleData() {
final data = [
new OrdinalSales('吃', 0),
new OrdinalSales('喝', 1),
new OrdinalSales('玩', 0),
new OrdinalSales('樂', 11),
new OrdinalSales('學', 28),
];
return [
new charts.Series<OrdinalSales, String>(
id: 'Sales',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (OrdinalSales sales, _) => sales.year,
measureFn: (OrdinalSales sales, _) => sales.sales,
data: data,
)
];
}
}
複製代碼
我絕對不會像某站的UP主同樣,逢人就喊「一鍵三連」,直到我碰見你……,因此,start呢github
--END--dom