forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawer.dart
156 lines (128 loc) · 4.19 KB
/
drawer.dart
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
// 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 'animated_component.dart';
import '../animation/animated_value.dart';
import '../animation/curves.dart';
import '../fn.dart';
import '../theme/colors.dart';
import '../theme/shadows.dart';
import 'dart:async';
import 'dart:math' as math;
import 'dart:sky' as sky;
import 'material.dart';
const double _kWidth = 304.0;
const double _kMinFlingVelocity = 0.4;
const double _kBaseSettleDurationMS = 246.0;
const double _kMaxSettleDurationMS = 600.0;
const Curve _kAnimationCurve = parabolicRise;
class DrawerController {
final AnimatedValue position = new AnimatedValue(-_kWidth);
bool get isClosed => position.value == -_kWidth;
bool get _isMostlyClosed => position.value <= -_kWidth / 2;
void toggle(_) => _isMostlyClosed ? _open() : _close();
void handleMaskTap(_) => _close();
void handlePointerDown(_) => position.stop();
void handlePointerMove(sky.PointerEvent event) {
if (position.isAnimating)
return;
position.value = math.min(0.0, math.max(position.value + event.dx, -_kWidth));
}
void handlePointerUp(_) {
if (!position.isAnimating)
_settle();
}
void handlePointerCancel(_) {
if (!position.isAnimating)
_settle();
}
void _open() => _animateToPosition(0.0);
void _close() => _animateToPosition(-_kWidth);
void _settle() => _isMostlyClosed ? _close() : _open();
void _animateToPosition(double targetPosition) {
double distance = (targetPosition - position.value).abs();
if (distance != 0) {
double targetDuration = distance / _kWidth * _kBaseSettleDurationMS;
double duration = math.min(targetDuration, _kMaxSettleDurationMS);
position.animateTo(targetPosition, duration, curve: _kAnimationCurve);
}
}
void handleFlingStart(event) {
double direction = event.velocityX.sign;
double velocityX = event.velocityX.abs() / 1000;
if (velocityX < _kMinFlingVelocity)
return;
double targetPosition = direction < 0.0 ? -_kWidth : 0.0;
double distance = (targetPosition - position.value).abs();
double duration = distance / velocityX;
position.animateTo(targetPosition, duration, curve: linear);
}
}
class Drawer extends AnimatedComponent {
// TODO(abarth): We need a better way to become a container for absolutely
// positioned elements.
static final Style _style = new Style('''
transform: translateX(0);''');
static final Style _maskStyle = new Style('''
background-color: black;
will-change: opacity;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;'''
);
static final Style _contentStyle = new Style('''
background-color: ${Grey[50]};
will-change: transform;
position: absolute;
width: ${_kWidth}px;
top: 0;
left: 0;
bottom: 0;'''
);
List<UINode> children;
int level;
DrawerController controller;
double _position;
Drawer({
Object key,
this.controller,
this.children,
this.level: 0
}) : super(key: key) {
animateField(controller.position, #_position);
}
UINode build() {
bool isClosed = _position <= -_kWidth;
String inlineStyle = 'display: ${isClosed ? 'none' : ''}';
String maskInlineStyle = 'opacity: ${(_position / _kWidth + 1) * 0.5}';
String contentInlineStyle = 'transform: translateX(${_position}px)';
var mask = new EventListenerNode(
new Container(
style: _maskStyle,
inlineStyle: maskInlineStyle
),
onGestureTap: controller.handleMaskTap,
onGestureFlingStart: controller.handleFlingStart
);
Material content = new Material(
content: new Container(
style: _contentStyle,
inlineStyle: contentInlineStyle,
children: children
),
level: level);
return new EventListenerNode(
new Container(
style: _style,
inlineStyle: inlineStyle,
children: [ mask, content ]
),
onPointerDown: controller.handlePointerDown,
onPointerMove: controller.handlePointerMove,
onPointerUp: controller.handlePointerUp,
onPointerCancel: controller.handlePointerCancel
);
}
}