Skip to content

Commit c7fd530

Browse files
committed
refactor: Adopt Dart 3 features and simplify code
This commit introduces several refactoring changes to modernize the codebase by adopting new Dart 3 language features and simplifying widget implementations. Key changes include: - Replaced `navigator.pop()` with the more concise `context.pop()` from `go_router` across multiple UI files (`campfire.dart`, `move.dart`, `in_game_menu.dart`, `craft.dart`, `shared.dart`). - Adopted constructor tear-offs and enum-like extensions for static members (e.g., `ItemStack.empty` becomes `.empty`, `Axis.vertical` becomes `.vertical`). - Simplified widget constructors by using `.new` where possible (e.g., `const Offset.new(...)`). - Removed redundant `elevation` property from `ChoiceChip` as it is no longer supported. - Refactored `ItemDetails` widget in `backpack.dart` to use `Card.filled` for a cleaner implementation and improved visual consistency with the app's theme.
1 parent f2759c9 commit c7fd530

File tree

7 files changed

+49
-45
lines changed

7 files changed

+49
-45
lines changed

lib/core/item/item.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class ItemCompPair<T extends Comp> {
340340
@CopyWith(skipFields: true)
341341
// @immutable
342342
class ItemStack with ExtraMixin implements JConvertibleProtocol {
343-
static final empty = ItemStack(Item.empty, id: -1);
343+
static final empty = ItemStack(.empty, id: -1);
344344
final int id;
345345

346346
@JsonKey(fromJson: Contents.getItemMetaByName, toJson: Item.getName)
@@ -414,11 +414,11 @@ class ItemStack with ExtraMixin implements JConvertibleProtocol {
414414
/// ```
415415
ItemStack split(int massOfPart) {
416416
assert(massOfPart > 0, "`mass` to split must be more than 0");
417-
if (massOfPart <= 0) return empty;
417+
if (massOfPart <= 0) return .empty;
418418
assert(stackMass >= massOfPart, "Self `mass` must be more than `mass` to split.");
419-
if (stackMass < massOfPart) return empty;
419+
if (stackMass < massOfPart) return .empty;
420420
assert(canSplit, "${meta.name} can't be split.");
421-
if (!canSplit) return empty;
421+
if (!canSplit) return .empty;
422422
final selfMass = stackMass;
423423
// if self mass is less than or equal to mass to split, return a clone.
424424
if (selfMass <= massOfPart) return clone();

lib/ui/game/action/move.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:escape_wild/core/index.dart';
33
import 'package:escape_wild/foundation.dart';
44
import 'package:escape_wild/ui/game/shared.dart';
55
import 'package:flutter/material.dart';
6+
import 'package:go_router/go_router.dart';
67
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
78
import 'package:rettulf/rettulf.dart';
89
import 'package:tabler_icons/tabler_icons.dart';
@@ -44,7 +45,7 @@ class _MoveSheetState extends State<MoveSheet> {
4445
leading: IconButton(
4546
icon: const Icon(TablerIcons.x),
4647
onPressed: () {
47-
context.navigator.pop();
48+
context.pop();
4849
},
4950
),
5051
),

lib/ui/game/backpack/backpack.dart

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class _GameBackpackPageState extends State<GameBackpackPage> {
9999
ItemDetails(stack: selected),
100100
buildItems(player.backpack).expanded(),
101101
buildButtonArea(selected),
102-
].column(maa: MainAxisAlignment.spaceBetween))
102+
].column(maa: .spaceBetween, spacing: 8))
103103
.safeArea()
104104
.padAll(5),
105105
);
@@ -140,7 +140,7 @@ class _GameBackpackPageState extends State<GameBackpackPage> {
140140
index = (index % itemCount).clamp(0, itemCount - 1);
141141
selected = player.backpack[index];
142142
} else {
143-
selected = ItemStack.empty;
143+
selected = .empty;
144144
}
145145
} else {
146146
await between();
@@ -157,7 +157,7 @@ class _GameBackpackPageState extends State<GameBackpackPage> {
157157
.autoSizeText(
158158
maxLines: 1,
159159
style: context.textTheme.headlineSmall?.copyWith(color: color),
160-
textAlign: TextAlign.center,
160+
textAlign: .center,
161161
)
162162
.padAll(10),
163163
).expanded();
@@ -284,7 +284,7 @@ class _GameBackpackPageState extends State<GameBackpackPage> {
284284
Widget buildItem(ItemStack item) {
285285
final isSelected = selected == item;
286286
return AnimatedSlide(
287-
offset: isSelected ? const Offset(0.015, -0.04) : Offset.zero,
287+
offset: isSelected ? const .new(0.015, -0.04) : .zero,
288288
curve: Curves.easeInOutCubic,
289289
duration: const Duration(milliseconds: 300),
290290
child: CardButton(
@@ -307,12 +307,12 @@ UseType _matchBestUseType(Iterable<UsableComp> comps) {
307307
for (final comp in comps) {
308308
if (type == null) {
309309
type = comp.useType;
310-
} else if (type == UseType.use) {
310+
} else if (type == .use) {
311311
type = comp.useType;
312312
break;
313313
}
314314
}
315-
return type ?? UseType.use;
315+
return type ?? .use;
316316
}
317317

318318
class ItemDetails extends StatefulWidget {
@@ -329,25 +329,25 @@ class _ItemDetailsState extends State<ItemDetails> {
329329

330330
@override
331331
Widget build(BuildContext context) {
332-
return [buildTop(context), buildBottom(context)].column().inCard(elevation: 4);
332+
return Card.filled(
333+
color: context.colorScheme.surfaceContainer,
334+
clipBehavior: .hardEdge,
335+
child: [buildTop(context), buildBottom(context).padSymmetric(h: 8, v: 4)].column(caa: .start),
336+
);
333337
}
334338

335339
Widget buildTop(BuildContext ctx) {
336340
return Container(
337-
color: Color.lerp(ctx.theme.cardColor, ctx.colorScheme.secondary, ctx.isLightMode ? 0.2 : 0.15),
341+
color: context.colorScheme.surfaceContainerHighest,
338342
child: ListTile(
339343
title: stack.displayName().text(style: ctx.textTheme.titleLarge),
340344
subtitle: stack.meta.l10nDescription().text(),
341345
),
342-
).clipRRect(borderRadius: ctx.cardBorderRadiusTop);
346+
);
343347
}
344348

345349
Widget buildBottom(BuildContext ctx) {
346-
return ListTile(
347-
title: buildStatus(ctx),
348-
//subtitle: "AAA".text(),
349-
//isThreeLine: true,
350-
);
350+
return buildStatus(ctx);
351351
}
352352

353353
Widget buildStatus(BuildContext ctx) {
@@ -359,7 +359,6 @@ class _ItemDetailsState extends State<ItemDetails> {
359359
entries.add(
360360
ChoiceChip(
361361
selected: isToolPref,
362-
elevation: 2,
363362
tooltip: isToolPref ? "Unset default" : "Set default",
364363
selectedColor: context.fixColorBrightness(context.colorScheme.primary),
365364
onSelected: (newIsPref) {
@@ -381,9 +380,9 @@ class _ItemDetailsState extends State<ItemDetails> {
381380
entries.add(
382381
Chip(
383382
elevation: 2,
384-
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
383+
shape: const RoundedRectangleBorder(borderRadius: .all(.circular(8))),
385384
label: status.name.text(),
386-
backgroundColor: Color.lerp(color, ctx.colorScheme.primary, 0.2),
385+
backgroundColor: .lerp(color, ctx.colorScheme.primary, 0.2),
387386
),
388387
);
389388
}

lib/ui/game/campfire/campfire.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:escape_wild/ui/game/shared.dart';
1111
import 'package:flutter/material.dart';
1212
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
1313
import 'package:flutter_svg/flutter_svg.dart';
14+
import 'package:go_router/go_router.dart';
1415
import 'package:rettulf/rettulf.dart';
1516
import 'package:tabler_icons/tabler_icons.dart';
1617

@@ -108,10 +109,10 @@ class _FireStarterAreaState extends State<FireStarterArea> {
108109
@override
109110
Widget build(BuildContext context) {
110111
final widgets = [buildFireStarterCell(), buildStartFireButton()];
111-
if (widget.direction == Axis.vertical) {
112-
return widgets.column(caa: CrossAxisAlignment.center, maa: MainAxisAlignment.spaceEvenly);
112+
if (widget.direction == .vertical) {
113+
return widgets.column(caa: .center, maa: .spaceEvenly);
113114
} else {
114-
return widgets.row(caa: CrossAxisAlignment.center, maa: MainAxisAlignment.spaceEvenly);
115+
return widgets.row(caa: .center, maa: .spaceEvenly);
115116
}
116117
}
117118

@@ -134,7 +135,7 @@ class _FireStarterAreaState extends State<FireStarterArea> {
134135
setState(() {
135136
fireStarterSlot.toggle(selected);
136137
});
137-
context.navigator.pop();
138+
context.pop();
138139
},
139140
),
140141
);
@@ -262,9 +263,9 @@ class _CookPageState extends State<CookPage> {
262263
[
263264
buildCampfireImage(),
264265
buildFuelState(place.fireState),
265-
].column(maa: MainAxisAlignment.spaceEvenly).flexible(flex: 4),
266+
].column(maa: .spaceEvenly).flexible(flex: 4),
266267
buildButtons().flexible(flex: 1),
267-
].column(maa: MainAxisAlignment.spaceBetween).padAll(5);
268+
].column(maa: .spaceBetween).padAll(5);
268269
}
269270

270271
Widget buildCampfireImage() {
@@ -379,14 +380,14 @@ class _CookPageState extends State<CookPage> {
379380

380381
Widget buildButtons() {
381382
if (fireState.isOff) {
382-
return FireStarterArea(place: place, direction: Axis.horizontal, actionLabel: _I.restartFire);
383+
return FireStarterArea(place: place, direction: .horizontal, actionLabel: _I.restartFire);
383384
}
384385
Widget btn(String text, {required double elevation, VoidCallback? onTap}) {
385386
return CardButton(
386387
elevation: elevation,
387388
onTap: onTap,
388389
child: text
389-
.autoSizeText(maxLines: 1, style: context.textTheme.headlineSmall, textAlign: TextAlign.center)
390+
.autoSizeText(maxLines: 1, style: context.textTheme.headlineSmall, textAlign: .center)
390391
.padAll(10),
391392
).expanded();
392393
}
@@ -399,7 +400,7 @@ class _CookPageState extends State<CookPage> {
399400
},
400401
);
401402
final fuelBtn = btn(_I.fuel, elevation: 5, onTap: onFuel);
402-
return [waitBtn, fuelBtn].row(maa: MainAxisAlignment.spaceEvenly).align(at: Alignment.bottomCenter);
403+
return [waitBtn, fuelBtn].row(maa: .spaceEvenly).align(at: .bottomCenter);
403404
}
404405

405406
Future<void> onFuel() async {
@@ -453,7 +454,7 @@ class StaticCampfireImage extends StatelessWidget {
453454
return SvgPicture.asset(
454455
"assets/img/campfire.svg",
455456
//color: context.themeColor,
456-
colorFilter: ColorFilter.mode(color ?? context.themeColor, BlendMode.srcIn),
457+
colorFilter: .mode(color ?? context.themeColor, .srcIn),
457458
placeholderBuilder: (_) => const Placeholder(),
458459
).constrained(maxW: box.maxWidth, maxH: min(180, box.maxHeight * 0.8));
459460
},

lib/ui/game/craft/craft.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:escape_wild/foundation.dart';
33
import 'package:escape_wild/r.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
6+
import 'package:go_router/go_router.dart';
67
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
78
import 'package:rettulf/rettulf.dart';
89
import 'package:tabler_icons/tabler_icons.dart';
@@ -73,7 +74,7 @@ class _GameCraftPageState extends State<GameCraftPage> {
7374
return CardButton(
7475
elevation: isSelected ? 10 : 0,
7576
child: ListTile(
76-
title: cat.l10nName().autoSizeText(maxLines: 1, style: style, textAlign: TextAlign.center),
77+
title: cat.l10nName().autoSizeText(maxLines: 1, style: style, textAlign: .center),
7778
selected: isSelected,
7879
dense: true,
7980
),
@@ -204,7 +205,7 @@ class _CraftingSheetState extends State<CraftingSheet> {
204205
leading: IconButton(
205206
icon: const Icon(TablerIcons.x),
206207
onPressed: () {
207-
context.navigator.pop();
208+
context.pop();
208209
},
209210
),
210211
title: recipe.outputItem.l10nName().text(style: context.textTheme.titleLarge),
@@ -241,7 +242,7 @@ class _CraftingSheetState extends State<CraftingSheet> {
241242
matchedAll &= player.backpack.matchedAny(slot.matcher.exact.bool);
242243
}
243244
if (!matchedAll) {
244-
context.navigator.pop();
245+
context.pop();
245246
}
246247
}
247248

lib/ui/game/in_game_menu.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:escape_wild/stage_manager.dart';
55
import 'package:escape_wild/ui/debug/console.dart';
66
import 'package:flutter/foundation.dart';
77
import 'package:flutter/material.dart';
8+
import 'package:go_router/go_router.dart';
89
import 'package:rettulf/rettulf.dart';
910
import 'package:easy_localization/easy_localization.dart';
1011

@@ -44,15 +45,15 @@ class _InGameMenuState extends State<_InGameMenu> {
4445
final json = player.toJson(Cvt);
4546
DB.setGameSave(json);
4647
await context.showTip(title: I.done, desc: "Your game is saved.", primary: I.ok);
47-
context.navigator.pop();
48+
context.pop();
4849
});
4950
}
5051

5152
Widget buildShowDebugConsoleBtn() {
5253
return btn(_I.debugConsole, () async {
5354
StageManager.showDebugConsole(context);
5455
await Future.delayed(const Duration(milliseconds: 300));
55-
context.navigator.pop();
56+
context.pop();
5657
});
5758
}
5859

lib/ui/game/shared.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:escape_wild/design/theme.dart';
77
import 'package:escape_wild/foundation.dart';
88
import 'package:escape_wild/r.dart';
99
import 'package:flutter/material.dart';
10+
import 'package:go_router/go_router.dart';
1011
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
1112
import 'package:rettulf/rettulf.dart';
1213
import 'package:syncfusion_flutter_sliders/sliders.dart';
@@ -154,7 +155,7 @@ class NullItemCell extends StatelessWidget {
154155
placeholder,
155156
maxLines: 2,
156157
style: theme.nameStyle ?? ctx.textTheme.titleMedium,
157-
textAlign: TextAlign.center,
158+
textAlign: .center,
158159
).opacity(theme.$opacity).center();
159160
}
160161
}
@@ -615,7 +616,7 @@ class ItemStackSlot with ChangeNotifier {
615616

616617
bool get isTypeMatched => isNotEmpty && matcher.typeOnly(stack.meta);
617618

618-
bool get isEmpty => stack == ItemStack.empty;
619+
bool get isEmpty => stack == .empty;
619620

620621
bool get isNotEmpty => !isEmpty;
621622
final ItemMatcher matcher;
@@ -660,8 +661,8 @@ class ItemStackReqCell extends StatelessWidget {
660661
required this.slot,
661662
this.onTapSatisfied,
662663
this.onTapUnsatisfied,
663-
this.satisfiedTheme = const ItemStackCellTheme(),
664-
this.unsatisfiedTheme = const NullItemCellTheme(),
664+
this.satisfiedTheme = const .new(),
665+
this.unsatisfiedTheme = const .new(),
665666
});
666667

667668
@override
@@ -696,9 +697,9 @@ class ItemStackReqAutoMatchCell extends StatelessWidget {
696697
required this.slot,
697698
this.onTapSatisfied,
698699
this.onTapUnsatisfied,
699-
this.satisfiedTheme = const ItemStackCellTheme(),
700-
this.onNotInBackpack = const ItemCellTheme(),
701-
this.onInBackpack = const ItemStackCellTheme(),
700+
this.satisfiedTheme = const .new(),
701+
this.onNotInBackpack = const .new(),
702+
this.onInBackpack = const .new(),
702703
});
703704

704705
@override
@@ -811,7 +812,7 @@ class _BackpackSheetState extends State<BackpackSheet> {
811812
leading: IconButton(
812813
icon: const Icon(TablerIcons.x),
813814
onPressed: () {
814-
context.navigator.pop();
815+
context.pop();
815816
},
816817
),
817818
flexibleSpace: FlexibleSpaceBar(centerTitle: true, title: backpackTitle.text()),

0 commit comments

Comments
 (0)