Add background images for games

This commit is contained in:
2025-02-22 16:58:03 +01:00
parent 0a40e5bbcf
commit e5fc67ef43
12 changed files with 414 additions and 139 deletions

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:gamer_updater/game.dart';
import 'package:image_picker/image_picker.dart';
class GameCard extends StatefulWidget {
final Game game;
@@ -57,19 +58,28 @@ class _GameCardState extends State<GameCard>
void _setupFocusListeners() {
void updateGame() {
var name =
widget.isNameEditable ? _nameController.text : widget.game.name;
if (name.isNotEmpty) {
widget.onGameUpdated(
Game(
name: name,
versionRegex: _versionRegexController.text,
lastPlayed: _lastPlayedController.text,
rssFeedUrl: _rssFeedUrlController.text,
actualVersion: widget.game.actualVersion,
lastUpdated: widget.game.lastUpdated,
),
);
try {
var name =
widget.isNameEditable ? _nameController.text : widget.game.name;
if (name.isNotEmpty) {
widget.onGameUpdated(
Game(
name: name,
versionRegex: _versionRegexController.text,
lastPlayed: _lastPlayedController.text,
rssFeedUrl: _rssFeedUrlController.text,
actualVersion: widget.game.actualVersion,
lastUpdated: widget.game.lastUpdated,
imageData: widget.game.imageData,
),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(e.toString())));
}
}
}
@@ -139,134 +149,240 @@ class _GameCardState extends State<GameCard>
});
}
Future<void> _pickImage() async {
final picker = ImagePicker();
final image = await picker.pickImage(source: ImageSource.gallery);
if (image != null) {
final imageBytes = await image.readAsBytes();
final updatedGame = await GameRepository.updateImage(
Game(
name: widget.game.name,
versionRegex: _versionRegexController.text,
lastPlayed: _lastPlayedController.text,
rssFeedUrl: _rssFeedUrlController.text,
actualVersion: widget.game.actualVersion,
lastUpdated: widget.game.lastUpdated,
imageData: imageBytes,
),
);
widget.onGameUpdated(updatedGame);
}
}
@override
Widget build(BuildContext context) {
final isUpToDate = widget.game.actualVersion == widget.game.lastPlayed;
final hasImage = widget.game.imageData != null;
return Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 40,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: TextField(
controller: _nameController,
focusNode: _nameFocus,
style: Theme.of(context).textTheme.titleLarge,
enabled: widget.isNameEditable,
decoration: const InputDecoration.collapsed(
hintText: 'New Game',
),
onSubmitted: (_) => _nameFocus.unfocus(),
),
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
if (widget.onDelete != null)
IconButton(
icon: Icon(
Icons.delete,
color: _deleteClickCount > 0 ? Colors.red : null,
),
onPressed: _handleDeleteClick,
),
if (!widget.isNameEditable)
RotationTransition(
turns: _controller,
child: IconButton(
icon: const Icon(Icons.refresh),
onPressed: _isLoading ? null : _refreshVersion,
),
),
],
),
],
child: Stack(
children: [
if (hasImage)
Positioned.fill(
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Image.memory(widget.game.imageData!, fit: BoxFit.cover),
),
),
const SizedBox(height: 8),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Version:',
style: Theme.of(context).textTheme.bodyLarge,
if (hasImage)
Positioned.fill(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.transparent,
Colors.black.withOpacity(0.7),
Colors.black.withOpacity(0.9),
],
),
),
Text(
widget.game.actualVersion.isEmpty
? 'Unknown'
: widget.game.actualVersion,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: isUpToDate ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 40,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: TextField(
controller: _nameController,
focusNode: _nameFocus,
style: Theme.of(context).textTheme.titleLarge
?.copyWith(color: hasImage ? Colors.white : null),
enabled: widget.isNameEditable,
decoration: const InputDecoration.collapsed(
hintText: 'New Game',
),
onSubmitted: (_) => _nameFocus.unfocus(),
),
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
if (!widget.isNameEditable)
IconButton(
icon: const Icon(Icons.image),
color: hasImage ? Colors.white : null,
onPressed: _pickImage,
),
if (widget.onDelete != null)
IconButton(
icon: Icon(
Icons.delete,
color:
_deleteClickCount > 0
? Colors.red
: (hasImage ? Colors.white : null),
),
onPressed: _handleDeleteClick,
),
if (!widget.isNameEditable)
RotationTransition(
turns: _controller,
child: IconButton(
icon: Icon(
Icons.refresh,
color: hasImage ? Colors.white : null,
),
onPressed: _isLoading ? null : _refreshVersion,
),
),
],
),
],
),
),
const SizedBox(height: 8),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Version:',
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: hasImage ? Colors.white : null,
),
),
),
Text(
widget.game.actualVersion.isEmpty
? 'Unknown'
: widget.game.actualVersion,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: isUpToDate ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
],
),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Last Updated:',
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: hasImage ? Colors.white : null,
),
),
),
Text(
widget.game.lastUpdated.isEmpty
? 'Never'
: DateTime.parse(widget.game.lastUpdated).toString(),
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: hasImage ? Colors.white : null,
),
),
],
),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Last Played:',
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: hasImage ? Colors.white : null,
),
),
),
Text(
widget.game.lastPlayed,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: isUpToDate ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
],
),
const Divider(),
TextField(
controller: _versionRegexController,
focusNode: _versionRegexFocus,
style: TextStyle(color: hasImage ? Colors.white : null),
decoration: InputDecoration(
labelText: 'Version Regex',
labelStyle: TextStyle(
color: hasImage ? Colors.white70 : null,
),
enabledBorder:
hasImage
? const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white70),
)
: null,
),
onSubmitted: (_) => _versionRegexFocus.unfocus(),
),
TextField(
controller: _rssFeedUrlController,
focusNode: _rssFeedUrlFocus,
style: TextStyle(color: hasImage ? Colors.white : null),
decoration: InputDecoration(
labelText: 'RSS Feed URL',
labelStyle: TextStyle(
color: hasImage ? Colors.white70 : null,
),
enabledBorder:
hasImage
? const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white70),
)
: null,
),
onSubmitted: (_) => _rssFeedUrlFocus.unfocus(),
),
TextField(
controller: _lastPlayedController,
focusNode: _lastPlayedFocus,
style: TextStyle(color: hasImage ? Colors.white : null),
decoration: InputDecoration(
labelText: 'Last Played',
labelStyle: TextStyle(
color: hasImage ? Colors.white70 : null,
),
enabledBorder:
hasImage
? const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white70),
)
: null,
),
onSubmitted: (_) => _lastPlayedFocus.unfocus(),
),
],
),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Last Updated:',
style: Theme.of(context).textTheme.bodyLarge,
),
),
Text(
widget.game.lastUpdated.isEmpty
? 'Never'
: DateTime.parse(widget.game.lastUpdated).toString(),
style: Theme.of(context).textTheme.bodyMedium,
),
],
),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Last Played:',
style: Theme.of(context).textTheme.bodyLarge,
),
),
Text(
widget.game.lastPlayed,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: isUpToDate ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
],
),
const Divider(),
TextField(
controller: _versionRegexController,
focusNode: _versionRegexFocus,
decoration: const InputDecoration(labelText: 'Version Regex'),
onSubmitted: (_) => _versionRegexFocus.unfocus(),
),
TextField(
controller: _rssFeedUrlController,
focusNode: _rssFeedUrlFocus,
decoration: const InputDecoration(labelText: 'RSS Feed URL'),
onSubmitted: (_) => _rssFeedUrlFocus.unfocus(),
),
TextField(
controller: _lastPlayedController,
focusNode: _lastPlayedFocus,
decoration: const InputDecoration(labelText: 'Last Played'),
onSubmitted: (_) => _lastPlayedFocus.unfocus(),
),
],
),
),
],
),
);
}