flutter饼图

在这里插入图片描述

使用插件快多了,
代码:

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;

void main()=>runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowMaterialGrid: false,
      home:piePage()
    );
  }
}

class piePage extends StatelessWidget {
  const piePage({Key key}) : super(key: key);


  Widget _Pie() {
    var random = Random();

    var data = [
    PieSales(0, random.nextInt(30)),
    PieSales(1, random.nextInt(20)),
    PieSales(2, random.nextInt(10)),
    ];

    var seriesList = [
      charts.Series<PieSales, int>(
        id: 'Sales',
        domainFn: (PieSales sales, _) => sales.year,
        measureFn: (PieSales sales, _) => sales.sales,
        data: data,
      )
    ];

    return charts.PieChart(seriesList, animate: true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("饼图")),
      body: Column(children: [
        Container(height: 300, child: _Pie()),
      ]),
    );
  }
}

class PieSales {
  final int year;
  final int sales;

  PieSales(this.year, this.sales);
}

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页