circle_avatar.dart 3.51 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
import 'package:flutter/widgets.dart';

import 'constants.dart';
import 'theme.dart';
10
import 'typography.dart';
Adam Barth's avatar
Adam Barth committed
11

Hixie's avatar
Hixie committed
12 13
/// A circle that represents a user.
///
14
/// Typically used with a user's profile image, or, in the absence of
Hixie's avatar
Hixie committed
15 16 17
/// such an image, the user's initials. A given user's initials should
/// always be paired with the same background color, for consistency.
///
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/// 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(
34
///   backgroundColor: Colors.brown.shade800,
35 36 37 38
///   child: new Text('AH'),
/// );
/// ```
///
39
/// See also:
40
///
41
///  * [Chip], for representing users or concepts in long form.
42 43
///  * [ListTile], which can combine an icon (such as a [CircleAvatar]) with some
///    text for a fixed height list entry.
44
///  * <https://material.google.com/components/chips.html#chips-contact-chips>
45
class CircleAvatar extends StatelessWidget {
46
  /// Creates a circle that represents a user.
47
  const CircleAvatar({
Adam Barth's avatar
Adam Barth committed
48
    Key key,
Hans Muller's avatar
Hans Muller committed
49
    this.child,
Adam Barth's avatar
Adam Barth committed
50
    this.backgroundColor,
51
    this.backgroundImage,
52 53
    this.foregroundColor,
    this.radius: 20.0,
Adam Barth's avatar
Adam Barth committed
54 55
  }) : super(key: key);

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

  /// The color with which to fill the circle. Changing the background
  /// color will cause the avatar to animate to the new color.
64 65
  ///
  /// If a background color is not specified, the theme's primary color is used.
Adam Barth's avatar
Adam Barth committed
66
  final Color backgroundColor;
Hixie's avatar
Hixie committed
67

68 69 70 71 72 73
  /// The default text color for text in the circle.
  ///
  /// Falls back to white if a background color is specified, or the primary
  /// text theme color otherwise.
  final Color foregroundColor;

74 75
  /// The background image of the circle. Changing the background
  /// image will cause the avatar to animate to the new image.
76 77
  ///
  /// If the [CircleAvatar] is to have the user's initials, use [child] instead.
78 79
  final ImageProvider backgroundImage;

Hixie's avatar
Hixie committed
80 81
  /// The size of the avatar. Changing the radius will cause the
  /// avatar to animate to the new size.
82 83
  ///
  /// Defaults to 20 logical pixels.
Hans Muller's avatar
Hans Muller committed
84
  final double radius;
Adam Barth's avatar
Adam Barth committed
85

86
  @override
Adam Barth's avatar
Adam Barth committed
87
  Widget build(BuildContext context) {
Hans Muller's avatar
Hans Muller committed
88
    final ThemeData theme = Theme.of(context);
89 90 91
    final TextStyle textStyle = backgroundColor != null ?
        new Typography(platform: theme.platform).white.title :
        theme.primaryTextTheme.title;
Adam Barth's avatar
Adam Barth committed
92
    return new AnimatedContainer(
93 94
      width: radius * 2.0,
      height: radius * 2.0,
Adam Barth's avatar
Adam Barth committed
95 96
      duration: kThemeChangeDuration,
      decoration: new BoxDecoration(
97 98
        color: backgroundColor ?? theme.primaryColor,
        image: backgroundImage != null ? new DecorationImage(
99 100
          image: backgroundImage
        ) : null,
101
        shape: BoxShape.circle,
Adam Barth's avatar
Adam Barth committed
102
      ),
103
      child: child != null ? new Center(
104
        child: new DefaultTextStyle(
105
          style: textStyle.copyWith(color: foregroundColor),
106
          child: child,
Hans Muller's avatar
Hans Muller committed
107
        )
108
      ) : null,
Adam Barth's avatar
Adam Barth committed
109 110 111
    );
  }
}