drawer_header.dart 3.41 KB
Newer Older
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/foundation.dart';
6
import 'package:flutter/widgets.dart';
7

8
import 'debug.dart';
9
import 'theme.dart';
10

11
const double _kDrawerHeaderHeight = 160.0 + 1.0; // bottom edge
12

13
/// The top-most region of a material design drawer. The header's [child]
14 15
/// widget, if any, is placed inside a [Container] whose [decoration] can be
/// passed as an argument, inset by the given [padding].
16 17 18
///
/// Part of the material design [Drawer].
///
19
/// Requires one of its ancestors to be a [Material] widget. This condition is
20
/// satisfied by putting the [DrawerHeader] in a [Drawer].
21 22
///
/// See also:
23
///
24 25
///  * [UserAccountsDrawerHeader], a variant of [DrawerHeader] that is
///    specialized for showing user accounts.
26
///  * <https://material.google.com/patterns/navigation-drawer.html>
27
class DrawerHeader extends StatelessWidget {
28 29 30
  /// Creates a material design drawer header.
  ///
  /// Requires one of its ancestors to be a [Material] widget.
31 32 33
  const DrawerHeader({
    Key key,
    this.decoration,
34
    this.margin: const EdgeInsets.only(bottom: 8.0),
35 36 37
    this.padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 8.0),
    this.duration: const Duration(milliseconds: 250),
    this.curve: Curves.fastOutSlowIn,
38
    @required this.child,
39
  }) : super(key: key);
40

41 42
  /// Decoration for the main drawer header [Container]; useful for applying
  /// backgrounds.
43 44 45 46 47 48 49 50 51 52 53 54
  ///
  /// This decoration will extend under the system status bar.
  ///
  /// If this is changed, it will be animated according to [duration] and [curve].
  final Decoration decoration;

  /// The padding by which to inset [child].
  ///
  /// The [DrawerHeader] additionally offsets the child by the height of the
  /// system status bar.
  ///
  /// If the child is null, the padding has no effect.
55
  final EdgeInsetsGeometry padding;
56

57
  /// The margin around the drawer header.
58
  final EdgeInsetsGeometry margin;
59

60 61 62 63 64
  /// The duration for animations of the [decoration].
  final Duration duration;

  /// The curve for animations of the [decoration].
  final Curve curve;
65

66
  /// A widget to be placed inside the drawer header, inset by the [padding].
67 68 69
  ///
  /// This widget will be sized to the size of the header. To position the child
  /// precisely, consider using an [Align] or [Center] widget.
70
  final Widget child;
71

72
  @override
73
  Widget build(BuildContext context) {
74
    assert(debugCheckHasMaterial(context));
75
    assert(debugCheckHasMediaQuery(context));
76
    final ThemeData theme = Theme.of(context);
77
    final double statusBarHeight = MediaQuery.of(context).padding.top;
78
    return new Container(
79
      height: statusBarHeight + _kDrawerHeaderHeight,
80
      margin: margin,
81
      decoration: new BoxDecoration(
82 83 84
        border: new Border(
          bottom: new BorderSide(
            color: theme.dividerColor,
85 86 87
            width: 0.0,
          ),
        ),
88
      ),
89
      child: new AnimatedContainer(
90
        padding: padding.add(new EdgeInsets.only(top: statusBarHeight)),
91
        decoration: decoration,
92 93 94
        duration: duration,
        curve: curve,
        child: child == null ? null : new DefaultTextStyle(
95
          style: theme.textTheme.body2,
Ian Hickson's avatar
Ian Hickson committed
96 97 98 99 100
          child: new MediaQuery.removePadding(
            context: context,
            removeTop: true,
            child: child,
          ),
101 102
        ),
      ),
103 104 105
    );
  }
}