Skip to content

Commit

Permalink
optimize: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
pdliuw committed Apr 14, 2020
1 parent 40ba307 commit 419ad34
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
3 changes: 3 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class _MyHomePageState extends State<MyHomePage>
pathStrokeWidth: 10,
valueStrokeWidth: 10,
gapDegree: 60,
roundCap: true,
),
),
Text("${_segmentValue / 10 * 100}%"),
Expand Down Expand Up @@ -200,6 +201,7 @@ class _MyHomePageState extends State<MyHomePage>
valueStrokeWidth: 10.0,
useCenter: false,
filled: false,
roundCap: true,
),
),
Text("${_segmentValue / 10 * 100}%"),
Expand All @@ -224,6 +226,7 @@ class _MyHomePageState extends State<MyHomePage>
valueStrokeWidth: 10.0,
useCenter: true,
filled: true,
roundCap: true,
),
),
Text("${_segmentValue / 10 * 100}%"),
Expand Down
12 changes: 8 additions & 4 deletions lib/src/circular_state_progress_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AirCircularStateProgressIndicator extends StatefulWidget {
double _valueStrokeWidth;
bool _filled;
bool _useCenter;
bool _roundCap;

/// constructor
AirCircularStateProgressIndicator({
Expand All @@ -37,6 +38,7 @@ class AirCircularStateProgressIndicator extends StatefulWidget {
double valueStrokeWidth = 5,
bool filled = false,
bool useCenter = false,
bool roundCap = true,
}) {
assert(size != null);
assert(min >= LIMITED_MIN_VALUE);
Expand All @@ -49,6 +51,7 @@ class AirCircularStateProgressIndicator extends StatefulWidget {
assert(valueStrokeWidth != null);
assert(filled != null);
assert(useCenter != null);
assert(roundCap != null);
_size = size;
//value
_value = value;
Expand All @@ -64,6 +67,7 @@ class AirCircularStateProgressIndicator extends StatefulWidget {
//
_filled = filled;
_useCenter = useCenter;
_roundCap = roundCap;

//The strokeWidth is zero when _filled is true,
if (_filled) {
Expand Down Expand Up @@ -101,17 +105,17 @@ class _CircularStateProgressIndicatorState
return Paint()
..color = widget._pathColor
..strokeWidth = widget._pathStrokeWidth
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..strokeCap = widget._roundCap ? StrokeCap.round : StrokeCap.square
..strokeJoin = widget._roundCap ? StrokeJoin.round : StrokeJoin.bevel
..style = widget._filled ? PaintingStyle.fill : PaintingStyle.stroke;
}

Paint _getValuePaint() {
return Paint()
..color = widget._valueColor
..strokeWidth = widget._valueStrokeWidth
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.bevel
..strokeCap = widget._roundCap ? StrokeCap.round : StrokeCap.square
..strokeJoin = widget._roundCap ? StrokeJoin.round : StrokeJoin.bevel
..style = widget._filled ? PaintingStyle.fill : PaintingStyle.stroke;
}
}
Expand Down
27 changes: 20 additions & 7 deletions lib/src/linear_state_progress_indicator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:ai_progress/src/progress_mixin.dart';
import 'package:flutter/material.dart';

///
const double LINEAR_LIMITED_MIN_VALUE = 0;
const double LINEAR_LIMITED_MAX_VALUE = 100;
const double LINEAR_DEFAULT_VALUE = 10;

/// LinearStateProgressIndicator
///
// ignore: must_be_immutable
Expand All @@ -19,16 +24,27 @@ class AirLinearStateProgressIndicator extends StatefulWidget {
/// constructor
AirLinearStateProgressIndicator({
@required Size size,
double min = 0.0,
double max = 100.0,
double value = 10,
double min = LINEAR_LIMITED_MIN_VALUE,
double max = LINEAR_LIMITED_MAX_VALUE,
double value = LINEAR_DEFAULT_VALUE,
Color pathColor = Colors.white,
Color valueColor = Colors.green,
bool roundCap = true,
bool shouldRepaint = false,
double pathStrokeWidth,
double valueStrokeWidth,
}) {
assert(size != null);
assert(min >= LINEAR_LIMITED_MIN_VALUE);
assert(max <= LINEAR_LIMITED_MAX_VALUE);
assert(value >= min);
assert(value <= max);
assert(pathColor != null);
assert(valueColor != null);
assert(roundCap != null);
assert(shouldRepaint != null);
assert(pathStrokeWidth != null);
assert(valueStrokeWidth != null);
_size = size;
_min = min;
_max = max;
Expand Down Expand Up @@ -128,7 +144,6 @@ class LinearProgressPainter extends CustomPainter with ProgressMixin {
@override
drawProgressPath({Canvas canvas, Paint paint, Size size}) {
double heightCenter = size.height / 2;
double widthCenter = size.width / 2;
double width = size.width;

Offset startPoint = Offset(0, heightCenter);
Expand All @@ -140,8 +155,6 @@ class LinearProgressPainter extends CustomPainter with ProgressMixin {
@override
drawProgressValue({Canvas canvas, Paint paint, Size size}) {
double heightCenter = size.height / 2;
double widthCenter = size.width / 2;
double width = size.width;

Offset startPoint = Offset(0, heightCenter);
Offset endPoint = Offset(_getDrawWidthValue(size: size), heightCenter);
Expand All @@ -164,7 +177,7 @@ class LinearProgressPainter extends CustomPainter with ProgressMixin {
}

double _getDrawWidthValue({@required Size size}) {
double drawWidthValue = size.width * (_value / _max);
double drawWidthValue = size.width * ((_value - _min) / (_max - _min));

//range size
drawWidthValue = drawWidthValue < 0 ? 0 : drawWidthValue;
Expand Down
49 changes: 42 additions & 7 deletions lib/src/step_state_progress_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import 'package:flutter/material.dart';
/// AirStepStateProgressIndicator
// ignore: must_be_immutable
class AirStepStateProgressIndicator extends StatefulWidget {
/// Limited
static const int LIMITED_STEP_MIN_VALUE = 0;

/// Default values
static const int DEFAULT_STEP_COUNT = 10;
static const int DEFAULT_STEP_VALUE = 1;
static const double DEFAULT_STROKE_WIDTH = 5.0;
static const double DEFAULT_SPACE_WIDTH = 2.0;

final Size size;
final int stepCount;
final int stepValue;
Expand All @@ -15,18 +24,32 @@ class AirStepStateProgressIndicator extends StatefulWidget {
Color _pathColor;
Color _valueColor;

bool _roundCap;

/// constructor
AirStepStateProgressIndicator({
this.size,
this.stepCount,
this.stepValue,
double pathStrokeWidth = 5.0,
double valueStrokeWidth = 5.0,
double spaceWidth = 2.0,
this.stepCount = DEFAULT_STEP_COUNT,
this.stepValue = DEFAULT_STEP_VALUE,
double pathStrokeWidth = DEFAULT_STROKE_WIDTH,
double valueStrokeWidth = DEFAULT_STROKE_WIDTH,
double spaceWidth = DEFAULT_SPACE_WIDTH,
Color pathColor = Colors.grey,
Color valueColor = Colors.green,
bool roundCap = false,
}) {
if (stepCount <= 0) {}
assert(size != null);
assert(stepCount != null);
assert(stepCount > LIMITED_STEP_MIN_VALUE);
assert(stepValue != null);
assert(stepValue >= LIMITED_STEP_MIN_VALUE);
assert(stepValue <= stepCount);
assert(pathStrokeWidth != null);
assert(valueStrokeWidth != null);
assert(spaceWidth != null);
assert(pathColor != null);
assert(valueColor != null);
assert(roundCap != null);

//stroke width
_pathStrokeWidth = pathStrokeWidth;
Expand All @@ -38,6 +61,9 @@ class AirStepStateProgressIndicator extends StatefulWidget {
//color
_pathColor = pathColor;
_valueColor = valueColor;

//cap style
_roundCap = roundCap;
}
@override
State<StatefulWidget> createState() {
Expand All @@ -60,6 +86,7 @@ class _StepStateProgressIndicator extends State<AirStepStateProgressIndicator> {
spaceWidth: widget._spaceWidth,
pathColor: widget._pathColor,
valueColor: widget._valueColor,
roundCap: widget._roundCap,
),
);
}
Expand All @@ -79,6 +106,8 @@ class StepProgressPainter extends CustomPainter with ProgressMixin {
Color _valueColor;
Color _pathColor;

bool _roundCap;

/// constructor
StepProgressPainter._({
@required bool shouldRepaint,
Expand All @@ -89,6 +118,7 @@ class StepProgressPainter extends CustomPainter with ProgressMixin {
double spaceWidth,
Color pathColor,
Color valueColor,
bool roundCap,
}) {
//should repaint
_shouldRepaint = shouldRepaint;
Expand All @@ -102,6 +132,7 @@ class StepProgressPainter extends CustomPainter with ProgressMixin {
//stroke
_pathStrokeWidth = pathStrokeWidth;
_valueStrokeWidth = valueStrokeWidth;
_roundCap = roundCap;

//
_spaceWidth = spaceWidth;
Expand Down Expand Up @@ -155,15 +186,19 @@ class StepProgressPainter extends CustomPainter with ProgressMixin {

Paint _getDrawPathPaint() {
return Paint()
..color = Colors.grey
..color = pathColor
..style = PaintingStyle.stroke
..strokeCap = _roundCap ? StrokeCap.round : StrokeCap.butt
..strokeJoin = _roundCap ? StrokeJoin.round : StrokeJoin.miter
..strokeWidth = pathStrokeWidth;
}

Paint _getDrawValuePaint() {
return Paint()
..color = valueColor
..style = PaintingStyle.stroke
..strokeCap = _roundCap ? StrokeCap.round : StrokeCap.butt
..strokeJoin = _roundCap ? StrokeJoin.round : StrokeJoin.miter
..strokeWidth = valueStrokeWidth;
}

Expand Down

0 comments on commit 419ad34

Please sign in to comment.