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: 30, 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 createState() => _MyHomePageState(); } class _MyHomePageState extends State { late Map games = {}; @override void initState() { super.initState(); _refreshGames(); } Future _refreshGames() async { final games = await GameRepository.getAll(); games.forEach((key, game) { game.updateActualVersion().then((_) { GameRepository.upsert(game); setState(() { games[game.name] = game; }); }); }); 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: SingleChildScrollView( padding: const EdgeInsets.all(8), child: Column( children: [ for (var i = 0; i < games.length + 1; i += 2) Padding( padding: const EdgeInsets.only(bottom: 8), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: i < games.length ? GameCard( game: games.values.elementAt(i), onGameUpdated: (game) async { game = await GameRepository.upsert(game); setState(() { games[game.name] = game; }); }, onDelete: () async { await GameRepository.delete( games.values.elementAt(i), ); setState(() { games.remove( games.values.elementAt(i).name, ); }); }, ) : NewGameCard( onGameCreated: (game) async { game = await GameRepository.upsert(game); setState(() { games[game.name] = game; }); }, ), ), const SizedBox(width: 8), Expanded( child: i + 1 < games.length ? GameCard( game: games.values.elementAt(i + 1), onGameUpdated: (game) async { game = await GameRepository.upsert(game); setState(() { games[game.name] = game; }); }, onDelete: () async { await GameRepository.delete( games.values.elementAt(i + 1), ); setState(() { games.remove( games.values.elementAt(i + 1).name, ); }); }, ) : const SizedBox(), // Empty space for odd number of items ), ], ), ), ], ), ), ), ); } }