drawer_header.dart 3.36 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
import 'package:flutter/widgets.dart';
6

7
import 'debug.dart';
8
import 'divider.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
/// Part of the Material Design [Drawer].
18
///
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.io/design/components/navigation-drawer.html>
27
class DrawerHeader extends StatelessWidget {
28
  /// Creates a Material Design drawer header.
29 30
  ///
  /// Requires one of its ancestors to be a [Material] widget.
31
  const DrawerHeader({
32
    super.key,
33
    this.decoration,
34 35 36 37
    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,
38
    required this.child,
39
  });
40

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

  /// 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
  ///
71
  /// {@macro flutter.widgets.ProxyWidget.child}
72
  final Widget? child;
73

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