Commit 23981f59 authored by Luke's avatar Luke Committed by Ian Hickson

Add border radius to Ink widgets (#8693)

* adds border radius to ink widgets. sets default ink border radius for material buttons with no background colors

* tidying up code

* add ink test stub

* remove unused import
parent cea7c66d
...@@ -289,12 +289,14 @@ class _MaterialButtonState extends State<MaterialButton> { ...@@ -289,12 +289,14 @@ class _MaterialButtonState extends State<MaterialButton> {
final ButtonTheme buttonTheme = ButtonTheme.of(context); final ButtonTheme buttonTheme = ButtonTheme.of(context);
final double height = config.height ?? buttonTheme.height; final double height = config.height ?? buttonTheme.height;
final int elevation = (_highlight ? config.highlightElevation : config.elevation) ?? 0; final int elevation = (_highlight ? config.highlightElevation : config.elevation) ?? 0;
final bool hasColorOrElevation = (config.color != null || elevation > 0);
Widget contents = new IconTheme.merge( Widget contents = new IconTheme.merge(
context: context, context: context,
data: new IconThemeData( data: new IconThemeData(
color: textColor color: textColor
), ),
child: new InkWell( child: new InkWell(
borderRadius: hasColorOrElevation ? null : kMaterialEdges[MaterialType.button],
highlightColor: config.highlightColor ?? theme.highlightColor, highlightColor: config.highlightColor ?? theme.highlightColor,
splashColor: config.splashColor ?? theme.splashColor, splashColor: config.splashColor ?? theme.splashColor,
onTap: config.onPressed, onTap: config.onPressed,
...@@ -308,7 +310,7 @@ class _MaterialButtonState extends State<MaterialButton> { ...@@ -308,7 +310,7 @@ class _MaterialButtonState extends State<MaterialButton> {
) )
) )
); );
if (elevation > 0 || config.color != null) { if (hasColorOrElevation) {
contents = new Material( contents = new Material(
type: MaterialType.button, type: MaterialType.button,
color: config.color, color: config.color,
......
...@@ -40,10 +40,12 @@ class InkHighlight extends InkFeature { ...@@ -40,10 +40,12 @@ class InkHighlight extends InkFeature {
@required RenderBox referenceBox, @required RenderBox referenceBox,
@required Color color, @required Color color,
BoxShape shape: BoxShape.rectangle, BoxShape shape: BoxShape.rectangle,
BorderRadius borderRadius,
RectCallback rectCallback, RectCallback rectCallback,
VoidCallback onRemoved, VoidCallback onRemoved,
}) : _color = color, }) : _color = color,
_shape = shape, _shape = shape,
_borderRadius = borderRadius ?? BorderRadius.zero,
_rectCallback = rectCallback, _rectCallback = rectCallback,
super(controller: controller, referenceBox: referenceBox, onRemoved: onRemoved) { super(controller: controller, referenceBox: referenceBox, onRemoved: onRemoved) {
assert(color != null); assert(color != null);
...@@ -61,6 +63,7 @@ class InkHighlight extends InkFeature { ...@@ -61,6 +63,7 @@ class InkHighlight extends InkFeature {
} }
final BoxShape _shape; final BoxShape _shape;
final BorderRadius _borderRadius;
final RectCallback _rectCallback; final RectCallback _rectCallback;
Animation<int> _alpha; Animation<int> _alpha;
...@@ -110,7 +113,16 @@ class InkHighlight extends InkFeature { ...@@ -110,7 +113,16 @@ class InkHighlight extends InkFeature {
canvas.drawCircle(rect.center, Material.defaultSplashRadius, paint); canvas.drawCircle(rect.center, Material.defaultSplashRadius, paint);
break; break;
case BoxShape.rectangle: case BoxShape.rectangle:
canvas.drawRect(rect, paint); if (_borderRadius != BorderRadius.zero) {
final RRect clipRRect = new RRect.fromRectAndCorners(
rect,
topLeft: _borderRadius.topLeft, topRight: _borderRadius.topRight,
bottomLeft: _borderRadius.bottomLeft, bottomRight: _borderRadius.bottomRight,
);
canvas.drawRRect(clipRRect, paint);
} else {
canvas.drawRect(rect, paint);
}
break; break;
} }
} }
......
...@@ -80,14 +80,17 @@ class InkSplash extends InkFeature { ...@@ -80,14 +80,17 @@ class InkSplash extends InkFeature {
Color color, Color color,
bool containedInkWell: false, bool containedInkWell: false,
RectCallback rectCallback, RectCallback rectCallback,
BorderRadius borderRadius = BorderRadius.zero,
double radius, double radius,
VoidCallback onRemoved, VoidCallback onRemoved,
}) : _position = position, }) : _position = position,
_color = color, _color = color,
_borderRadius = borderRadius,
_targetRadius = radius ?? _getTargetRadius(referenceBox, containedInkWell, rectCallback, position), _targetRadius = radius ?? _getTargetRadius(referenceBox, containedInkWell, rectCallback, position),
_clipCallback = _getClipCallback(referenceBox, containedInkWell, rectCallback), _clipCallback = _getClipCallback(referenceBox, containedInkWell, rectCallback),
_repositionToReferenceBox = !containedInkWell, _repositionToReferenceBox = !containedInkWell,
super(controller: controller, referenceBox: referenceBox, onRemoved: onRemoved) { super(controller: controller, referenceBox: referenceBox, onRemoved: onRemoved) {
assert(_borderRadius != null);
_radiusController = new AnimationController(duration: _kUnconfirmedSplashDuration, vsync: controller.vsync) _radiusController = new AnimationController(duration: _kUnconfirmedSplashDuration, vsync: controller.vsync)
..addListener(controller.markNeedsPaint) ..addListener(controller.markNeedsPaint)
..forward(); ..forward();
...@@ -107,6 +110,7 @@ class InkSplash extends InkFeature { ...@@ -107,6 +110,7 @@ class InkSplash extends InkFeature {
} }
final Point _position; final Point _position;
final BorderRadius _borderRadius;
final double _targetRadius; final double _targetRadius;
final RectCallback _clipCallback; final RectCallback _clipCallback;
final bool _repositionToReferenceBox; final bool _repositionToReferenceBox;
...@@ -158,6 +162,26 @@ class InkSplash extends InkFeature { ...@@ -158,6 +162,26 @@ class InkSplash extends InkFeature {
super.dispose(); super.dispose();
} }
RRect _clipRRectFromRect(Rect rect) {
return new RRect.fromRectAndCorners(
rect,
topLeft: _borderRadius.topLeft, topRight: _borderRadius.topRight,
bottomLeft: _borderRadius.bottomLeft, bottomRight: _borderRadius.bottomRight,
);
}
void _clipCanvasWithRect(Canvas canvas, Rect rect, {Offset offset}) {
Rect clipRect = rect;
if (offset != null) {
clipRect = clipRect.shift(offset);
}
if (_borderRadius != BorderRadius.zero) {
canvas.clipRRect(_clipRRectFromRect(clipRect));
} else {
canvas.clipRect(clipRect);
}
}
@override @override
void paintFeature(Canvas canvas, Matrix4 transform) { void paintFeature(Canvas canvas, Matrix4 transform) {
final Paint paint = new Paint()..color = _color.withAlpha(_alpha.value); final Paint paint = new Paint()..color = _color.withAlpha(_alpha.value);
...@@ -168,14 +192,15 @@ class InkSplash extends InkFeature { ...@@ -168,14 +192,15 @@ class InkSplash extends InkFeature {
if (originOffset == null) { if (originOffset == null) {
canvas.save(); canvas.save();
canvas.transform(transform.storage); canvas.transform(transform.storage);
if (_clipCallback != null) if (_clipCallback != null) {
canvas.clipRect(_clipCallback()); _clipCanvasWithRect(canvas, _clipCallback());
}
canvas.drawCircle(center, _radius.value, paint); canvas.drawCircle(center, _radius.value, paint);
canvas.restore(); canvas.restore();
} else { } else {
if (_clipCallback != null) { if (_clipCallback != null) {
canvas.save(); canvas.save();
canvas.clipRect(_clipCallback().shift(originOffset)); _clipCanvasWithRect(canvas, _clipCallback(), offset: originOffset);
} }
canvas.drawCircle(center + originOffset, _radius.value, paint); canvas.drawCircle(center + originOffset, _radius.value, paint);
if (_clipCallback != null) if (_clipCallback != null)
......
...@@ -41,6 +41,7 @@ class InkResponse extends StatefulWidget { ...@@ -41,6 +41,7 @@ class InkResponse extends StatefulWidget {
this.containedInkWell: false, this.containedInkWell: false,
this.highlightShape: BoxShape.circle, this.highlightShape: BoxShape.circle,
this.radius, this.radius,
this.borderRadius: BorderRadius.zero,
this.highlightColor, this.highlightColor,
this.splashColor, this.splashColor,
}) : super(key: key); }) : super(key: key);
...@@ -73,6 +74,9 @@ class InkResponse extends StatefulWidget { ...@@ -73,6 +74,9 @@ class InkResponse extends StatefulWidget {
/// The radius of the ink splash. /// The radius of the ink splash.
final double radius; final double radius;
/// The clipping radius of the containing rect.
final BorderRadius borderRadius;
/// The highlight color of the ink response. If this property is null then the /// The highlight color of the ink response. If this property is null then the
/// highlight color of the theme will be used. /// highlight color of the theme will be used.
final Color highlightColor; final Color highlightColor;
...@@ -128,6 +132,7 @@ class _InkResponseState<T extends InkResponse> extends State<T> { ...@@ -128,6 +132,7 @@ class _InkResponseState<T extends InkResponse> extends State<T> {
referenceBox: referenceBox, referenceBox: referenceBox,
color: config.highlightColor ?? Theme.of(context).highlightColor, color: config.highlightColor ?? Theme.of(context).highlightColor,
shape: config.highlightShape, shape: config.highlightShape,
borderRadius: config.borderRadius,
rectCallback: config.getRectCallback(referenceBox), rectCallback: config.getRectCallback(referenceBox),
onRemoved: () { onRemoved: () {
assert(_lastHighlight != null); assert(_lastHighlight != null);
...@@ -157,6 +162,7 @@ class _InkResponseState<T extends InkResponse> extends State<T> { ...@@ -157,6 +162,7 @@ class _InkResponseState<T extends InkResponse> extends State<T> {
containedInkWell: config.containedInkWell, containedInkWell: config.containedInkWell,
rectCallback: config.containedInkWell ? rectCallback : null, rectCallback: config.containedInkWell ? rectCallback : null,
radius: config.radius, radius: config.radius,
borderRadius: config.borderRadius ?? BorderRadius.zero,
onRemoved: () { onRemoved: () {
if (_splashes != null) { if (_splashes != null) {
assert(_splashes.contains(splash)); assert(_splashes.contains(splash));
...@@ -256,6 +262,7 @@ class InkWell extends InkResponse { ...@@ -256,6 +262,7 @@ class InkWell extends InkResponse {
ValueChanged<bool> onHighlightChanged, ValueChanged<bool> onHighlightChanged,
Color highlightColor, Color highlightColor,
Color splashColor, Color splashColor,
BorderRadius borderRadius,
}) : super( }) : super(
key: key, key: key,
child: child, child: child,
...@@ -267,5 +274,6 @@ class InkWell extends InkResponse { ...@@ -267,5 +274,6 @@ class InkWell extends InkResponse {
highlightShape: BoxShape.rectangle, highlightShape: BoxShape.rectangle,
highlightColor: highlightColor, highlightColor: highlightColor,
splashColor: splashColor, splashColor: splashColor,
borderRadius: borderRadius,
); );
} }
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_test/src/widget_tester.dart';
void main() {
testWidgets('Does the ink widget render a border radius', (WidgetTester tester) async {
final Color highlightColor = new Color(0xAAFF0000);
final Color splashColor = new Color(0xAA0000FF);
final Key materialKey = new UniqueKey();
final Key inkKey = new UniqueKey();
final BorderRadius borderRadius = new BorderRadius.circular(6.0);
await tester.pumpWidget(
new Material(
key: materialKey,
child: new Center(
child: new Container(
width: 200.0,
height: 60.0,
child: new InkWell(
key: inkKey,
borderRadius: borderRadius,
highlightColor: highlightColor,
splashColor: splashColor,
onTap: () {},
),
),
),
),
);
final Point center = tester.getCenter(find.byKey(materialKey));
final TestGesture gesture = await tester.startGesture(center);
await tester.pump(new Duration(milliseconds: 200));
// TODO(ianh) - stub. needs to be completed.
await gesture.up();
});
}
\ No newline at end of file
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