Flutter学习第十一天:使用Fl_chart依赖实现饼图,功能齐全符合我的要求?

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纵排列
ExpandedExpanded使用与类似与Column,Row,Flex等展示多个组件集合的组件,Expanded包含的组件可以占据剩余的空间
AspectRatioAspectRatio作用于父控件,根据aspectRatio计算父控件的宽或者高,AspectRatio的子控件将填充满父控件,子控件的宽高无效
Indicator主要实现饼图中的各部分的颜色注释作用
PieChart实现饼图,还是比较容易理解其功能的作用
SizeBox用来设置两个widget之间的间距
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个使用fl_chart库创建饼图的简单示例: ```dart import 'package:flutter/material.dart'; import 'package:fl_chart/fl_chart.dart'; class PieChartSample extends StatefulWidget { @override _PieChartSampleState createState() => _PieChartSampleState(); } class _PieChartSampleState extends State<PieChartSample> { int touchedIndex; @override Widget build(BuildContext context) { return 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: getSections(), ), ), ); } List<PieChartSectionData> getSections() { 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: Colors.red, value: 40, title: '40%', radius: radius, titleStyle: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff), ), ); case 1: return PieChartSectionData( color: Colors.green, value: 30, title: '30%', radius: radius, titleStyle: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff), ), ); case 2: return PieChartSectionData( color: Colors.blue, value: 15, title: '15%', radius: radius, titleStyle: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff), ), ); case 3: return PieChartSectionData( color: Colors.yellow, value: 15, title: '15%', radius: radius, titleStyle: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff), ), ); default: return null; } }); } } ``` 在这个例子中,我们创建了一个PieChartSample类,它继承自StatefulWidget。在build()方法中,我们创建了一个AspectRatio小部件,它将PieChart小部件的宽高比设置为1:1。然后,我们创建了一个PieChart小部件,它接收一个PieChartData对象作为参数。PieChartData对象包含所有的饼图数据,包括饼图的颜色、半径、标题、值等。 在PieChartData对象中,我们使用了PieTouchData对象来处理用户的触摸事件。我们还使用FlBorderData对象来隐藏边框,使用sectionsSpace属性来设置饼图部分之间的间距,使用centerSpaceRadius属性来设置中心空间的半径。 在getSections()函数中,我们生成了饼图的各个部分。每个部分都由一个PieChartSectionData对象表示,它包括颜色、半径、值、标题和标题样式等属性。我们返回一个包含所有部分的列表,然后将它们传递给PieChartData对象。 最后,我们在PieTouchData对象的touchCallback属性中处理触摸事件。如果用户停止触摸饼图,我们将touchedIndex设置为-1,否则,我们将touchedIndex设置为当前触摸部分的索引。根据touchedIndex的值,我们可以设置饼图部分的半径和标题样式,以突出显示当前触摸的部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值