forked from flutter/assets-for-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_scroll_view.dart
169 lines (151 loc) · 5.03 KB
/
custom_scroll_view.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
// 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 'package:flutter_test/flutter_test.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;
const String _customScrollView = 'custom_scroll_view';
class CustomScrollViewDiagram extends StatefulWidget
implements DiagramMetadata {
const CustomScrollViewDiagram(this.name, {Key? key}) : super(key: key);
@override
final String name;
@override
State<StatefulWidget> createState() => _CustomScrollViewDiagramState();
}
class _CustomScrollViewDiagramState extends State<CustomScrollViewDiagram>
with TickerProviderStateMixin<CustomScrollViewDiagram> {
final ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
SchedulerBinding.instance.scheduleFrameCallback((Duration _) {
_play();
});
}
@override
void didUpdateWidget(CustomScrollViewDiagram oldWidget) {
super.didUpdateWidget(oldWidget);
_scrollController.jumpTo(0.0);
SchedulerBinding.instance.scheduleFrameCallback((Duration _) {
_play();
});
}
Future<void> _play() async {
await Future<void>.delayed(_kScrollPauseDuration);
await _animate(
to: 650.0,
duration: _kScrollUpDuration,
);
await Future<void>.delayed(_kScrollPauseDuration);
await _animate(
to: 500.0,
duration: _kScrollDownDurationPartOne,
);
await Future<void>.delayed(_kScrollPauseDuration);
await _animate(
to: 0.0,
duration: _kScrollDownDurationPartTwo,
);
}
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>[
const SliverAppBar(
pinned: true,
expandedHeight: 250.0,
flexibleSpace: FlexibleSpaceBar(
title: Text('Demo'),
),
),
SliverGrid(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200.0,
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
childAspectRatio: 4.0,
),
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
alignment: Alignment.center,
color: Colors.teal[100 * (index % 9)],
child: Text('Grid Item $index'),
);
},
childCount: 20,
),
),
SliverFixedExtentList(
itemExtent: 50.0,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
alignment: Alignment.center,
color: Colors.lightBlue[100 * (index % 9)],
child: Text('List Item $index'),
);
},
),
),
],
),
);
}
}
class CustomScrollViewDiagramStep extends DiagramStep<CustomScrollViewDiagram> {
CustomScrollViewDiagramStep(DiagramController controller) : super(controller);
@override
final String category = 'widgets';
@override
Future<List<CustomScrollViewDiagram>> get diagrams async =>
<CustomScrollViewDiagram>[
const CustomScrollViewDiagram(_customScrollView),
];
@override
Future<File> generateDiagram(CustomScrollViewDiagram diagram) async {
controller.builder = (BuildContext context) => diagram;
return controller.drawAnimatedDiagramToFiles(
end: _kTotalDuration,
frameRate: _kCurveAnimationFrameRate,
name: diagram.name,
category: category,
);
}
}