circle_avatar.dart 1.68 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4 5 6 7 8 9
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

import 'constants.dart';
import 'theme.dart';

Hixie's avatar
Hixie committed
10 11 12 13 14 15 16 17
/// A circle that represents a user.
///
/// Typicially used with a user's profile image, or, in the absence of
/// such an image, the user's initials. A given user's initials should
/// always be paired with the same background color, for consistency.
///
/// This class is used by [Chip].
/// See also: <https://www.google.com/design/spec/components/chips.html#chips-contact-chips>
18
class CircleAvatar extends StatelessWidget {
Adam Barth's avatar
Adam Barth committed
19 20
  CircleAvatar({
    Key key,
Hans Muller's avatar
Hans Muller committed
21
    this.child,
Adam Barth's avatar
Adam Barth committed
22
    this.backgroundColor,
Hans Muller's avatar
Hans Muller committed
23
    this.radius: 40.0
Adam Barth's avatar
Adam Barth committed
24 25
  }) : super(key: key);

26
  /// The widget below this widget in the tree.
Hans Muller's avatar
Hans Muller committed
27
  final Widget child;
Hixie's avatar
Hixie committed
28 29 30

  /// The color with which to fill the circle. Changing the background
  /// color will cause the avatar to animate to the new color.
Adam Barth's avatar
Adam Barth committed
31
  final Color backgroundColor;
Hixie's avatar
Hixie committed
32 33 34

  /// The size of the avatar. Changing the radius will cause the
  /// avatar to animate to the new size.
Hans Muller's avatar
Hans Muller committed
35
  final double radius;
Adam Barth's avatar
Adam Barth committed
36

37
  @override
Adam Barth's avatar
Adam Barth committed
38
  Widget build(BuildContext context) {
Hans Muller's avatar
Hans Muller committed
39 40
    final ThemeData theme = Theme.of(context);
    final Color color = backgroundColor ?? theme.primaryColor;
Adam Barth's avatar
Adam Barth committed
41 42

    return new AnimatedContainer(
Hans Muller's avatar
Hans Muller committed
43 44
      width: radius,
      height: radius,
Adam Barth's avatar
Adam Barth committed
45 46 47
      duration: kThemeChangeDuration,
      decoration: new BoxDecoration(
        backgroundColor: color,
48
        shape: BoxShape.circle
Adam Barth's avatar
Adam Barth committed
49 50
      ),
      child: new Center(
Hans Muller's avatar
Hans Muller committed
51 52 53 54
        child: new DefaultTextStyle(
          style: theme.primaryTextTheme.title,
          child: child
        )
Adam Barth's avatar
Adam Barth committed
55 56 57 58
      )
    );
  }
}