Skip to content

Commit

Permalink
Remove path_drawing dependency, we now use the dashPath() code directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
imaN Khoshabi committed Mar 22, 2021
1 parent 962b1e5 commit fc64cde
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/src/extensions/path_extension.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:ui';

import 'package:path_drawing/path_drawing.dart';
import 'package:fl_chart/src/utils/path_drawing/dash_path.dart';

/// Defines extensions on the [Path]
extension DashedPath on Path {
Expand Down
89 changes: 89 additions & 0 deletions lib/src/utils/path_drawing/dash_path.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'dart:ui';

/// Came from [flutter_path_drawing](https://github.com/dnfield/flutter_path_drawing) library.
/// Creates a new path that is drawn from the segments of `source`.
///
/// Dash intervals are controled by the `dashArray` - see [CircularIntervalList]
/// for examples.
///
/// `dashOffset` specifies an initial starting point for the dashing.
///
/// Passing a `source` that is an empty path will return an empty path.
Path dashPath(
Path source, {
required CircularIntervalList<double> dashArray,
DashOffset? dashOffset,
}) {
assert(dashArray != null); // ignore: unnecessary_null_comparison

dashOffset = dashOffset ?? const DashOffset.absolute(0.0);
// TODO: Is there some way to determine how much of a path would be visible today?

final dest = Path();
for (final metric in source.computeMetrics()) {
var distance = dashOffset._calculate(metric.length);
var draw = true;
while (distance < metric.length) {
final len = dashArray.next;
if (draw) {
dest.addPath(metric.extractPath(distance, distance + len), Offset.zero);
}
distance += len;
draw = !draw;
}
}

return dest;
}

enum _DashOffsetType { Absolute, Percentage }

/// Specifies the starting position of a dash array on a path, either as a
/// percentage or absolute value.
///
/// The internal value will be guaranteed to not be null.
class DashOffset {
/// Create a DashOffset that will be measured as a percentage of the length
/// of the segment being dashed.
///
/// `percentage` will be clamped between 0.0 and 1.0.
DashOffset.percentage(double percentage)
: _rawVal = percentage.clamp(0.0, 1.0),
_dashOffsetType = _DashOffsetType.Percentage;

/// Create a DashOffset that will be measured in terms of absolute pixels
/// along the length of a [Path] segment.
const DashOffset.absolute(double start)
: _rawVal = start,
_dashOffsetType = _DashOffsetType.Absolute;

final double _rawVal;
final _DashOffsetType _dashOffsetType;

double _calculate(double length) {
return _dashOffsetType == _DashOffsetType.Absolute ? _rawVal : length * _rawVal;
}
}

/// A circular array of dash offsets and lengths.
///
/// For example, the array `[5, 10]` would result in dashes 5 pixels long
/// followed by blank spaces 10 pixels long. The array `[5, 10, 5]` would
/// result in a 5 pixel dash, a 10 pixel gap, a 5 pixel dash, a 5 pixel gap,
/// a 10 pixel dash, etc.
///
/// Note that this does not quite conform to an [Iterable<T>], because it does
/// not have a moveNext.
class CircularIntervalList<T> {
CircularIntervalList(this._values);

final List<T> _values;
int _idx = 0;

T get next {
if (_idx >= _values.length) {
_idx = 0;
}
return _values[_idx++];
}
}
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ dependencies:
flutter:
sdk: flutter

path_drawing: ^0.5.0-nullsafety.0
equatable: ^2.0.0-nullsafety.3
equatable: ^2.0.0
pedantic: ^1.10.0

dev_dependencies:
Expand Down

0 comments on commit fc64cde

Please sign in to comment.