-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.dart
281 lines (257 loc) · 10.5 KB
/
main.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
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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:timeline_editor/timeline_editor.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
Duration totalDuration = Duration(seconds: 600);
Duration box1Start = Duration(seconds: 30);
Duration box1Duration = Duration(seconds: 50);
bool box1Selected = false;
Duration box2Start = Duration(seconds: 100);
Duration box2Duration = Duration(seconds: 180);
bool box2Selected = false;
Duration box1bStart = Duration(seconds: 30);
Duration box1bDuration = Duration(seconds: 50);
bool box1bSelected = false;
Duration box2bStart = Duration(seconds: 100);
Duration box2bDuration = Duration(seconds: 180);
bool box2bSelected = false;
bool deleted = false;
double position = 0;
bool customTimeString = false;
bool withHeaders = false;
StreamController<double> positionController;
Timer progressTimer;
TimelineEditorScaleController scaleController;
double scale;
double _trackHeight = 100;
AnimationController _controller;
Animation<double> _animation;
void moveBox1(Duration newStart) {
if (box1Start + newStart < Duration.zero) {
newStart = Duration.zero;
}
if (box1Start + newStart + box1Duration < box2Start)
setState(() {
box1Start += newStart;
});
}
void moveBox1End(Duration move) {
if (box1Start + box1Duration + move < box2Start)
setState(() => box1Duration = box1Duration + move);
}
void moveBox2(Duration startMove) {
var newStart = box2Start + startMove;
if (box1Start + box1Duration < newStart &&
newStart + box2Duration < totalDuration)
setState(() {
box2Start = newStart;
});
}
void moveBox2End(Duration move) {
if (box2Start + box2Duration + move < totalDuration)
setState(() => box2Duration = box2Duration + move);
}
// 2nd track
void moveBox1b(Duration newStart) {
if (box1bStart + newStart < Duration.zero) {
newStart = Duration.zero;
}
if (box1bStart + newStart + box1bDuration < box2bStart)
setState(() {
box1bStart += newStart;
});
}
void moveBox1bEnd(Duration move) {
if (box1bStart + box1bDuration + move < box2bStart)
setState(() => box1bDuration = box1bDuration + move);
}
void moveBox2b(Duration startMove) {
var newStart = box2bStart + startMove;
if (box1Start + box1bDuration < newStart &&
newStart + box2bDuration < totalDuration)
setState(() {
box2bStart = newStart;
});
}
void moveBox2bEnd(Duration move) {
if (box2bStart + box2bDuration + move < totalDuration)
setState(() => box2bDuration = box2bDuration + move);
}
void positionUpdate(Timer timer) {
position += 0.350;
if (position > 300) position = 0;
positionController.add(position);
/* if (position + 0.1 < 300)
Timer(Duration(milliseconds: 100), () => positionUpdate());*/
}
@override
void initState() {
super.initState();
scaleController = TimelineEditorScaleController();
positionController = StreamController<double>.broadcast();
progressTimer = Timer.periodic(Duration(milliseconds: 350), positionUpdate);
_controller =
AnimationController(vsync: this, duration: Duration(seconds: 2));
_animation = Tween(begin: 100.0, end: 200.0).animate(_controller)
..addListener(() {
setState(() => _trackHeight = _animation.value);
});
positionUpdate(null);
}
@override
void dispose() {
scaleController.dispose();
progressTimer?.cancel();
positionController?.close();
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
darkTheme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('Timeline_editor example app'),
),
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Timeline_editor example app'),
StreamBuilder<double>(
stream: scaleController.scaleUpdates,
initialData: 1,
builder: (context, snapshot) {
return Text('Current scale: ' + snapshot.data.toString());
}),
RaisedButton(
child: const Text('Change track height'),
onPressed: () =>
_controller.status == AnimationStatus.forward ||
_controller.status == AnimationStatus.completed
? _controller.reverse()
: _controller.forward(),
),
SwitchListTile(
title: Text('Custom time string'),
value: customTimeString,
onChanged: (value) =>
setState(() => customTimeString = value)),
SwitchListTile(
title: Text('Headers'),
value: withHeaders,
onChanged: (value) => setState(() => withHeaders = value)),
],
))),
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: TimelineEditor(
scaleController: scaleController,
minimumTimeWidgetExtent: customTimeString ? 100 : null,
leadingWidgetBuilder: withHeaders
? (index) => Center(
child: RaisedButton(
onPressed: () {}, child: Text("$index")))
: null,
timelineLeadingWidget:
withHeaders ? Center(child: Text("HEADER")) : null,
timeWidgetBuilder: customTimeString
? (d, t) => Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
'${d.inSeconds}/${t.inSeconds}',
overflow: TextOverflow.ellipsis,
),
)
: null,
onPositionTap: (s) => position = s,
positionStream: positionController.stream,
countTracks: 2,
trackBuilder: (track, pps, duration, scrollControllers) =>
track == 0
? TimelineEditorTrack(
// key: Key('separated'),
scrollControllers: scrollControllers,
defaultColor: Colors.green[700],
boxes: [
TimelineEditorCard(
box1Start,
duration: box1Duration,
selected: box1Selected,
onTap: () => setState(
() => box1Selected = !box1Selected),
color: Colors.blue,
onMovedDuration: moveBox1End,
onMovedStart: moveBox1,
),
TimelineEditorCard(box2Start,
duration: box2Duration,
menuEntries: [
PopupMenuItem<String>(
child: Text('Demo!'), value: 'demo')
],
onSelectedMenuItem: (v) {
print(v);
},
onMovedDuration: moveBox2End,
onMovedStart: moveBox2,
selected: box2Selected,
onTap: () => setState(
() => box2Selected = !box2Selected),
color: Colors.green),
],
pixelsPerSeconds: pps,
duration: duration,
)
: TimelineEditorTrack(
// key: Key('separated'),
scrollControllers: scrollControllers,
defaultColor: Colors.green[700],
boxes: [
TimelineEditorCard(
box1Start,
duration: box1bDuration,
selected: box1bSelected,
onTap: () => setState(
() => box1bSelected = !box1bSelected),
color: Colors.red,
onMovedDuration: moveBox1bEnd,
onMovedStart: moveBox1b,
),
TimelineEditorCard(box2bStart,
duration: box2bDuration,
borderColor: Colors.yellow,
menuEntries: [
PopupMenuItem<String>(
child: Text('Demo 2!'), value: 'demo')
],
onSelectedMenuItem: (v) {
print(v);
},
onMovedDuration: moveBox2bEnd,
onMovedStart: moveBox2b,
selected: box2bSelected,
onTap: () => setState(
() => box2bSelected = !box2bSelected),
color: Colors.deepOrange),
],
pixelsPerSeconds: pps,
duration: duration,
),
duration: totalDuration,
)),
],
),
),
);
}
}