bottom_app_bar.dart 5.88 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 8 9 10
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

11
import 'bottom_app_bar_theme.dart';
12
import 'elevation_overlay.dart';
13 14
import 'material.dart';
import 'scaffold.dart';
15
import 'theme.dart';
16 17 18 19

// Examples can assume:
// Widget bottomAppBarContents;

20
/// A container that is typically used with [Scaffold.bottomNavigationBar], and
21 22 23 24 25
/// can have a notch along the top that makes room for an overlapping
/// [FloatingActionButton].
///
/// Typically used with a [Scaffold] and a [FloatingActionButton].
///
26
/// {@tool snippet}
27
/// ```dart
28 29
/// Scaffold(
///   bottomNavigationBar: BottomAppBar(
30 31 32
///     color: Colors.white,
///     child: bottomAppBarContents,
///   ),
33
///   floatingActionButton: FloatingActionButton(onPressed: null),
34 35
/// )
/// ```
36
/// {@end-tool}
37 38 39
///
/// See also:
///
Dan Field's avatar
Dan Field committed
40
///  * [NotchedShape] which calculates the notch for a notched [BottomAppBar].
41 42 43 44 45
///  * [FloatingActionButton] which the [BottomAppBar] makes a notch for.
///  * [AppBar] for a toolbar that is shown at the top of the screen.
class BottomAppBar extends StatefulWidget {
  /// Creates a bottom application bar.
  ///
46
  /// The [clipBehavior] argument defaults to [Clip.none] and must not be null.
47
  /// Additionally, [elevation] must be non-negative.
48 49 50 51
  ///
  /// If [color], [elevation], or [shape] are null, their [BottomAppBarTheme] values will be used.
  /// If the corresponding [BottomAppBarTheme] property is null, then the default
  /// specified in the property's documentation will be used.
52 53 54
  const BottomAppBar({
    Key key,
    this.color,
55
    this.elevation,
56
    this.shape,
57
    this.clipBehavior = Clip.none,
58
    this.notchMargin = 4.0,
59
    this.child,
60
  }) : assert(elevation == null || elevation >= 0.0),
61
       assert(notchMargin != null),
62
       assert(clipBehavior != null),
63 64 65 66 67 68 69 70 71 72 73
       super(key: key);

  /// The widget below this widget in the tree.
  ///
  /// {@macro flutter.widgets.child}
  ///
  /// Typically this the child will be a [Row], with the first child
  /// being an [IconButton] with the [Icons.menu] icon.
  final Widget child;

  /// The bottom app bar's background color.
74
  ///
75 76 77
  /// If this property is null then [BottomAppBarTheme.color] of
  /// [ThemeData.bottomAppBarTheme] is used. If that's null then
  /// [ThemeData.bottomAppBarColor] is used.
78 79
  final Color color;

80 81 82 83 84
  /// The z-coordinate at which to place this bottom app bar relative to its
  /// parent.
  ///
  /// This controls the size of the shadow below the bottom app bar. The
  /// value is non-negative.
85
  ///
86 87 88
  /// If this property is null then [BottomAppBarTheme.elevation] of
  /// [ThemeData.bottomAppBarTheme] is used. If that's null, the default value
  /// is 8.
89 90
  final double elevation;

91
  /// The notch that is made for the floating action button.
92
  ///
93 94 95
  /// If this property is null then [BottomAppBarTheme.shape] of
  /// [ThemeData.bottomAppBarTheme] is used. If that's null then the shape will
  /// be rectangular with no notch.
96 97
  final NotchedShape shape;

98
  /// {@macro flutter.widgets.Clip}
99 100
  ///
  /// Defaults to [Clip.none], and must not be null.
101 102
  final Clip clipBehavior;

103 104
  /// The margin between the [FloatingActionButton] and the [BottomAppBar]'s
  /// notch.
105
  ///
106 107
  /// Not used if [shape] is null.
  final double notchMargin;
108

109
  @override
110
  State createState() => _BottomAppBarState();
111 112 113 114
}

class _BottomAppBarState extends State<BottomAppBar> {
  ValueListenable<ScaffoldGeometry> geometryListenable;
115
  static const double _defaultElevation = 8.0;
116 117 118 119 120 121 122 123 124

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    geometryListenable = Scaffold.geometryOf(context);
  }

  @override
  Widget build(BuildContext context) {
125 126 127
    final BottomAppBarTheme babTheme = BottomAppBarTheme.of(context);
    final NotchedShape notchedShape = widget.shape ?? babTheme.shape;
    final CustomClipper<Path> clipper = notchedShape != null
128
      ? _BottomAppBarClipper(
129
        geometry: geometryListenable,
130
        shape: notchedShape,
131 132
        notchMargin: widget.notchMargin,
      )
133
      : const ShapeBorderClipper(shape: RoundedRectangleBorder());
134 135 136
    final double elevation = widget.elevation ?? babTheme.elevation ?? _defaultElevation;
    final Color color = widget.color ?? babTheme.color ?? Theme.of(context).bottomAppBarColor;
    final Color effectiveColor = ElevationOverlay.applyOverlay(context, color, elevation);
137
    return PhysicalShape(
138
      clipper: clipper,
139 140
      elevation: elevation,
      color: effectiveColor,
141
      clipBehavior: widget.clipBehavior,
142
      child: Material(
143
        type: MaterialType.transparency,
144 145
        child: widget.child == null
          ? null
146
          : SafeArea(child: widget.child),
147 148 149 150 151 152 153
      ),
    );
  }
}

class _BottomAppBarClipper extends CustomClipper<Path> {
  const _BottomAppBarClipper({
154 155 156
    @required this.geometry,
    @required this.shape,
    @required this.notchMargin,
157
  }) : assert(geometry != null),
158 159
       assert(shape != null),
       assert(notchMargin != null),
160 161 162
       super(reclip: geometry);

  final ValueListenable<ScaffoldGeometry> geometry;
163 164
  final NotchedShape shape;
  final double notchMargin;
165 166 167 168

  @override
  Path getClip(Size size) {
    // button is the floating action button's bounding rectangle in the
169 170 171 172 173 174 175
    // coordinate system whose origin is at the appBar's top left corner,
    // or null if there is no floating action button.
    final Rect button = geometry.value.floatingActionButtonArea?.translate(
      0.0,
      geometry.value.bottomNavigationBarTop * -1.0,
    );
    return shape.getOuterPath(Offset.zero & size, button?.inflate(notchMargin));
176 177 178
  }

  @override
179 180 181 182 183
  bool shouldReclip(_BottomAppBarClipper oldClipper) {
    return oldClipper.geometry != geometry
        || oldClipper.shape != shape
        || oldClipper.notchMargin != notchMargin;
  }
184
}