Files
flutter-gamer-updater/lib/main.dart

135 lines
4.0 KiB
Dart

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';
import 'package:gamer_updater/widgets/game_card.dart';
void main() async {
await DB.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Gamer Updater',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
useMaterial3: true,
scaffoldBackgroundColor: Colors.black,
colorSchemeSeed: Colors.deepPurple,
cardTheme: CardTheme(
color: Colors.grey[900],
elevation: 4,
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
),
textTheme: const TextTheme(
titleLarge: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
bodyLarge: TextStyle(fontSize: 18),
bodyMedium: TextStyle(fontSize: 16),
bodySmall: TextStyle(fontSize: 14),
),
),
themeMode: ThemeMode.system,
home: const MyHomePage(title: 'Gamer Updater'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late Map<String, Game> games = {};
@override
void initState() {
super.initState();
_refreshGames();
}
Future<void> _refreshGames() async {
final games = await GameRepository.getAll();
setState(() {
this.games = games;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
actions: [
IconButton(icon: const Icon(Icons.refresh), onPressed: _refreshGames),
],
),
body: RefreshIndicator(
onRefresh: _refreshGames,
child: LayoutBuilder(
builder: (context, constraints) {
final cardWidth = constraints.maxWidth / 2;
final cardHeight = (cardWidth * 215) / 400; // Maintain aspect ratio
return SingleChildScrollView(
padding: const EdgeInsets.all(8),
child: Wrap(
spacing: 8,
runSpacing: 8,
children: [
...games.values.map(
(game) => SizedBox(
width: cardWidth - 10, // Subtract spacing to fit 3 cards
height: cardHeight - 10,
child: GameCard(
game: game,
onGameUpdated: (game) async {
game = await GameRepository.upsert(game);
setState(() {
games[game.name] = game;
});
},
onDelete: () async {
await GameRepository.delete(game);
setState(() {
games.remove(game.name);
});
},
),
),
),
SizedBox(
width: cardWidth - 10, // Subtract spacing to fit 3 cards
height: cardHeight - 10,
child: NewGameCard(
onGameCreated: (game) async {
game = await GameRepository.upsert(game);
setState(() {
games[game.name] = game;
});
},
),
),
],
),
);
},
),
),
);
}
}