/
main.dart
310 lines (281 loc) 路 9.85 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
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
// ignore_for_file: prefer_function_declarations_over_variables
import 'dart:io';
import 'dart:math';
import 'package:betrayal/betrayal.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Betrayal Demo',
theme: ThemeData.from(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(255, 199, 184, 116),
brightness: Brightness.light)),
darkTheme: ThemeData.from(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(255, 199, 184, 116),
brightness: Brightness.dark)),
themeMode: ThemeMode.system,
home: const Scaffold(
body: ElementSelector(
axis: Axis.vertical,
)),
);
}
}
class Data {
Data(this.widget, this.key);
final Function(BuildContext) widget;
final Key key;
}
class ElementSelector extends StatefulWidget {
const ElementSelector(
{Key? key, this.dimension = 150, this.axis = Axis.vertical})
: super(key: key);
final double dimension;
final Axis axis;
@override
State<ElementSelector> createState() => _ElementSelectorState();
}
class _ElementSelectorState extends State<ElementSelector>
with TickerProviderStateMixin {
late final AnimationController _animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 400),
);
late final Animation<double> _animation = CurvedAnimation(
parent: _animationController,
curve: Curves.easeOutBack,
);
late final TrayIcon _icon = TrayIcon(const TrayIconData());
@override
void dispose() {
_animationController.dispose();
_icon.dispose();
super.dispose();
}
final _items = [
Data((_) => const FlutterLogo(), GlobalKey()),
Data((_) => const FlutterLogo(), GlobalKey()),
Data((_) => const FlutterLogo(), GlobalKey()),
];
int _currentPage = 0;
late PageController _controller;
@override
Widget build(BuildContext context) {
_controller = PageController(
initialPage: _currentPage,
viewportFraction: widget.dimension /
(widget.axis == Axis.vertical
? MediaQuery.of(context).size.height
: MediaQuery.of(context).size.width));
return PageView.builder(
allowImplicitScrolling: true,
scrollDirection: widget.axis,
itemCount: _items.length + 1,
onPageChanged: (pageIndex) {
setState(() {
_currentPage = pageIndex;
});
_icon.setTooltip(_items[_currentPage].key.toString());
_icon.setIcon();
_icon.show();
},
itemBuilder: (BuildContext context, int index) {
final animateHere = () {
_controller.animateToPage(index,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut);
};
final isSelected = index == _currentPage;
Widget res;
if (index == _items.length) {
res = IconButton(
tooltip: "Add new Image",
iconSize: widget.dimension * 0.5,
icon: Icon(Icons.add,
color: Theme.of(context).colorScheme.onBackground),
onPressed: () async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: const ["ico", "png"]);
if (result == null) return;
final path = result.files.first.path!;
await _animationController.reverse();
setState(() {
_items.add(Data((_) => Image.file(File(path)), GlobalKey()));
_animationController.reset();
});
},
);
} else {
final item = _items[index];
res = Selectable(
axis: widget.axis,
key: item.key,
dimension: widget.dimension,
onTap: animateHere,
onRemove: () async {
await _animationController.forward();
setState(() {
_items.removeAt(index);
_animationController.reset();
});
},
isSelected: isSelected,
child: Padding(
padding: const EdgeInsets.all(8),
child: item.widget(context)));
}
final double slideFactor = index >= _currentPage ? -1 : 0;
return SlideTransition(
position: _animation.drive(Tween(
begin: Offset.zero,
end: Offset(
widget.axis == Axis.horizontal ? slideFactor : 0,
widget.axis == Axis.vertical ? slideFactor : 0,
))),
child: res);
},
scrollBehavior: ScrollConfiguration.of(context).copyWith(dragDevices: {
PointerDeviceKind.mouse,
PointerDeviceKind.stylus,
PointerDeviceKind.touch
}, scrollbars: false),
physics: const PageScrollPhysics(parent: BouncingScrollPhysics()),
controller: _controller,
);
}
}
class Selectable extends StatefulWidget {
const Selectable(
{Key? key,
this.isSelected = false,
required this.dimension,
this.gap = 8.0,
this.axis = Axis.vertical,
this.onTap,
this.child,
this.onRemove,
this.timeToRemove = const Duration(milliseconds: 200)})
: super(key: key);
final bool isSelected;
final Axis axis;
final Widget? child;
final double gap;
final double dimension;
final Function()? onTap;
final Function()? onRemove;
final Duration timeToRemove;
@override
State<Selectable> createState() => _SelectableState();
}
class _SelectableState extends State<Selectable> {
final iconInsetFactor = 0.7;
late bool _isSelected = widget.isSelected;
bool _removing = false;
@override
void didUpdateWidget(Selectable oldWidget) {
_isSelected = widget.isSelected;
super.didUpdateWidget(oldWidget);
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final transparent = theme.colorScheme.background.withOpacity(0.0);
return Padding(
padding: EdgeInsets.symmetric(
vertical: widget.axis == Axis.vertical ? widget.gap : 0,
horizontal: widget.axis == Axis.horizontal ? widget.gap : 0),
child: Stack(
alignment: Alignment.center,
children: [
SizedBox.square(
dimension: widget.dimension,
child: Material(
clipBehavior: Clip.antiAlias,
type: MaterialType.circle,
color: transparent,
child: InkWell(
hoverColor: _removing ? transparent : null,
highlightColor: _removing ? transparent : null,
onTap: widget.onTap,
onFocusChange: (hasFocus) =>
hasFocus ? widget.onTap?.call() : null,
autofocus: true,
canRequestFocus: true,
focusColor: transparent,
child: AnimatedContainer(
duration: _removing
? widget.timeToRemove
: const Duration(milliseconds: 800),
curve: Curves.ease,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _isSelected
? Color.lerp(theme.colorScheme.background,
theme.colorScheme.outline, 0.15)
: transparent),
foregroundDecoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: _isSelected ? 2.0 : 0,
color: _isSelected
? theme.colorScheme.outline
: transparent)),
child: Padding(
padding: EdgeInsets.all(sqrt(widget.dimension)),
child: AnimatedOpacity(
opacity: _removing ? 0.0 : 1.0,
duration: widget.timeToRemove,
child: widget.child),
)),
),
),
),
if (_isSelected && widget.onRemove != null)
Positioned.fill(
left: (widget.dimension -
(widget.axis == Axis.horizontal ? widget.gap * 2 : 0)) *
iconInsetFactor,
bottom: (widget.dimension -
(widget.axis == Axis.vertical ? widget.gap * 2 : 0)) *
iconInsetFactor,
child: Transform.scale(
scale: .6,
child: FloatingActionButton(
tooltip: "Remove",
elevation: 0,
focusElevation: 0,
highlightElevation: 0,
disabledElevation: 0,
hoverElevation: 0,
onPressed: () async {
setState(() {
_isSelected = false;
_removing = true;
});
Focus.maybeOf(context)?.unfocus();
await Future.delayed(widget.timeToRemove);
widget.onRemove!();
},
child: Transform.rotate(
angle: pi / 4,
child: const Icon(
Icons.add,
size: 36,
)),
),
)),
],
),
);
}
}