Commit 81b76c2b authored by Adam Barth's avatar Adam Barth Committed by Ian Hickson

Make CircularProgressIndicator's stroke width configurable (#9794)

Fixes #9062
parent ab8202dc
...@@ -11,7 +11,6 @@ import 'theme.dart'; ...@@ -11,7 +11,6 @@ import 'theme.dart';
const double _kLinearProgressIndicatorHeight = 6.0; const double _kLinearProgressIndicatorHeight = 6.0;
const double _kMinCircularProgressIndicatorSize = 36.0; const double _kMinCircularProgressIndicatorSize = 36.0;
const double _kCircularProgressIndicatorStrokeWidth = 4.0;
// TODO(hansmuller): implement the support for buffer indicator // TODO(hansmuller): implement the support for buffer indicator
...@@ -34,7 +33,7 @@ abstract class ProgressIndicator extends StatefulWidget { ...@@ -34,7 +33,7 @@ abstract class ProgressIndicator extends StatefulWidget {
Key key, Key key,
this.value, this.value,
this.backgroundColor, this.backgroundColor,
this.valueColor this.valueColor,
}) : super(key: key); }) : super(key: key);
/// If non-null, the value of this progress indicator with 0.0 corresponding /// If non-null, the value of this progress indicator with 0.0 corresponding
...@@ -76,7 +75,7 @@ class _LinearProgressIndicatorPainter extends CustomPainter { ...@@ -76,7 +75,7 @@ class _LinearProgressIndicatorPainter extends CustomPainter {
this.backgroundColor, this.backgroundColor,
this.valueColor, this.valueColor,
this.value, this.value,
this.animationValue this.animationValue,
}); });
final Color backgroundColor; final Color backgroundColor;
...@@ -139,7 +138,7 @@ class LinearProgressIndicator extends ProgressIndicator { ...@@ -139,7 +138,7 @@ class LinearProgressIndicator extends ProgressIndicator {
/// indicator). See [value] for details. /// indicator). See [value] for details.
const LinearProgressIndicator({ const LinearProgressIndicator({
Key key, Key key,
double value double value,
}) : super(key: key, value: value); }) : super(key: key, value: value);
@override @override
...@@ -170,16 +169,16 @@ class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with ...@@ -170,16 +169,16 @@ class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with
return new Container( return new Container(
constraints: const BoxConstraints.tightFor( constraints: const BoxConstraints.tightFor(
width: double.INFINITY, width: double.INFINITY,
height: _kLinearProgressIndicatorHeight height: _kLinearProgressIndicatorHeight,
), ),
child: new CustomPaint( child: new CustomPaint(
painter: new _LinearProgressIndicatorPainter( painter: new _LinearProgressIndicatorPainter(
backgroundColor: widget._getBackgroundColor(context), backgroundColor: widget._getBackgroundColor(context),
valueColor: widget._getValueColor(context), valueColor: widget._getValueColor(context),
value: widget.value, // may be null value: widget.value, // may be null
animationValue: animationValue // ignored if widget.value is not null animationValue: animationValue, // ignored if widget.value is not null
) ),
) ),
); );
} }
...@@ -192,7 +191,7 @@ class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with ...@@ -192,7 +191,7 @@ class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with
animation: _animation, animation: _animation,
builder: (BuildContext context, Widget child) { builder: (BuildContext context, Widget child) {
return _buildIndicator(context, _animation.value); return _buildIndicator(context, _animation.value);
} },
); );
} }
} }
...@@ -211,7 +210,7 @@ class _CircularProgressIndicatorPainter extends CustomPainter { ...@@ -211,7 +210,7 @@ class _CircularProgressIndicatorPainter extends CustomPainter {
this.tailValue, this.tailValue,
this.stepValue, this.stepValue,
this.rotationValue, this.rotationValue,
this.strokeWidth this.strokeWidth,
}) : arcStart = value != null }) : arcStart = value != null
? _kStartAngle ? _kStartAngle
: _kStartAngle + tailValue * 3 / 2 * math.PI + rotationValue * math.PI * 1.7 - stepValue * 0.8 * math.PI, : _kStartAngle + tailValue * 3 / 2 * math.PI + rotationValue * math.PI * 1.7 - stepValue * 0.8 * math.PI,
...@@ -282,24 +281,28 @@ class CircularProgressIndicator extends ProgressIndicator { ...@@ -282,24 +281,28 @@ class CircularProgressIndicator extends ProgressIndicator {
Key key, Key key,
double value, double value,
Color backgroundColor, Color backgroundColor,
Animation<Color> valueColor Animation<Color> valueColor,
this.strokeWidth: 4.0,
}) : super(key: key, value: value, backgroundColor: backgroundColor, valueColor: valueColor); }) : super(key: key, value: value, backgroundColor: backgroundColor, valueColor: valueColor);
/// The width of the line used to draw the circle.
final double strokeWidth;
@override @override
_CircularProgressIndicatorState createState() => new _CircularProgressIndicatorState(); _CircularProgressIndicatorState createState() => new _CircularProgressIndicatorState();
} }
// Tweens used by circular progress indicator // Tweens used by circular progress indicator
final Animatable<double> _kStrokeHeadTween = new CurveTween( final Animatable<double> _kStrokeHeadTween = new CurveTween(
curve: const Interval(0.0, 0.5, curve: Curves.fastOutSlowIn) curve: const Interval(0.0, 0.5, curve: Curves.fastOutSlowIn),
).chain(new CurveTween( ).chain(new CurveTween(
curve: const SawTooth(5) curve: const SawTooth(5),
)); ));
final Animatable<double> _kStrokeTailTween = new CurveTween( final Animatable<double> _kStrokeTailTween = new CurveTween(
curve: const Interval(0.5, 1.0, curve: Curves.fastOutSlowIn) curve: const Interval(0.5, 1.0, curve: Curves.fastOutSlowIn),
).chain(new CurveTween( ).chain(new CurveTween(
curve: const SawTooth(5) curve: const SawTooth(5),
)); ));
final Animatable<int> _kStepTween = new StepTween(begin: 0, end: 5); final Animatable<int> _kStepTween = new StepTween(begin: 0, end: 5);
...@@ -338,9 +341,9 @@ class _CircularProgressIndicatorState extends State<CircularProgressIndicator> w ...@@ -338,9 +341,9 @@ class _CircularProgressIndicatorState extends State<CircularProgressIndicator> w
tailValue: tailValue, tailValue: tailValue,
stepValue: stepValue, stepValue: stepValue,
rotationValue: rotationValue, rotationValue: rotationValue,
strokeWidth: _kCircularProgressIndicatorStrokeWidth strokeWidth: widget.strokeWidth,
) ),
) ),
); );
} }
...@@ -353,9 +356,9 @@ class _CircularProgressIndicatorState extends State<CircularProgressIndicator> w ...@@ -353,9 +356,9 @@ class _CircularProgressIndicatorState extends State<CircularProgressIndicator> w
_kStrokeHeadTween.evaluate(_controller), _kStrokeHeadTween.evaluate(_controller),
_kStrokeTailTween.evaluate(_controller), _kStrokeTailTween.evaluate(_controller),
_kStepTween.evaluate(_controller), _kStepTween.evaluate(_controller),
_kRotationTween.evaluate(_controller) _kRotationTween.evaluate(_controller),
); );
} },
); );
} }
...@@ -376,7 +379,7 @@ class _RefreshProgressIndicatorPainter extends _CircularProgressIndicatorPainter ...@@ -376,7 +379,7 @@ class _RefreshProgressIndicatorPainter extends _CircularProgressIndicatorPainter
int stepValue, int stepValue,
double rotationValue, double rotationValue,
double strokeWidth, double strokeWidth,
this.arrowheadScale this.arrowheadScale,
}) : super( }) : super(
valueColor: valueColor, valueColor: valueColor,
value: value, value: value,
...@@ -384,7 +387,7 @@ class _RefreshProgressIndicatorPainter extends _CircularProgressIndicatorPainter ...@@ -384,7 +387,7 @@ class _RefreshProgressIndicatorPainter extends _CircularProgressIndicatorPainter
tailValue: tailValue, tailValue: tailValue,
stepValue: stepValue, stepValue: stepValue,
rotationValue: rotationValue, rotationValue: rotationValue,
strokeWidth: strokeWidth strokeWidth: strokeWidth,
); );
final double arrowheadScale; final double arrowheadScale;
...@@ -442,12 +445,14 @@ class RefreshProgressIndicator extends CircularProgressIndicator { ...@@ -442,12 +445,14 @@ class RefreshProgressIndicator extends CircularProgressIndicator {
Key key, Key key,
double value, double value,
Color backgroundColor, Color backgroundColor,
Animation<Color> valueColor Animation<Color> valueColor,
double strokeWidth: 2.0, // Different default than CircularProgressIndicator.
}) : super( }) : super(
key: key, key: key,
value: value, value: value,
backgroundColor: backgroundColor, backgroundColor: backgroundColor,
valueColor: valueColor valueColor: valueColor,
strokeWidth: strokeWidth,
); );
@override @override
...@@ -491,12 +496,12 @@ class _RefreshProgressIndicatorState extends _CircularProgressIndicatorState { ...@@ -491,12 +496,12 @@ class _RefreshProgressIndicatorState extends _CircularProgressIndicatorState {
tailValue: tailValue, tailValue: tailValue,
stepValue: stepValue, stepValue: stepValue,
rotationValue: rotationValue, rotationValue: rotationValue,
strokeWidth: 2.0, strokeWidth: widget.strokeWidth,
arrowheadScale: arrowheadScale arrowheadScale: arrowheadScale,
) ),
) ),
) ),
) ),
); );
} }
} }
...@@ -6,6 +6,8 @@ import 'package:flutter_test/flutter_test.dart'; ...@@ -6,6 +6,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../rendering/mock_canvas.dart';
void main() { void main() {
// The "can be constructed" tests that follow are primarily to ensure that any // The "can be constructed" tests that follow are primarily to ensure that any
...@@ -56,4 +58,15 @@ void main() { ...@@ -56,4 +58,15 @@ void main() {
final List<Layer> layers2 = tester.layers; final List<Layer> layers2 = tester.layers;
expect(layers1, isNot(equals(layers2))); expect(layers1, isNot(equals(layers2)));
}); });
testWidgets('CircularProgressIndicator stoke width', (WidgetTester tester) async {
await tester.pumpWidget(const CircularProgressIndicator());
expect(find.byType(CircularProgressIndicator), paints..arc(strokeWidth: 4.0));
await tester.pumpWidget(const CircularProgressIndicator(strokeWidth: 16.0));
expect(find.byType(CircularProgressIndicator), paints..arc(strokeWidth: 16.0));
});
} }
...@@ -198,6 +198,18 @@ abstract class PaintPattern { ...@@ -198,6 +198,18 @@ abstract class PaintPattern {
/// [Canvas.drawLine] call are ignored. /// [Canvas.drawLine] call are ignored.
void line({ Color color, double strokeWidth, bool hasMaskFilter, PaintingStyle style }); void line({ Color color, double strokeWidth, bool hasMaskFilter, PaintingStyle style });
/// Indicates that an arc is expected next.
///
/// The next arc is examined. Any arguments that are passed to this method
/// are compared to the actual [Canvas.drawArc] call's `paint` argument, and
/// any mismatches result in failure.
///
/// If no call to [Canvas.drawArc] was made, then this results in failure.
///
/// Any calls made between the last matched call (if any) and the
/// [Canvas.drawArc] call are ignored.
void arc({ Color color, double strokeWidth, bool hasMaskFilter, PaintingStyle style });
/// Indicates that a paragraph is expected next. /// Indicates that a paragraph is expected next.
/// ///
/// Calls are skipped until a call to [Canvas.drawParagraph] is found. Any /// Calls are skipped until a call to [Canvas.drawParagraph] is found. Any
...@@ -299,6 +311,11 @@ class _TestRecordingCanvasPatternMatcher extends Matcher implements PaintPattern ...@@ -299,6 +311,11 @@ class _TestRecordingCanvasPatternMatcher extends Matcher implements PaintPattern
_predicates.add(new _LinePaintPredicate(color: color, strokeWidth: strokeWidth, hasMaskFilter: hasMaskFilter, style: style)); _predicates.add(new _LinePaintPredicate(color: color, strokeWidth: strokeWidth, hasMaskFilter: hasMaskFilter, style: style));
} }
@override
void arc({ Color color, double strokeWidth, bool hasMaskFilter, PaintingStyle style }) {
_predicates.add(new _ArcPaintPredicate(color: color, strokeWidth: strokeWidth, hasMaskFilter: hasMaskFilter, style: style));
}
@override @override
void paragraph({ ui.Paragraph paragraph, Offset offset }) { void paragraph({ ui.Paragraph paragraph, Offset offset }) {
_predicates.add(new _FunctionPaintPredicate(#drawParagraph, <dynamic>[paragraph, offset])); _predicates.add(new _FunctionPaintPredicate(#drawParagraph, <dynamic>[paragraph, offset]));
...@@ -602,6 +619,12 @@ class _LinePaintPredicate extends _DrawCommandPaintPredicate { ...@@ -602,6 +619,12 @@ class _LinePaintPredicate extends _DrawCommandPaintPredicate {
); );
} }
class _ArcPaintPredicate extends _DrawCommandPaintPredicate {
_ArcPaintPredicate({ Color color, double strokeWidth, bool hasMaskFilter, PaintingStyle style }) : super(
#drawArc, 'an arc', 5, 4, color: color, strokeWidth: strokeWidth, hasMaskFilter: hasMaskFilter, style: style
);
}
class _SomethingPaintPredicate extends _PaintPredicate { class _SomethingPaintPredicate extends _PaintPredicate {
_SomethingPaintPredicate(this.predicate); _SomethingPaintPredicate(this.predicate);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment