wonders/lib/ui/screens/collection/collection_screen.dart

84 lines
2.8 KiB
Dart
Raw Normal View History

2022-08-29 20:38:28 -06:00
import 'dart:async';
2022-12-22 13:37:22 -07:00
import 'package:flutter/foundation.dart';
2022-08-29 20:38:28 -06:00
import 'package:wonders/common_libs.dart';
import 'package:wonders/logic/collectibles_logic.dart';
import 'package:wonders/logic/data/collectible_data.dart';
import 'package:wonders/logic/data/wonder_data.dart';
import 'package:wonders/ui/common/centered_box.dart';
2022-12-20 10:23:20 -07:00
import 'package:wonders/ui/common/controls/app_header.dart';
2022-08-29 20:38:28 -06:00
import 'package:wonders/ui/common/modals/app_modals.dart';
2022-11-08 11:15:39 -07:00
part 'widgets/_collectible_image.dart';
2022-11-14 09:14:08 -07:00
part 'widgets/_collection_footer.dart';
2022-08-29 20:38:28 -06:00
part 'widgets/_collection_list.dart';
2022-11-08 11:15:39 -07:00
part 'widgets/_collection_list_card.dart';
2022-11-14 09:14:08 -07:00
part 'widgets/_newly_discovered_items_btn.dart';
2022-08-29 20:38:28 -06:00
class CollectionScreen extends StatefulWidget with GetItStatefulWidgetMixin {
CollectionScreen({required this.fromId, Key? key}) : super(key: key);
final String fromId;
@override
State<CollectionScreen> createState() => _CollectionScreenState();
}
class _CollectionScreenState extends State<CollectionScreen> with GetItStateMixin {
2022-11-08 11:15:39 -07:00
final GlobalKey _scrollKey = GlobalKey();
2022-08-29 20:38:28 -06:00
@override
void initState() {
super.initState();
2022-11-14 09:14:08 -07:00
final states = collectiblesLogic.statesById.value;
if (widget.fromId.isNotEmpty && states[widget.fromId] == CollectibleState.discovered) {
2022-08-29 20:38:28 -06:00
scheduleMicrotask(() => _scrollToTarget(false));
}
}
void _scrollToTarget([bool animate = true]) {
2022-11-08 11:15:39 -07:00
if (_scrollKey.currentContext != null) {
Scrollable.ensureVisible(_scrollKey.currentContext!, alignment: 0.15, duration: animate ? 300.ms : 0.ms);
2022-08-29 20:38:28 -06:00
}
}
void _handleReset() async {
String msg = $strings.collectionPopupResetConfirm;
final result = await showModal(context, child: OkCancelModal(msg: msg));
if (result == true) {
collectiblesLogic.reset();
}
}
@override
Widget build(BuildContext context) {
2022-11-14 09:14:08 -07:00
// Rebuild when collectible states change
watchX((CollectiblesLogic o) => o.statesById);
int discovered = collectiblesLogic.discoveredCount;
int explored = collectiblesLogic.exploredCount;
int total = collectiblesLogic.all.length;
2022-08-29 20:38:28 -06:00
return ColoredBox(
color: $styles.colors.greyStrong,
2022-10-27 10:24:28 -06:00
child: Column(
children: [
Expanded(
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
2022-12-20 10:23:20 -07:00
AppHeader(title: $strings.collectionTitleCollection),
2022-11-08 11:15:39 -07:00
_NewlyDiscoveredItemsBtn(count: discovered, onPressed: _scrollToTarget),
2022-10-27 10:24:28 -06:00
Flexible(
child: _CollectionList(
fromId: widget.fromId,
scrollKey: _scrollKey,
onReset: discovered + explored > 0 ? _handleReset : null,
),
),
]),
),
_CollectionFooter(count: discovered + explored, total: total),
],
),
2022-08-29 20:38:28 -06:00
);
}
}