circle_avatar.dart 3.04 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4
// 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.

5
import 'package:flutter/services.dart';
Adam Barth's avatar
Adam Barth committed
6 7 8 9 10
import 'package:flutter/widgets.dart';

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

Hixie's avatar
Hixie committed
11 12
/// A circle that represents a user.
///
13
/// Typically used with a user's profile image, or, in the absence of
Hixie's avatar
Hixie committed
14 15 16
/// such an image, the user's initials. A given user's initials should
/// always be paired with the same background color, for consistency.
///
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/// If the avatar is to have an image, the image should be specified in the
/// [backgroundImage] property:
///
/// ```dart
/// new CircleAvatar(
///   backgroundImage: new NetworkImage(userAvatarUrl),
/// )
/// ```
///
/// The image will be cropped to have a circle shape.
///
/// If the avatar is to just have the user's initials, they are typically
/// provided using a [Text] widget as the [child] and a [backgroundColor]:
///
/// ```dart
/// new CircleAvatar(
///   backgroundColor: Colors.brown[800],
///   child: new Text('AH'),
/// );
/// ```
///
38
/// See also:
39
///
40 41 42
///  * [Chip], for representing users or concepts in long form.
///  * [ListItem], which combines an icon (such as a [CircleAvatar]) with some
///    text in a horizontal row.
43
///  * <https://material.google.com/components/chips.html#chips-contact-chips>
44
class CircleAvatar extends StatelessWidget {
45
  /// Creates a circle that represents a user.
Adam Barth's avatar
Adam Barth committed
46 47
  CircleAvatar({
    Key key,
Hans Muller's avatar
Hans Muller committed
48
    this.child,
Adam Barth's avatar
Adam Barth committed
49
    this.backgroundColor,
50 51
    this.backgroundImage,
    this.radius: 20.0
Adam Barth's avatar
Adam Barth committed
52 53
  }) : super(key: key);

54
  /// The widget below this widget in the tree.
55 56 57
  ///
  /// Typically a [Text] widget. If the [CircleAvatar] is to have an image, use
  /// [backgroundImage] instead.
Hans Muller's avatar
Hans Muller committed
58
  final Widget child;
Hixie's avatar
Hixie committed
59 60 61

  /// 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
62
  final Color backgroundColor;
Hixie's avatar
Hixie committed
63

64 65
  /// The background image of the circle. Changing the background
  /// image will cause the avatar to animate to the new image.
66 67
  ///
  /// If the [CircleAvatar] is to have the user's initials, use [child] instead.
68 69
  final ImageProvider backgroundImage;

Hixie's avatar
Hixie committed
70 71
  /// The size of the avatar. Changing the radius will cause the
  /// avatar to animate to the new size.
72 73
  ///
  /// Defaults to 20 logical pixels.
Hans Muller's avatar
Hans Muller committed
74
  final double radius;
Adam Barth's avatar
Adam Barth committed
75

76
  @override
Adam Barth's avatar
Adam Barth committed
77
  Widget build(BuildContext context) {
Hans Muller's avatar
Hans Muller committed
78 79
    final ThemeData theme = Theme.of(context);
    final Color color = backgroundColor ?? theme.primaryColor;
Adam Barth's avatar
Adam Barth committed
80
    return new AnimatedContainer(
81 82
      width: radius * 2.0,
      height: radius * 2.0,
Adam Barth's avatar
Adam Barth committed
83 84 85
      duration: kThemeChangeDuration,
      decoration: new BoxDecoration(
        backgroundColor: color,
86 87 88
        backgroundImage: backgroundImage != null ? new BackgroundImage(
          image: backgroundImage
        ) : null,
89
        shape: BoxShape.circle,
Adam Barth's avatar
Adam Barth committed
90
      ),
91
      child: child != null ? new Center(
92
        child: new DefaultTextStyle(
Hans Muller's avatar
Hans Muller committed
93
          style: theme.primaryTextTheme.title,
94
          child: child,
Hans Muller's avatar
Hans Muller committed
95
        )
96
      ) : null,
Adam Barth's avatar
Adam Barth committed
97 98 99
    );
  }
}