Commit a8ea45f9 authored by Adam Barth's avatar Adam Barth

Merge pull request #1535 from abarth/button_highlight

FlatButton highlights but doesn't tap around edge
parents dd6d4676 1cea4846
...@@ -26,7 +26,15 @@ class DrawerItem extends StatefulComponent { ...@@ -26,7 +26,15 @@ class DrawerItem extends StatefulComponent {
_DrawerItemState createState() => new _DrawerItemState(); _DrawerItemState createState() => new _DrawerItemState();
} }
class _DrawerItemState extends ButtonState<DrawerItem> { class _DrawerItemState extends State<DrawerItem> {
bool _highlight = false;
void _handleHighlightChanged(bool value) {
setState(() {
_highlight = value;
});
}
TextStyle _getTextStyle(ThemeData themeData) { TextStyle _getTextStyle(ThemeData themeData) {
TextStyle result = themeData.text.body2; TextStyle result = themeData.text.body2;
if (config.selected) if (config.selected)
...@@ -35,7 +43,7 @@ class _DrawerItemState extends ButtonState<DrawerItem> { ...@@ -35,7 +43,7 @@ class _DrawerItemState extends ButtonState<DrawerItem> {
} }
Color _getBackgroundColor(ThemeData themeData) { Color _getBackgroundColor(ThemeData themeData) {
if (highlight) if (_highlight)
return themeData.highlightColor; return themeData.highlightColor;
if (config.selected) if (config.selected)
return themeData.selectedColor; return themeData.selectedColor;
...@@ -48,7 +56,7 @@ class _DrawerItemState extends ButtonState<DrawerItem> { ...@@ -48,7 +56,7 @@ class _DrawerItemState extends ButtonState<DrawerItem> {
return new sky.ColorFilter.mode(const Color(0x73000000), sky.TransferMode.dstIn); return new sky.ColorFilter.mode(const Color(0x73000000), sky.TransferMode.dstIn);
} }
Widget buildContent(BuildContext context) { Widget build(BuildContext context) {
ThemeData themeData = Theme.of(context); ThemeData themeData = Theme.of(context);
List<Widget> flexChildren = new List<Widget>(); List<Widget> flexChildren = new List<Widget>();
...@@ -80,6 +88,7 @@ class _DrawerItemState extends ButtonState<DrawerItem> { ...@@ -80,6 +88,7 @@ class _DrawerItemState extends ButtonState<DrawerItem> {
decoration: new BoxDecoration(backgroundColor: _getBackgroundColor(themeData)), decoration: new BoxDecoration(backgroundColor: _getBackgroundColor(themeData)),
child: new InkWell( child: new InkWell(
onTap: config.onPressed, onTap: config.onPressed,
onHighlightChanged: _handleHighlightChanged,
child: new Row(flexChildren) child: new Row(flexChildren)
) )
); );
......
...@@ -30,8 +30,16 @@ class FloatingActionButton extends StatefulComponent { ...@@ -30,8 +30,16 @@ class FloatingActionButton extends StatefulComponent {
_FloatingActionButtonState createState() => new _FloatingActionButtonState(); _FloatingActionButtonState createState() => new _FloatingActionButtonState();
} }
class _FloatingActionButtonState extends ButtonState<FloatingActionButton> { class _FloatingActionButtonState extends State<FloatingActionButton> {
Widget buildContent(BuildContext context) { bool _highlight = false;
void _handleHighlightChanged(bool value) {
setState(() {
_highlight = value;
});
}
Widget build(BuildContext context) {
IconThemeColor iconThemeColor = IconThemeColor.white; IconThemeColor iconThemeColor = IconThemeColor.white;
Color materialColor = config.backgroundColor; Color materialColor = config.backgroundColor;
if (materialColor == null) { if (materialColor == null) {
...@@ -43,13 +51,14 @@ class _FloatingActionButtonState extends ButtonState<FloatingActionButton> { ...@@ -43,13 +51,14 @@ class _FloatingActionButtonState extends ButtonState<FloatingActionButton> {
return new Material( return new Material(
color: materialColor, color: materialColor,
type: MaterialType.circle, type: MaterialType.circle,
level: highlight ? 3 : 2, level: _highlight ? 3 : 2,
child: new ClipOval( child: new ClipOval(
child: new Container( child: new Container(
width: _kSize, width: _kSize,
height: _kSize, height: _kSize,
child: new InkWell( child: new InkWell(
onTap: config.onPressed, onTap: config.onPressed,
onHighlightChanged: _handleHighlightChanged,
child: new Center( child: new Center(
child: new IconTheme( child: new IconTheme(
data: new IconThemeData(color: iconThemeColor), data: new IconThemeData(color: iconThemeColor),
......
...@@ -98,13 +98,17 @@ class _InkSplash { ...@@ -98,13 +98,17 @@ class _InkSplash {
} }
} }
typedef _HighlightChangedCallback(bool value);
class _RenderInkWell extends RenderProxyBox { class _RenderInkWell extends RenderProxyBox {
_RenderInkWell({ _RenderInkWell({
RenderBox child, RenderBox child,
GestureTapCallback onTap, GestureTapCallback onTap,
GestureLongPressCallback onLongPress GestureLongPressCallback onLongPress,
_HighlightChangedCallback onHighlightChanged
}) : super(child) { }) : super(child) {
this.onTap = onTap; this.onTap = onTap;
this.onHighlightChanged = onHighlightChanged;
this.onLongPress = onLongPress; this.onLongPress = onLongPress;
} }
...@@ -115,6 +119,13 @@ class _RenderInkWell extends RenderProxyBox { ...@@ -115,6 +119,13 @@ class _RenderInkWell extends RenderProxyBox {
_syncTapRecognizer(); _syncTapRecognizer();
} }
_HighlightChangedCallback get onHighlightChanged => _onHighlightChanged;
_HighlightChangedCallback _onHighlightChanged;
void set onHighlightChanged (_HighlightChangedCallback value) {
_onHighlightChanged = value;
_syncTapRecognizer();
}
GestureTapCallback get onLongPress => _onLongPress; GestureTapCallback get onLongPress => _onLongPress;
GestureTapCallback _onLongPress; GestureTapCallback _onLongPress;
void set onLongPress (GestureTapCallback value) { void set onLongPress (GestureTapCallback value) {
...@@ -148,10 +159,11 @@ class _RenderInkWell extends RenderProxyBox { ...@@ -148,10 +159,11 @@ class _RenderInkWell extends RenderProxyBox {
} }
void _syncTapRecognizer() { void _syncTapRecognizer() {
if (onTap == null) { if (onTap == null && onHighlightChanged == null) {
_disposeTapRecognizer(); _disposeTapRecognizer();
} else { } else {
_tap ??= new TapGestureRecognizer(router: FlutterBinding.instance.pointerRouter) _tap ??= new TapGestureRecognizer(router: FlutterBinding.instance.pointerRouter)
..onTapDown = _handleTapDown
..onTap = _handleTap ..onTap = _handleTap
..onTapCancel = _handleTapCancel; ..onTapCancel = _handleTapCancel;
} }
...@@ -176,13 +188,26 @@ class _RenderInkWell extends RenderProxyBox { ...@@ -176,13 +188,26 @@ class _RenderInkWell extends RenderProxyBox {
_longPress = null; _longPress = null;
} }
void _handleTapDown() {
if (onHighlightChanged != null)
onHighlightChanged(true);
}
void _handleTap() { void _handleTap() {
_splashes.last?.confirm(); if (_splashes.isNotEmpty)
_splashes.last.confirm();
if (onHighlightChanged != null)
onHighlightChanged(false);
if (onTap != null)
onTap(); onTap();
} }
void _handleTapCancel() { void _handleTapCancel() {
_splashes.last?.cancel(); _splashes.last?.cancel();
if (onHighlightChanged != null)
onHighlightChanged(false);
} }
void _handleLongPress() { void _handleLongPress() {
...@@ -209,16 +234,19 @@ class InkWell extends OneChildRenderObjectWidget { ...@@ -209,16 +234,19 @@ class InkWell extends OneChildRenderObjectWidget {
Key key, Key key,
Widget child, Widget child,
this.onTap, this.onTap,
this.onHighlightChanged,
this.onLongPress this.onLongPress
}) : super(key: key, child: child); }) : super(key: key, child: child);
final GestureTapCallback onTap; final GestureTapCallback onTap;
final _HighlightChangedCallback onHighlightChanged;
final GestureLongPressCallback onLongPress; final GestureLongPressCallback onLongPress;
_RenderInkWell createRenderObject() => new _RenderInkWell(onTap: onTap, onLongPress: onLongPress); _RenderInkWell createRenderObject() => new _RenderInkWell(onTap: onTap, onHighlightChanged: onHighlightChanged, onLongPress: onLongPress);
void updateRenderObject(_RenderInkWell renderObject, InkWell oldWidget) { void updateRenderObject(_RenderInkWell renderObject, InkWell oldWidget) {
renderObject.onTap = onTap; renderObject.onTap = onTap;
renderObject.onHighlightChanged = onHighlightChanged;
renderObject.onLongPress = onLongPress; renderObject.onLongPress = onLongPress;
} }
} }
...@@ -25,11 +25,19 @@ abstract class MaterialButton extends StatefulComponent { ...@@ -25,11 +25,19 @@ abstract class MaterialButton extends StatefulComponent {
final GestureTapCallback onPressed; final GestureTapCallback onPressed;
} }
abstract class MaterialButtonState<T extends MaterialButton> extends ButtonState<T> { abstract class MaterialButtonState<T extends MaterialButton> extends State<T> {
bool highlight = false;
Color getColor(BuildContext context); Color getColor(BuildContext context);
int get level; int get level;
Widget buildContent(BuildContext context) { void _handleHighlightChanged(bool value) {
setState(() {
highlight = value;
});
}
Widget build(BuildContext context) {
Widget contents = new Container( Widget contents = new Container(
padding: new EdgeDims.symmetric(horizontal: 8.0), padding: new EdgeDims.symmetric(horizontal: 8.0),
child: new Center( child: new Center(
...@@ -47,6 +55,7 @@ abstract class MaterialButtonState<T extends MaterialButton> extends ButtonState ...@@ -47,6 +55,7 @@ abstract class MaterialButtonState<T extends MaterialButton> extends ButtonState
color: getColor(context), color: getColor(context),
child: new InkWell( child: new InkWell(
onTap: config.enabled ? config.onPressed : null, onTap: config.enabled ? config.onPressed : null,
onHighlightChanged: config.enabled ? _handleHighlightChanged : null,
child: contents child: contents
) )
) )
......
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