File size: 10,285 Bytes
7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 e0a2378 7d80aa8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | // Copyright 2014 The Flutter 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:math' as math;
import 'package:flutter/material.dart';
const double _kFrontHeadingHeight = 32.0; // front layer beveled rectangle
const double _kFrontClosedHeight = 92.0; // front layer height when closed
const double _kBackAppBarHeight = 56.0; // back layer (options) appbar height
// The size of the front layer heading's left and right beveled corners.
final Animatable<BorderRadius?> _kFrontHeadingBevelRadius = BorderRadiusTween(
begin: const BorderRadius.only(topLeft: Radius.circular(12.0), topRight: Radius.circular(12.0)),
end: const BorderRadius.only(
topLeft: Radius.circular(_kFrontHeadingHeight),
topRight: Radius.circular(_kFrontHeadingHeight),
),
);
class _TappableWhileStatusIs extends StatefulWidget {
const _TappableWhileStatusIs(this.status, {this.controller, this.child});
final AnimationController? controller;
final AnimationStatus status;
final Widget? child;
@override
_TappableWhileStatusIsState createState() => _TappableWhileStatusIsState();
}
class _TappableWhileStatusIsState extends State<_TappableWhileStatusIs> {
bool? _active;
@override
void initState() {
super.initState();
widget.controller!.addStatusListener(_handleStatusChange);
_active = widget.controller!.status == widget.status;
}
@override
void dispose() {
widget.controller!.removeStatusListener(_handleStatusChange);
super.dispose();
}
void _handleStatusChange(AnimationStatus status) {
final bool value = widget.controller!.status == widget.status;
if (_active != value) {
setState(() {
_active = value;
});
}
}
@override
Widget build(BuildContext context) {
Widget child = AbsorbPointer(absorbing: !_active!, child: widget.child);
if (!_active!) {
child = FocusScope(
canRequestFocus: false,
debugLabel: '$_TappableWhileStatusIs',
child: child,
);
}
return child;
}
}
class _CrossFadeTransition extends AnimatedWidget {
const _CrossFadeTransition({
this.alignment = Alignment.center,
required Animation<double> progress,
this.child0,
this.child1,
}) : super(listenable: progress);
final AlignmentGeometry alignment;
final Widget? child0;
final Widget? child1;
@override
Widget build(BuildContext context) {
final Animation<double> progress = listenable as Animation<double>;
final double opacity1 =
CurvedAnimation(parent: ReverseAnimation(progress), curve: const Interval(0.5, 1.0)).value;
final double opacity2 =
CurvedAnimation(parent: progress, curve: const Interval(0.5, 1.0)).value;
return Stack(
alignment: alignment,
children: <Widget>[
Opacity(
opacity: opacity1,
child: Semantics(scopesRoute: true, explicitChildNodes: true, child: child1),
),
Opacity(
opacity: opacity2,
child: Semantics(scopesRoute: true, explicitChildNodes: true, child: child0),
),
],
);
}
}
class _BackAppBar extends StatelessWidget {
const _BackAppBar({
this.leading = const SizedBox(width: 56.0),
required this.title,
this.trailing,
});
final Widget leading;
final Widget title;
final Widget? trailing;
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
return IconTheme.merge(
data: theme.primaryIconTheme,
child: DefaultTextStyle(
style: theme.primaryTextTheme.titleLarge!,
child: SizedBox(
height: _kBackAppBarHeight,
child: Row(
children: <Widget>[
Container(alignment: Alignment.center, width: 56.0, child: leading),
Expanded(child: title),
if (trailing != null)
Container(alignment: Alignment.center, width: 56.0, child: trailing),
],
),
),
),
);
}
}
class Backdrop extends StatefulWidget {
const Backdrop({
super.key,
this.frontAction,
this.frontTitle,
this.frontHeading,
this.frontLayer,
this.backTitle,
this.backLayer,
});
final Widget? frontAction;
final Widget? frontTitle;
final Widget? frontLayer;
final Widget? frontHeading;
final Widget? backTitle;
final Widget? backLayer;
@override
State<Backdrop> createState() => _BackdropState();
}
class _BackdropState extends State<Backdrop> with SingleTickerProviderStateMixin {
final GlobalKey _backdropKey = GlobalKey(debugLabel: 'Backdrop');
AnimationController? _controller;
late Animation<double> _frontOpacity;
static final Animatable<double> _frontOpacityTween = Tween<double>(
begin: 0.2,
end: 1.0,
).chain(CurveTween(curve: const Interval(0.0, 0.4, curve: Curves.easeInOut)));
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 300),
value: 1.0,
vsync: this,
);
_controller!.addStatusListener((AnimationStatus status) {
setState(() {
// This is intentionally left empty. The state change itself takes
// place inside the AnimationController, so there's nothing to update.
// All we want is for the widget to rebuild and read the new animation
// state from the AnimationController.
});
});
_frontOpacity = _controller!.drive(_frontOpacityTween);
}
@override
void dispose() {
_controller!.dispose();
super.dispose();
}
double get _backdropHeight {
// Warning: this can be safely called from the event handlers but it may
// not be called at build time.
final RenderBox renderBox = _backdropKey.currentContext!.findRenderObject()! as RenderBox;
return math.max(0.0, renderBox.size.height - _kBackAppBarHeight - _kFrontClosedHeight);
}
void _handleDragUpdate(DragUpdateDetails details) {
_controller!.value -= details.primaryDelta! / _backdropHeight;
}
void _handleDragEnd(DragEndDetails details) {
if (!_controller!.isDismissed) {
return;
}
final double flingVelocity = details.velocity.pixelsPerSecond.dy / _backdropHeight;
_controller!.fling(
velocity: switch (flingVelocity) {
< 0.0 => math.max(2.0, -flingVelocity),
> 0.0 => math.min(-2.0, -flingVelocity),
_ => _controller!.value < 0.5 ? -2.0 : 2.0,
},
);
}
void _toggleFrontLayer() {
_controller!.fling(velocity: _controller!.isForwardOrCompleted ? -2.0 : 2.0);
}
Widget _buildStack(BuildContext context, BoxConstraints constraints) {
final Animation<RelativeRect> frontRelativeRect = _controller!.drive(
RelativeRectTween(
begin: RelativeRect.fromLTRB(
0.0,
constraints.biggest.height - _kFrontClosedHeight,
0.0,
0.0,
),
end: const RelativeRect.fromLTRB(0.0, _kBackAppBarHeight, 0.0, 0.0),
),
);
return Stack(
key: _backdropKey,
children: <Widget>[
// Back layer
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
_BackAppBar(
leading: widget.frontAction!,
title: _CrossFadeTransition(
progress: _controller!,
alignment: AlignmentDirectional.centerStart,
child0: Semantics(namesRoute: true, child: widget.frontTitle),
child1: Semantics(namesRoute: true, child: widget.backTitle),
),
trailing: IconButton(
onPressed: _toggleFrontLayer,
tooltip: 'Toggle options page',
icon: AnimatedIcon(icon: AnimatedIcons.close_menu, progress: _controller!),
),
),
Expanded(
child: _TappableWhileStatusIs(
AnimationStatus.dismissed,
controller: _controller,
child: Visibility(
visible: !_controller!.isCompleted,
maintainState: true,
child: widget.backLayer!,
),
),
),
],
),
// Front layer
PositionedTransition(
rect: frontRelativeRect,
child: AnimatedBuilder(
animation: _controller!,
builder: (BuildContext context, Widget? child) {
return PhysicalShape(
elevation: 12.0,
color: Theme.of(context).canvasColor,
clipper: ShapeBorderClipper(
shape: BeveledRectangleBorder(
borderRadius: _kFrontHeadingBevelRadius.transform(_controller!.value)!,
),
),
clipBehavior: Clip.antiAlias,
child: child,
);
},
child: _TappableWhileStatusIs(
AnimationStatus.completed,
controller: _controller,
child: FadeTransition(opacity: _frontOpacity, child: widget.frontLayer),
),
),
),
// The front "heading" is a (typically transparent) widget that's stacked on
// top of, and at the top of, the front layer. It adds support for dragging
// the front layer up and down and for opening and closing the front layer
// with a tap. It may obscure part of the front layer's topmost child.
if (widget.frontHeading != null)
PositionedTransition(
rect: frontRelativeRect,
child: ExcludeSemantics(
child: Container(
alignment: Alignment.topLeft,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: _toggleFrontLayer,
onVerticalDragUpdate: _handleDragUpdate,
onVerticalDragEnd: _handleDragEnd,
child: widget.frontHeading,
),
),
),
),
],
);
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: _buildStack);
}
}
|