Refactor display to separate widget
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:gamer_updater/db.dart';
|
||||
import 'package:gamer_updater/game.dart';
|
||||
import 'package:gamer_updater/widgets/new_game_card.dart';
|
||||
|
||||
void main() async {
|
||||
await DB.init();
|
||||
@@ -86,8 +87,18 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
crossAxisSpacing: 4,
|
||||
mainAxisSpacing: 4,
|
||||
),
|
||||
itemCount: games.length,
|
||||
itemCount: games.length + 1, // +1 for the new game card
|
||||
itemBuilder: (context, index) {
|
||||
if (index == games.length) {
|
||||
return NewGameCard(
|
||||
onGameCreated: (game) async {
|
||||
game = await GameRepository.upsert(game);
|
||||
setState(() {
|
||||
games.add(game);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
return GameCard(
|
||||
game: games[index],
|
||||
onGameUpdated: (game) async {
|
||||
@@ -96,6 +107,12 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
games[index] = game;
|
||||
});
|
||||
},
|
||||
onDelete: () async {
|
||||
await GameRepository.delete(games[index]);
|
||||
setState(() {
|
||||
games.removeAt(index);
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -107,8 +124,14 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
class GameCard extends StatefulWidget {
|
||||
final Game game;
|
||||
final Function(Game) onGameUpdated;
|
||||
final VoidCallback onDelete;
|
||||
|
||||
const GameCard({super.key, required this.game, required this.onGameUpdated});
|
||||
const GameCard({
|
||||
super.key,
|
||||
required this.game,
|
||||
required this.onGameUpdated,
|
||||
required this.onDelete,
|
||||
});
|
||||
|
||||
@override
|
||||
State<GameCard> createState() => _GameCardState();
|
||||
|
Reference in New Issue
Block a user