Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate charts to null safety #80

Merged
merged 20 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @dart=2.9

import 'dart:math';

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -29,25 +27,25 @@ class BrnDoughnutDataItem {
double radius = 0;

BrnDoughnutDataItem({
this.value,
this.title,
required this.value,
required this.title,
this.color = Colors.blueAccent,
});
}

/// 选中扇形区域后执行的回调
typedef BrnDoughnutSelectCallback = void Function(
BrnDoughnutDataItem selectedItem);
BrnDoughnutDataItem? selectedItem);

class BrnDoughnut extends CustomPainter {
///圆心位置
Offset circleCenter;
late Offset circleCenter;

/// 选中的区域
final BrnDoughnutDataItem selectedItem;
final BrnDoughnutDataItem? selectedItem;

/// 选中区域回调
final BrnDoughnutSelectCallback brnDoughnutSelectCallback;
final BrnDoughnutSelectCallback? brnDoughnutSelectCallback;

/// 字体大小
final double fontSize;
Expand Down Expand Up @@ -75,18 +73,18 @@ class BrnDoughnut extends CustomPainter {

BrnDoughnut(
{this.ringWidth = 50,
this.data,
required this.data,
this.fontSize = 12,
this.fontColor = Colors.white,
this.selectedItem,
this.showTitleWhenSelected = false,
this.brnDoughnutSelectCallback}) {
double lastEndRadius = 0;
double totalValue = 0;
this.data?.forEach((BrnDoughnutDataItem item) {
this.data.forEach((BrnDoughnutDataItem item) {
totalValue += item.value;
});
this.data?.forEach((BrnDoughnutDataItem item) {
this.data.forEach((BrnDoughnutDataItem item) {
item.percentage = item.value / totalValue;
item.startRadius = lastEndRadius;
item.radius = 2 * pi * item.percentage;
Expand All @@ -110,7 +108,7 @@ class BrnDoughnut extends CustomPainter {
Offset center = drawArea.center;
circleCenter = center;

this.data?.forEach((BrnDoughnutDataItem item) {
this.data.forEach((BrnDoughnutDataItem item) {
// 画扇形
Paint _paint = Paint()
..color = item.color
Expand All @@ -126,9 +124,8 @@ class BrnDoughnut extends CustomPainter {
canvas.drawArc(rect, item.startRadius, item.radius, true, _paint);

// 画文本
if (item.title != null &&
(this.showTitleWhenSelected == false ||
item.startRadius == selectedItem?.startRadius)) {
if (this.showTitleWhenSelected == false ||
item.startRadius == selectedItem?.startRadius) {
// 画引线
Offset indicarorLPoint =
calcOffsetWith(item.middleRadius, indicatorLCircleRadius);
Expand Down Expand Up @@ -230,15 +227,15 @@ class BrnDoughnut extends CustomPainter {
}

@override
bool hitTest(Offset position) {
bool? hitTest(Offset position) {
int length = data.length;
for (int i = 0; i < length; i++) {
BrnDoughnutDataItem item = data[i];
double radain = pointRadianInSector(position);
if (item.startRadius < radain &&
radain < (item.startRadius + item.radius)) {
if (null != brnDoughnutSelectCallback)
brnDoughnutSelectCallback(
brnDoughnutSelectCallback!(
item.startRadius == selectedItem?.startRadius ? null : item);
break;
}
Expand Down Expand Up @@ -270,10 +267,10 @@ class BrnDoughnutChart extends StatelessWidget {
final double height;

/// 选中的项目
final BrnDoughnutDataItem selectedItem;
final BrnDoughnutDataItem? selectedItem;

/// 选中项目时候的回掉
final BrnDoughnutSelectCallback selectCallback;
final BrnDoughnutSelectCallback? selectCallback;

/// 选中时展示文字大小,默认12
final double fontSize;
Expand All @@ -298,7 +295,7 @@ class BrnDoughnutChart extends StatelessWidget {
this.height = 0,
this.padding = EdgeInsets.zero,
this.ringWidth = 50,
this.data,
required this.data,
this.fontSize = 12,
this.fontColor = Colors.white,
this.selectedItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @dart=2.9

import 'package:bruno/src/components/charts/brn_doughunt_chart/brn_doughnut_chart.dart';
import 'package:flutter/material.dart';

Expand All @@ -26,21 +24,22 @@ class DoughnutChartLegend extends StatelessWidget {
final List<BrnDoughnutDataItem> data;

DoughnutChartLegend(
{this.legendStyle = BrnDoughnutChartLegendStyle.wrap, this.data});
{this.legendStyle = BrnDoughnutChartLegendStyle.wrap,
required this.data});

@override
Widget build(BuildContext context) {
if (BrnDoughnutChartLegendStyle.list == this.legendStyle) {
List<Widget> items = List();
this.data?.forEach((BrnDoughnutDataItem item) {
List<Widget> items = [];
this.data.forEach((BrnDoughnutDataItem item) {
items.add(this.genItem(item));
});
return Column(
children: items,
);
} else if (BrnDoughnutChartLegendStyle.wrap == this.legendStyle) {
List<Widget> items = List();
this.data?.forEach((BrnDoughnutDataItem item) {
List<Widget> items = [];
this.data.forEach((BrnDoughnutDataItem item) {
items.add(this.genItem(item));
});

Expand All @@ -50,7 +49,7 @@ class DoughnutChartLegend extends StatelessWidget {
children: items,
);
} else {
return Container();
return const SizedBox.shrink();
}
}

Expand All @@ -70,7 +69,7 @@ class DoughnutChartLegend extends StatelessWidget {
width: 6,
),
Text(
item.title ?? '',
item.title,
style: TextStyle(color: Colors.black),
),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @dart=2.9

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

Expand All @@ -9,31 +7,31 @@ class BrnBarChartScaleStyle {

/// y轴获取的值,只读
double get titleValue {
if (title == null || title.isEmpty) {
if (title.isEmpty) {
return 0;
} else {
return double.parse(title);
}
}

/// 刻度标志样式
TextStyle titleStyle;
TextStyle? titleStyle;

/// 与最大数值的比率,用来计算绘制刻度的位置使用。
double positionRetioy;
double? positionRetioy;

/// 下面标注文案独属y轴使用,目前还没有x轴扩展需求,x轴设置下面参数无效,后期有需要再扩展
/// 两个刻度之间的标注文案(向前绘制即x轴在该刻度左侧绘制,y轴在该刻度下面绘制),不需要的话不设置
String centerSubTitle;
String? centerSubTitle;

/// 标注文案样式,centerSubTitle有内容时有效
TextStyle centerSubTextStyle;
TextStyle? centerSubTextStyle;

/// 标注文案位置是否是在左侧,false表示在右侧,centerSubTitle有内容时有效
bool isLeft;

BrnBarChartScaleStyle(
{this.title,
{required this.title,
this.titleStyle,
this.centerSubTitle,
this.centerSubTextStyle,
Expand All @@ -60,10 +58,10 @@ class BrnBarDataBean {
/// 每条线的定义
class BrnBarBean {
/// 名称
String name;
String? name;

///x轴的字体样式
TextStyle xTitleStyle;
TextStyle? xTitleStyle;

///是否显示x轴的文字,用来处理多个线条绘制的时候,同一x轴坐标不需要绘制多次,则只需要将多条线中一个标记绘制即可
bool isDrawX;
Expand All @@ -78,19 +76,19 @@ class BrnBarBean {
bool isCurve;

///点集合
List<BrnBarDataBean> points;
List<BrnBarDataBean>? points;

///曲线或折线的颜色
Color lineColor;

///填充色
List<Color> colors;
List<Color>? colors;

///占位图是否需要打断线条绘制,如果打断的话这个点的y值将没有意义,只有x轴有效,如果不打断的话,y轴值有效
bool placehoderImageBreak;

///用户当前进行位置的小图标(比如一个小锁),默认没有只显示y轴的值,如果有内容则显示这个小图标,
ui.Image placehoderImage;
ui.Image? placehoderImage;
BrnBarBean(
{this.name,
this.xTitleStyle,
Expand Down
Loading