Flutter實作漂亮的餅圖
- 1.匯入依賴
- 2.餅圖
- 1.效果圖
- 2.所有代碼
- 3.代碼決議
- 1.代碼結構
- 3.組件作用
1.匯入依賴
fl_chart: ^0.12.2
如果你想進一步了解這個插件的話,可以去看一下它的原始碼,框架地址
關于如何匯入依賴步驟如圖所示:
pubspec.yaml檔案->在dependencies下面添加依賴->點擊pub get

2.餅圖
1.效果圖
廢話少說,先看一下效果

2.所有代碼
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'indicator.dart';
void main(){
runApp(MaterialApp(
home: PieChartSample2(),
));
}
class PieChartSample2 extends StatefulWidget {
@override
State<StatefulWidget> createState() => PieChart2State();
}
class PieChart2State extends State {
int touchedIndex;
@override
Widget build(BuildContext context) {
return Container(
height: 100,
child:Card(
elevation: 5,//陰影
shape: const RoundedRectangleBorder(//形狀
//修改圓角
borderRadius: BorderRadius.all(Radius.circular(10)),
),
color: Colors.white,
child: Row(
children: <Widget>[
const SizedBox(
height: 18,
),
Expanded(
child: AspectRatio(
aspectRatio: 1,
child: PieChart(
PieChartData(
pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) {
setState(() {
if (pieTouchResponse.touchInput is FlLongPressEnd ||
pieTouchResponse.touchInput is FlPanEnd) {
touchedIndex = -1;
} else {
touchedIndex = pieTouchResponse.touchedSectionIndex;
}
});
}),
borderData: FlBorderData(
show: false,
),
sectionsSpace: 0,
centerSpaceRadius: 40,
sections: showingSections()),
),
),
),
Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children:<Widget>[
Indicator(
color: Color(0xff0293ee),
text: 'First',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: Color(0xfff8b250),
text: 'Second',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: Color(0xff845bef),
text: 'Third',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: Color(0xff13d38e),
text: 'Fourth',
isSquare: true,
),
SizedBox(
height: 18,
),
],
),
const SizedBox(
width: 28,
),
],
),
),
);
}
List<PieChartSectionData> showingSections() {
return List.generate(4, (i) {
final isTouched = i == touchedIndex;
final double fontSize = isTouched ? 25 : 16;
final double radius = isTouched ? 60 : 50;
switch (i) {
case 0:
return PieChartSectionData(
color: const Color(0xff0293ee),
value: 40,
title: '40%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)),
);
case 1:
return PieChartSectionData(
color: const Color(0xfff8b250),
value: 30,
title: '30%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)),
);
case 2:
return PieChartSectionData(
color: const Color(0xff845bef),
value: 15,
title: '15%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)),
);
case 3:
return PieChartSectionData(
color: const Color(0xff13d38e),
value: 15,
title: '15%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)),
);
default:
return null;
}
});
}
}
3.代碼決議
1.代碼結構
dart的語法有點像俄羅斯套娃

3.組件作用
| 組件 | 功能 |
|---|---|
| Card | 類似于卡片式的布局 |
| Row | 橫排列,不過一般只能排列一行,Wrap多行 |
| Column | 縱排列 |
| Expanded | Expanded使用與類似與Column,Row,Flex等展示多個組件集合的組件,Expanded包含的組件可以占據剩余的空間 |
| AspectRatio | AspectRatio作用于父控制元件,根據aspectRatio計算父控制元件的寬或者高,AspectRatio的子控制元件將填充滿父控制元件,子控制元件的寬高無效 |
| Indicator | 主要實作餅圖中的各部分的顏色注釋作用 |
| PieChart | 實作餅圖,還是比較容易理解其功能的作用 |
| SizeBox | 用來設定兩個widget之間的間距 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/259536.html
標籤:區塊鏈
上一篇:微信支付寶簽名邏輯鑒賞
下一篇:golang多語言支持
