From b823e47f0ed89ddf609d82cfe47e35a1fef57f91 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 23 Nov 2015 11:28:44 -0800 Subject: [PATCH] Add avatars to material chips Fixes #464 --- examples/material_gallery/lib/chip_demo.dart | 6 ++- packages/flutter/lib/src/material/chip.dart | 40 +++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/examples/material_gallery/lib/chip_demo.dart b/examples/material_gallery/lib/chip_demo.dart index 2577400b738..66ee104e7be 100644 --- a/examples/material_gallery/lib/chip_demo.dart +++ b/examples/material_gallery/lib/chip_demo.dart @@ -23,7 +23,11 @@ class _ChipDemoState extends State { List chips = [ new Chip( label: new Text('Apple') - ) + ), + new Chip( + avatar: new CircleAvatar(label: 'B'), + label: new Text('Blueberry') + ), ]; if (_showBananas) { diff --git a/packages/flutter/lib/src/material/chip.dart b/packages/flutter/lib/src/material/chip.dart index e505c3e42c7..73aa1b77828 100644 --- a/packages/flutter/lib/src/material/chip.dart +++ b/packages/flutter/lib/src/material/chip.dart @@ -7,6 +7,9 @@ import 'package:flutter/widgets.dart'; import 'colors.dart'; import 'icon.dart'; +const double _kChipHeight = 32.0; +const double _kAvatarDiamater = _kChipHeight; + const TextStyle _kLabelStyle = const TextStyle( inherit: false, fontSize: 13.0, @@ -21,26 +24,39 @@ final ColorFilter _kIconColorFilter = new ColorFilter.mode( class Chip extends StatelessComponent { const Chip({ Key key, - this.icon, + this.avatar, this.label, this.onDeleted }) : super(key: key); - final Widget icon; + final Widget avatar; final Widget label; final VoidCallback onDeleted; Widget build(BuildContext context) { final bool deletable = onDeleted != null; + double leftPadding = 12.0; + double rightPadding = 12.0; - List children = [ - new DefaultTextStyle( - style: _kLabelStyle, - child: label - ) - ]; + List children = []; + + if (avatar != null) { + leftPadding = 0.0; + children.add(new Container( + margin: const EdgeDims.only(right: 8.0), + width: _kAvatarDiamater, + height: _kAvatarDiamater, + child: avatar + )); + } + + children.add(new DefaultTextStyle( + style: _kLabelStyle, + child: label + )); if (deletable) { + rightPadding = 0.0; children.add(new GestureDetector( onTap: onDeleted, child: new Container( @@ -54,13 +70,9 @@ class Chip extends StatelessComponent { )); } - EdgeDims padding = deletable ? - new EdgeDims.only(left: 12.0) : - new EdgeDims.symmetric(horizontal: 12.0); - return new Container( - height: 32.0, - padding: padding, + height: _kChipHeight, + padding: new EdgeDims.only(left: leftPadding, right: rightPadding), decoration: new BoxDecoration( backgroundColor: Colors.grey[300], borderRadius: 16.0