drawer_header.dart 3.37 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:flutter/widgets.dart';
8

9
import 'debug.dart';
10
import 'divider.dart';
11
import 'theme.dart';
12

13
const double _kDrawerHeaderHeight = 160.0 + 1.0; // bottom edge
14

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

43 44
  /// Decoration for the main drawer header [Container]; useful for applying
  /// backgrounds.
45 46 47 48 49 50 51 52 53 54 55 56
  ///
  /// 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.
57
  final EdgeInsetsGeometry padding;
58

59
  /// The margin around the drawer header.
60
  final EdgeInsetsGeometry margin;
61

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

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

68
  /// A widget to be placed inside the drawer header, inset by the [padding].
69 70 71
  ///
  /// This widget will be sized to the size of the header. To position the child
  /// precisely, consider using an [Align] or [Center] widget.
72 73
  ///
  /// {@macro flutter.widgets.child}
74
  final Widget child;
75

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