bottom_sheet.dart 8.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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.

import 'dart:async';

import 'package:flutter/widgets.dart';

import 'colors.dart';
import 'material.dart';

const Duration _kBottomSheetDuration = const Duration(milliseconds: 200);
13
const double _kMinFlingVelocity = 700.0;
14
const double _kCloseProgressThreshold = 0.5;
15 16
const Color _kTransparent = const Color(0x00000000);
const Color _kBarrierColor = Colors.black54;
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/// A material design bottom sheet.
///
/// There are two kinds of bottom sheets in material design:
///
///  * _Persistent_. A persistent bottom sheet shows information that
///    supplements the primary content of the app. A persistent bottom sheet
///    remains visible even when the user interacts with other parts of the app.
///    Persistent bottom sheets can be created and displayed with the
///    [Scaffold.showBottomSheet] function.
///
///  * _Modal_. A modal bottom sheet is an alternative to a menu or a dialog and
///    prevents the user from interacting with the rest of the app. Modal bottom
///    sheets can be created and displayed with the [showModalBottomSheet]
///    function.
///
/// The [BottomSheet] widget itself is rarely used directly. Instead, prefer to
/// create a persistent bottom sheet with [Scaffold.showBottomSheet] and a modal
/// bottom sheet with [showModalBottomSheet].
///
/// See also:
///
///  * [Scaffold.showBottomSheet]
///  * [showModalBottomSheet]
///  * <https://www.google.com/design/spec/components/bottom-sheets.html>
42
class BottomSheet extends StatefulWidget {
43 44 45 46 47
  /// Creates a bottom sheet.
  ///
  /// Typically, bottom sheets are created implicitly by
  /// [Scaffold.showBottomSheet], for persistent bottom sheets, or by
  /// [showModalBottomSheet], for modal bottom sheets.
48
  BottomSheet({
49
    Key key,
50
    this.animationController,
51 52 53 54
    this.onClosing,
    this.builder
  }) : super(key: key) {
    assert(onClosing != null);
55
    assert(builder != null);
56
  }
57

58 59 60 61
  /// The animation that controls the bottom sheet's position.
  ///
  /// The BottomSheet widget will manipulate the position of this animation, it
  /// is not just a passive observer.
62
  final AnimationController animationController;
63 64 65 66 67 68

  /// Called when the bottom sheet begins to close.
  ///
  /// A bottom sheet might be be prevented from closing (e.g., by user
  /// interaction) even after this callback is called. For this reason, this
  /// callback might be call multiple times for a given bottom sheet.
69
  final VoidCallback onClosing;
70 71 72 73 74

  /// A builder for the contents of the sheet.
  ///
  /// The bottom sheet will wrap the widget produced by this builder in a
  /// [Material] widget.
75 76
  final WidgetBuilder builder;

77
  @override
78 79
  _BottomSheetState createState() => new _BottomSheetState();

80
  /// Creates an animation controller suitable for controlling a [BottomSheet].
81 82
  static AnimationController createAnimationController() {
    return new AnimationController(
83 84 85 86
      duration: _kBottomSheetDuration,
      debugLabel: 'BottomSheet'
    );
  }
87 88 89 90
}

class _BottomSheetState extends State<BottomSheet> {

91
  final GlobalKey _childKey = new GlobalKey(debugLabel: 'BottomSheet child');
92 93 94 95 96

  double get _childHeight {
    final RenderBox renderBox = _childKey.currentContext.findRenderObject();
    return renderBox.size.height;
  }
97

Adam Barth's avatar
Adam Barth committed
98
  bool get _dismissUnderway => config.animationController.status == AnimationStatus.reverse;
99

100
  void _handleDragUpdate(DragUpdateDetails details) {
101 102
    if (_dismissUnderway)
      return;
103
    config.animationController.value -= details.primaryDelta / (_childHeight ?? details.primaryDelta);
104 105
  }

106
  void _handleDragEnd(DragEndDetails details) {
107 108
    if (_dismissUnderway)
      return;
109 110
    if (details.velocity.pixelsPerSecond.dy > _kMinFlingVelocity) {
      double flingVelocity = -details.velocity.pixelsPerSecond.dy / _childHeight;
111
      config.animationController.fling(velocity: flingVelocity);
112
      if (flingVelocity < 0.0)
113
        config.onClosing();
114 115
    } else if (config.animationController.value < _kCloseProgressThreshold) {
      config.animationController.fling(velocity: -1.0);
116
      config.onClosing();
117
    } else {
118
      config.animationController.forward();
119
    }
120 121
  }

122
  @override
123 124 125
  Widget build(BuildContext context) {
    return new GestureDetector(
      onVerticalDragUpdate: _handleDragUpdate,
126
      onVerticalDragEnd: _handleDragEnd,
127
      child: new Material(
128
        key: _childKey,
129
        child: config.builder(context)
130
      )
131 132 133 134
    );
  }
}

