circle_avatar.dart 1.73 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
/// 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.
///
16
/// See also:
17
///
18 19 20
///  * [Chip]
///  * [ListItem]
///  * <https://www.google.com/design/spec/components/chips.html#chips-contact-chips>
21
class CircleAvatar extends StatelessWidget {
22
  /// Creates a circle that represents a user.
Adam Barth's avatar
Adam Barth committed
23 24
  CircleAvatar({
    Key key,
Hans Muller's avatar
Hans Muller committed
25
    this.child,
Adam Barth's avatar
Adam Barth committed
26
    this.backgroundColor,
Hans Muller's avatar
Hans Muller committed
27
    this.radius: 40.0
Adam Barth's avatar
Adam Barth committed
28 29
  }) : super(key: key);

30
  /// The widget below this widget in the tree.
Hans Muller's avatar
Hans Muller committed
31
  final Widget child;
Hixie's avatar
Hixie committed
32 33 34

  /// 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
35
  final Color backgroundColor;
Hixie's avatar
Hixie committed
36 37 38

  /// 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
39
  final double radius;
Adam Barth's avatar
Adam Barth committed
40

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

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