-
Notifications
You must be signed in to change notification settings - Fork 900
/
_collection_list.dart
96 lines (87 loc) · 3.18 KB
/
_collection_list.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
part of '../collection_screen.dart';
@immutable
class _CollectionList extends StatefulWidget with GetItStatefulWidgetMixin {
_CollectionList({
Key? key,
this.onReset,
required this.fromId,
this.scrollKey,
}) : super(key: key);
static const double _vtCardExtent = 300;
static const double _hzCardExtent = 600;
final VoidCallback? onReset;
final Key? scrollKey;
final String fromId;
@override
State<_CollectionList> createState() => _CollectionListState();
}
class _CollectionListState extends State<_CollectionList> with GetItStateMixin {
final ScrollController scrollController = ScrollController();
late final ValueNotifier<bool> _vtMode = ValueNotifier(true)..addListener(_maintainScrollPos);
WonderType? get scrollTargetWonder {
CollectibleData? item;
if (widget.fromId.isEmpty) {
item = collectiblesLogic.getFirstDiscoveredOrNull();
} else {
item = collectiblesLogic.fromId(widget.fromId);
}
return item?.wonder;
}
// Maintain scroll position when switching between vertical and horizontal orientation.
// Multiplies or divides the current scroll position by the ratio of the vertical and horizontal card extents.
void _maintainScrollPos() {
const extentFactor = _CollectionList._vtCardExtent / _CollectionList._hzCardExtent;
final currentPx = scrollController.position.pixels;
if (_vtMode.value == true) {
scrollController.jumpTo(currentPx * extentFactor);
} else {
scrollController.jumpTo(currentPx / extentFactor);
}
}
@override
Widget build(BuildContext context) {
watchX((CollectiblesLogic o) => o.statesById);
List<WonderData> wonders = wondersLogic.all;
_vtMode.value = context.isLandscape == false;
final scrollWonder = scrollTargetWonder;
// Create list of collections that is shared by both hz and vt layouts
List<Widget> collections = [
...wonders.map((d) {
return _CollectionListCard(
key: d.type == scrollWonder ? widget.scrollKey : null,
width: _vtMode.value ? null : _CollectionList._hzCardExtent,
height: _vtMode.value ? _CollectionList._vtCardExtent : 400,
fromId: widget.fromId,
data: d,
);
}).toList()
];
// Scroll view adapts to scroll vertically or horizontally
return SingleChildScrollView(
controller: scrollController,
scrollDirection: _vtMode.value ? Axis.vertical : Axis.horizontal,
child: Padding(
padding: EdgeInsets.all($styles.insets.lg),
child: SeparatedFlex(
direction: _vtMode.value ? Axis.vertical : Axis.horizontal,
mainAxisSize: MainAxisSize.min,
separatorBuilder: () => Gap($styles.insets.lg),
children: [
...collections,
Gap($styles.insets.sm),
if (kDebugMode) _buildResetBtn(context),
],
),
),
);
}
Widget _buildResetBtn(BuildContext context) {
Widget btn = AppBtn.from(
onPressed: widget.onReset ?? () {},
text: $strings.collectionButtonReset,
isSecondary: true,
expand: true,
);
return AnimatedOpacity(opacity: widget.onReset == null ? 0.25 : 1, duration: $styles.times.fast, child: btn);
}
}