135
// PERSISTENT BOTTOM SHEETS
136

137
// See scaffold.dart
138 139


140
// MODAL BOTTOM SHEETS
141

142
class _ModalBottomSheetLayout extends SingleChildLayoutDelegate {
143 144 145
  _ModalBottomSheetLayout(this.progress);

  final double progress;
146

147
  @override
148 149 150 151 152 153 154 155 156
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    return new BoxConstraints(
      minWidth: constraints.maxWidth,
      maxWidth: constraints.maxWidth,
      minHeight: 0.0,
      maxHeight: constraints.maxHeight * 9.0 / 16.0
    );
  }

157
  @override
158 159
  Offset getPositionForChild(Size size, Size childSize) {
    return new Offset(0.0, size.height - childSize.height * progress);
160 161
  }

162
  @override
163 164
  bool shouldRelayout(_ModalBottomSheetLayout oldDelegate) {
    return progress != oldDelegate.progress;
165 166 167
  }
}

168
class _ModalBottomSheet<T> extends StatefulWidget {
169 170
  _ModalBottomSheet({ Key key, this.route }) : super(key: key);

171
  final _ModalBottomSheetRoute<T> route;
172

173
  @override
174
  _ModalBottomSheetState<T> createState() => new _ModalBottomSheetState<T>();
175 176
}

177
class _ModalBottomSheetState<T> extends State<_ModalBottomSheet<T>> {
178
  @override
179
  Widget build(BuildContext context) {
180
    return new GestureDetector(
Hixie's avatar
Hixie committed
181
      onTap: () => Navigator.pop(context),
182
      child: new AnimatedBuilder(
183
        animation: config.route.animation,
184
        builder: (BuildContext context, Widget child) {
185
          return new ClipRect(
186
            child: new CustomSingleChildLayout(
187
              delegate: new _ModalBottomSheetLayout(config.route.animation.value),
188
              child: new BottomSheet(
189
                animationController: config.route._animationController,
Hixie's avatar
Hixie committed
190
                onClosing: () => Navigator.pop(context),
191
                builder: config.route.builder
192
              )
193
            )
194 195 196
          );
        }
      )
197 198 199 200
    );
  }
}

Hixie's avatar
Hixie committed
201 202 203 204 205
class _ModalBottomSheetRoute<T> extends PopupRoute<T> {
  _ModalBottomSheetRoute({
    Completer<T> completer,
    this.builder
  }) : super(completer: completer);
206 207 208

  final WidgetBuilder builder;

209
  @override
Hixie's avatar
Hixie committed
210
  Duration get transitionDuration => _kBottomSheetDuration;
211 212

  @override
Hixie's avatar
Hixie committed
213
  bool get barrierDismissable => true;
214 215

  @override
Hixie's avatar
Hixie committed
216
  Color get barrierColor => Colors.black54;
217

218 219
  AnimationController _animationController;

220
  @override
221
  AnimationController createAnimationController() {
222 223 224
    assert(_animationController == null);
    _animationController = BottomSheet.createAnimationController();
    return _animationController;
225 226
  }

227
  @override
228
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> forwardAnimation) {
229
    return new _ModalBottomSheet<T>(route: this);
230 231 232
  }
}

233 234 235 236 237 238 239 240 241 242 243
/// Shows a modal material design bottom sheet.
///
/// A modal bottom sheet is an alternative to a menu or a dialog and prevents
/// the user from interacting with the rest of the app.
///
/// A closely related widget is a persistent bottom sheet, which shows
/// information that supplements the primary content of the app without
/// preventing the use from interacting with the app. Persistent bottom sheets
/// can be created and displayed with the [Scaffold.showBottomSheet] function.
///
/// Returns a `Future` that resolves to the value (if any) that was passed to
244
/// [Navigator.pop] when the modal bottom sheet was closed.
245 246 247 248 249 250
///
/// See also:
///
///  * [BottomSheet]
///  * [Scaffold.showBottomSheet]
///  * <https://www.google.com/design/spec/components/bottom-sheets.html#bottom-sheets-modal-bottom-sheets>
251
Future<dynamic/*=T*/> showModalBottomSheet/*<T>*/({ BuildContext context, WidgetBuilder builder }) {
252 253
  assert(context != null);
  assert(builder != null);
254 255
  final Completer<dynamic/*=T*/> completer = new Completer<dynamic/*=T*/>();
  Navigator.push(context, new _ModalBottomSheetRoute<dynamic/*=T*/>(
256
    completer: completer,
257
    builder: builder
258 259 260
  ));
  return completer.future;
}