|
| 1 | +// Copyright 2018-present the Flutter authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'package:flutter/material.dart'; |
| 16 | +import 'package:meta/meta.dart'; |
| 17 | + |
| 18 | +import 'model/product.dart'; |
| 19 | +import 'login.dart'; |
| 20 | + |
| 21 | +const double _kFlingVelocity = 2.0; |
| 22 | + |
| 23 | +class _FrontLayer extends StatelessWidget { |
| 24 | + const _FrontLayer({ |
| 25 | + Key key, |
| 26 | + this.onTap, |
| 27 | + this.child, |
| 28 | + }) : super(key: key); |
| 29 | + |
| 30 | + final VoidCallback onTap; |
| 31 | + final Widget child; |
| 32 | + |
| 33 | + @override |
| 34 | + Widget build(BuildContext context) { |
| 35 | + return Material( |
| 36 | + elevation: 16.0, |
| 37 | + shape: BeveledRectangleBorder( |
| 38 | + borderRadius: BorderRadius.only(topLeft: Radius.circular(46.0)), |
| 39 | + ), |
| 40 | + child: Column( |
| 41 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 42 | + children: <Widget>[ |
| 43 | + GestureDetector( |
| 44 | + behavior: HitTestBehavior.opaque, |
| 45 | + onTap: onTap, |
| 46 | + child: Container( |
| 47 | + height: 40.0, |
| 48 | + alignment: AlignmentDirectional.centerStart, |
| 49 | + ), |
| 50 | + ), |
| 51 | + Expanded( |
| 52 | + child: child, |
| 53 | + ), |
| 54 | + ], |
| 55 | + ), |
| 56 | + ); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +class _BackdropTitle extends AnimatedWidget { |
| 61 | + final Function onPress; |
| 62 | + final Widget frontTitle; |
| 63 | + final Widget backTitle; |
| 64 | + |
| 65 | + const _BackdropTitle({ |
| 66 | + Key key, |
| 67 | + Listenable listenable, |
| 68 | + this.onPress, |
| 69 | + @required this.frontTitle, |
| 70 | + @required this.backTitle, |
| 71 | + }) : assert(frontTitle != null), |
| 72 | + assert(backTitle != null), |
| 73 | + super(key: key, listenable: listenable); |
| 74 | + |
| 75 | + @override |
| 76 | + Widget build(BuildContext context) { |
| 77 | + final Animation<double> animation = this.listenable; |
| 78 | + |
| 79 | + return DefaultTextStyle( |
| 80 | + style: Theme.of(context).primaryTextTheme.title, |
| 81 | + softWrap: false, |
| 82 | + overflow: TextOverflow.ellipsis, |
| 83 | + child: Row(children: <Widget>[ |
| 84 | + // branded icon |
| 85 | + SizedBox( |
| 86 | + width: 72.0, |
| 87 | + child: IconButton( |
| 88 | + padding: EdgeInsets.only(right: 8.0), |
| 89 | + onPressed: this.onPress, |
| 90 | + icon: Stack(children: <Widget>[ |
| 91 | + Opacity( |
| 92 | + opacity: animation.value, |
| 93 | + child: ImageIcon(AssetImage('assets/slanted_menu.png')), |
| 94 | + ), |
| 95 | + FractionalTranslation( |
| 96 | + translation: Tween<Offset>( |
| 97 | + begin: Offset.zero, |
| 98 | + end: Offset(1.0, 0.0), |
| 99 | + ).evaluate(animation), |
| 100 | + child: ImageIcon(AssetImage('assets/diamond.png')), |
| 101 | + )]), |
| 102 | + ), |
| 103 | + ), |
| 104 | + // Here, we do a custom cross fade between backTitle and frontTitle. |
| 105 | + // This makes a smooth animation between the two texts. |
| 106 | + Stack( |
| 107 | + children: <Widget>[ |
| 108 | + Opacity( |
| 109 | + opacity: CurvedAnimation( |
| 110 | + parent: ReverseAnimation(animation), |
| 111 | + curve: Interval(0.5, 1.0), |
| 112 | + ).value, |
| 113 | + child: FractionalTranslation( |
| 114 | + translation: Tween<Offset>( |
| 115 | + begin: Offset.zero, |
| 116 | + end: Offset(0.5, 0.0), |
| 117 | + ).evaluate(animation), |
| 118 | + child: backTitle, |
| 119 | + ), |
| 120 | + ), |
| 121 | + Opacity( |
| 122 | + opacity: CurvedAnimation( |
| 123 | + parent: animation, |
| 124 | + curve: Interval(0.5, 1.0), |
| 125 | + ).value, |
| 126 | + child: FractionalTranslation( |
| 127 | + translation: Tween<Offset>( |
| 128 | + begin: Offset(-0.25, 0.0), |
| 129 | + end: Offset.zero, |
| 130 | + ).evaluate(animation), |
| 131 | + child: frontTitle, |
| 132 | + ), |
| 133 | + ), |
| 134 | + ], |
| 135 | + ) |
| 136 | + ]), |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +/// Builds a Backdrop. |
| 142 | +/// |
| 143 | +/// A Backdrop widget has two layers, front and back. The front layer is shown |
| 144 | +/// by default, and slides down to show the back layer, from which a user |
| 145 | +/// can make a selection. The user can also configure the titles for when the |
| 146 | +/// front or back layer is showing. |
| 147 | +class Backdrop extends StatefulWidget { |
| 148 | + final Category currentCategory; |
| 149 | + final Widget frontLayer; |
| 150 | + final Widget backLayer; |
| 151 | + final Widget frontTitle; |
| 152 | + final Widget backTitle; |
| 153 | + |
| 154 | + const Backdrop({ |
| 155 | + @required this.currentCategory, |
| 156 | + @required this.frontLayer, |
| 157 | + @required this.backLayer, |
| 158 | + @required this.frontTitle, |
| 159 | + @required this.backTitle, |
| 160 | + }) : assert(currentCategory != null), |
| 161 | + assert(frontLayer != null), |
| 162 | + assert(backLayer != null), |
| 163 | + assert(frontTitle != null), |
| 164 | + assert(backTitle != null); |
| 165 | + |
| 166 | + @override |
| 167 | + _BackdropState createState() => _BackdropState(); |
| 168 | +} |
| 169 | + |
| 170 | +class _BackdropState extends State<Backdrop> |
| 171 | + with SingleTickerProviderStateMixin { |
| 172 | + final GlobalKey _backdropKey = GlobalKey(debugLabel: 'Backdrop'); |
| 173 | + AnimationController _controller; |
| 174 | + |
| 175 | + @override |
| 176 | + void initState() { |
| 177 | + super.initState(); |
| 178 | + _controller = AnimationController( |
| 179 | + duration: Duration(milliseconds: 300), |
| 180 | + value: 1.0, |
| 181 | + vsync: this, |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + @override |
| 186 | + void didUpdateWidget(Backdrop old) { |
| 187 | + super.didUpdateWidget(old); |
| 188 | + |
| 189 | + if (widget.currentCategory != old.currentCategory) { |
| 190 | + _toggleBackdropLayerVisibility(); |
| 191 | + } else if (!_frontLayerVisible) { |
| 192 | + _controller.fling(velocity: _kFlingVelocity); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @override |
| 197 | + void dispose() { |
| 198 | + _controller.dispose(); |
| 199 | + super.dispose(); |
| 200 | + } |
| 201 | + |
| 202 | + bool get _frontLayerVisible { |
| 203 | + final AnimationStatus status = _controller.status; |
| 204 | + return status == AnimationStatus.completed || |
| 205 | + status == AnimationStatus.forward; |
| 206 | + } |
| 207 | + |
| 208 | + void _toggleBackdropLayerVisibility() { |
| 209 | + _controller.fling( |
| 210 | + velocity: _frontLayerVisible ? -_kFlingVelocity : _kFlingVelocity); |
| 211 | + } |
| 212 | + |
| 213 | + Widget _buildStack(BuildContext context, BoxConstraints constraints) { |
| 214 | + const double layerTitleHeight = 48.0; |
| 215 | + final Size layerSize = constraints.biggest; |
| 216 | + final double layerTop = layerSize.height - layerTitleHeight; |
| 217 | + |
| 218 | + Animation<RelativeRect> layerAnimation = RelativeRectTween( |
| 219 | + begin: RelativeRect.fromLTRB( |
| 220 | + 0.0, layerTop, 0.0, layerTop - layerSize.height), |
| 221 | + end: RelativeRect.fromLTRB(0.0, 0.0, 0.0, 0.0), |
| 222 | + ).animate(_controller.view); |
| 223 | + |
| 224 | + return Stack( |
| 225 | + key: _backdropKey, |
| 226 | + children: <Widget>[ |
| 227 | + widget.backLayer, |
| 228 | + PositionedTransition( |
| 229 | + rect: layerAnimation, |
| 230 | + child: _FrontLayer( |
| 231 | + onTap: _toggleBackdropLayerVisibility, |
| 232 | + child: widget.frontLayer, |
| 233 | + ), |
| 234 | + ), |
| 235 | + ], |
| 236 | + ); |
| 237 | + } |
| 238 | + |
| 239 | + @override |
| 240 | + Widget build(BuildContext context) { |
| 241 | + var appBar = AppBar( |
| 242 | + brightness: Brightness.light, |
| 243 | + elevation: 0.0, |
| 244 | + titleSpacing: 0.0, |
| 245 | + title: _BackdropTitle( |
| 246 | + listenable: _controller.view, |
| 247 | + onPress: _toggleBackdropLayerVisibility, |
| 248 | + frontTitle: widget.frontTitle, |
| 249 | + backTitle: widget.backTitle, |
| 250 | + ), |
| 251 | + actions: <Widget>[ |
| 252 | + new IconButton( |
| 253 | + icon: const Icon(Icons.search), |
| 254 | + onPressed: () { |
| 255 | + Navigator.push( |
| 256 | + context, |
| 257 | + MaterialPageRoute(builder: (BuildContext context) => LoginPage()), |
| 258 | + ); |
| 259 | + }, |
| 260 | + ), |
| 261 | + new IconButton( |
| 262 | + icon: const Icon(Icons.tune), |
| 263 | + onPressed: () { |
| 264 | + Navigator.push( |
| 265 | + context, |
| 266 | + MaterialPageRoute(builder: (BuildContext context) => LoginPage()), |
| 267 | + ); |
| 268 | + }, |
| 269 | + ), |
| 270 | + ], |
| 271 | + ); |
| 272 | + return Scaffold( |
| 273 | + appBar: appBar, |
| 274 | + body: LayoutBuilder( |
| 275 | + builder: _buildStack, |
| 276 | + ), |
| 277 | + ); |
| 278 | + } |
| 279 | +} |
0 commit comments