forked from flutter/assets-for-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliver_app_bars.dart
198 lines (178 loc) · 5.3 KB
/
sliver_app_bars.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
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
// Copyright 2013 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:async';
import 'dart:io';
import 'package:diagram_capture/diagram_capture.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'diagram_step.dart';
import 'fake_drag_scroll_activity.dart';
final Duration _kTotalDuration = _kScrollUpDuration +
_kScrollPauseDuration +
_kScrollDownDurationPartOne +
_kScrollPauseDuration +
_kScrollDownDurationPartTwo +
_kScrollPauseDuration;
const Duration _kScrollUpDuration = Duration(seconds: 1, milliseconds: 500);
const Duration _kScrollDownDurationPartOne = Duration(milliseconds: 800);
const Duration _kScrollDownDurationPartTwo = Duration(seconds: 1);
const Duration _kScrollPauseDuration = Duration(milliseconds: 900);
const double _kCurveAnimationFrameRate = 60.0;
class SliverAppBarDiagram extends StatefulWidget implements DiagramMetadata {
const SliverAppBarDiagram({
this.pinned = false,
this.floating = false,
this.snap = false,
this.repeatAnimation = false,
Key? key,
}) : super(key: key);
final bool pinned;
final bool floating;
final bool snap;
final bool repeatAnimation;
@override
State<SliverAppBarDiagram> createState() => SliverAppBarDiagramState();
@override
String get name {
String name = 'app_bar';
if (pinned) {
name += '_pinned';
}
if (floating) {
name += '_floating';
}
if (snap) {
name += '_snap';
}
return name;
}
}
class SliverAppBarDiagramState extends State<SliverAppBarDiagram>
with TickerProviderStateMixin<SliverAppBarDiagram> {
final ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
SchedulerBinding.instance!.scheduleFrameCallback((Duration _) {
_play();
});
}
@override
void didUpdateWidget(SliverAppBarDiagram oldWidget) {
super.didUpdateWidget(oldWidget);
_scrollController.jumpTo(0.0);
SchedulerBinding.instance!.scheduleFrameCallback((Duration _) {
_play();
});
}
Future<void> _play() async {
await Future<void>.delayed(_kScrollPauseDuration);
if (!mounted) {
return;
}
await _animate(
to: 600.0,
duration: _kScrollUpDuration,
);
await Future<void>.delayed(_kScrollPauseDuration);
if (!mounted) {
return;
}
await _animate(
to: 490.0,
duration: _kScrollDownDurationPartOne,
);
await Future<void>.delayed(_kScrollPauseDuration);
if (!mounted) {
return;
}
await _animate(
to: 0.0,
duration: _kScrollDownDurationPartTwo,
);
if (widget.repeatAnimation && mounted) {
_play();
}
}
Future<void> _animate({required double to, required Duration duration}) {
final ScrollPositionWithSingleContext position =
_scrollController.position as ScrollPositionWithSingleContext;
final FakeDragScrollActivity activity = FakeDragScrollActivity(
position,
from: _scrollController.offset,
to: to,
duration: duration,
curve: Curves.easeInOut,
vsync: this,
);
_scrollController.position.beginActivity(activity);
return activity.done;
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
height: 400.0,
width: 376.0,
child: CustomScrollView(
controller: _scrollController,
slivers: <Widget>[
SliverAppBar(
primary: false,
pinned: widget.pinned,
snap: widget.snap,
floating: widget.floating,
title: const Text('App Bar'),
expandedHeight: 180.0,
leading: const Icon(Icons.menu),
),
SliverList(
delegate:
SliverChildListDelegate(List<Widget>.generate(20, (int i) {
return Container(
color: i.isEven ? Colors.white : Colors.black12,
height: 100.0,
child: Center(
child: Text('$i', textScaleFactor: 5),
),
);
})),
)
],
),
);
}
}
class SliverAppBarDiagramStep extends DiagramStep<SliverAppBarDiagram> {
SliverAppBarDiagramStep(DiagramController controller) : super(controller) {
for (final bool pinned in <bool>[false, true]) {
for (final bool floating in <bool>[false, true]) {
// snap is only a legal option if floating is true.
for (final bool snap
in floating ? <bool>[false, true] : <bool>[false]) {
_diagrams.add(SliverAppBarDiagram(
pinned: pinned,
floating: floating,
snap: snap,
));
}
}
}
}
@override
final String category = 'material';
final List<SliverAppBarDiagram> _diagrams = <SliverAppBarDiagram>[];
@override
Future<List<SliverAppBarDiagram>> get diagrams async => _diagrams;
@override
Future<File> generateDiagram(SliverAppBarDiagram diagram) async {
controller.builder = (BuildContext context) => diagram;
return controller.drawAnimatedDiagramToFiles(
end: _kTotalDuration,
frameRate: _kCurveAnimationFrameRate,
name: diagram.name,
category: category,
);
}
}