From e90dcf8e343e3c45e3768d3dde760e71cdbc4385 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Fri, 23 Oct 2015 16:23:42 -0700 Subject: [PATCH] Adds modal bottom sheets: showModalBottomSheet() --- packages/flutter/lib/material.dart | 1 + .../lib/src/material/bottom_sheet.dart | 117 ++++++++++++++++++ packages/flutter/lib/src/widgets/basic.dart | 2 +- 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 packages/flutter/lib/src/material/bottom_sheet.dart diff --git a/packages/flutter/lib/material.dart b/packages/flutter/lib/material.dart index aae1359b89..a2560cca35 100644 --- a/packages/flutter/lib/material.dart +++ b/packages/flutter/lib/material.dart @@ -7,6 +7,7 @@ /// See https://www.google.com/design/spec/material-design/introduction.html library material; +export 'src/material/bottom_sheet.dart'; export 'src/material/card.dart'; export 'src/material/checkbox.dart'; export 'src/material/circle_avatar.dart'; diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart new file mode 100644 index 0000000000..e9a4245049 --- /dev/null +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -0,0 +1,117 @@ +// 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/animation.dart'; +import 'package:flutter/painting.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/widgets.dart'; + +import 'colors.dart'; +import 'material.dart'; + +const Duration _kBottomSheetDuration = const Duration(milliseconds: 200); + +class _BottomSheet extends StatefulComponent { + _BottomSheet({ + Key key, + this.child, + this.performance + }) : super(key: key); + + final Widget child; + final PerformanceView performance; + + _BottomSheetState createState() => new _BottomSheetState(); +} + +class _BottomSheetLayout extends OneChildLayoutDelegate { + // The distance from the bottom of the parent to the top of the BottomSheet child. + AnimatedValue childTop = new AnimatedValue(0.0); + + BoxConstraints getConstraintsForChild(BoxConstraints constraints) { + return new BoxConstraints( + minWidth: constraints.maxWidth, + maxWidth: constraints.maxWidth, + minHeight: 0.0, + maxHeight: constraints.maxHeight * 9.0 / 16.0 + ); + } + + Point getPositionForChild(Size size, Size childSize) { + childTop.end = childSize.height; + return new Point(0.0, size.height - childTop.value); + } +} + +class _BottomSheetState extends State<_BottomSheet> { + + final _BottomSheetLayout _layout = new _BottomSheetLayout(); + + Widget build(BuildContext context) { + return new BuilderTransition( + performance: config.performance, + variables: >[_layout.childTop], + builder: (BuildContext context) { + return new ClipRect( + child: new CustomOneChildLayout( + delegate: _layout, + token: _layout.childTop.value, + child: new Material(child: config.child) + ) + ); + } + ); + } +} + +class _ModalBottomSheetRoute extends PerformanceRoute { + _ModalBottomSheetRoute({ this.completer, this.child }); + + final Completer completer; + final Widget child; + + bool get ephemeral => true; + bool get modal => true; + bool get opaque => false; + Duration get transitionDuration => _kBottomSheetDuration; + + Widget build(RouteArguments args) { + return new Focus( + key: new GlobalObjectKey(this), + autofocus: true, + child: new GestureDetector( + onTap: () { navigator.pop(); }, + child: new Stack([ + // mask + new ColorTransition( + performance: performance, + color: new AnimatedColorValue(Colors.transparent, end: Colors.black54), + child: new Container() + ), + // sheet + new _BottomSheet( + performance: performance, + child: child + ) + ]) + ) + ); + } + + void didPop([dynamic result]) { + completer.complete(result); + super.didPop(result); + } +} + +Future showModalBottomSheet({ BuildContext context, Widget child }) { + final Completer completer = new Completer(); + Navigator.of(context).push(new _ModalBottomSheetRoute( + completer: completer, + child: child + )); + return completer.future; +} diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 265bb91706..20d75aa171 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -347,7 +347,7 @@ class FractionallySizedBox extends OneChildRenderObjectWidget { heightFactor: height ); - void updateRenderObject(RenderFractionallySizedBox renderObject, SizedBox oldWidget) { + void updateRenderObject(RenderFractionallySizedBox renderObject, FractionallySizedBox oldWidget) { renderObject.widthFactor = width; renderObject.heightFactor = height; }