Refactor display to separate widget
This commit is contained in:
218
lib/widgets/game_card.dart
Normal file
218
lib/widgets/game_card.dart
Normal file
@@ -0,0 +1,218 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:gamer_updater/game.dart';
|
||||
|
||||
class GameCard extends StatefulWidget {
|
||||
final Game game;
|
||||
final Function(Game) onGameUpdated;
|
||||
final Function()? onDelete;
|
||||
final bool isNameEditable;
|
||||
|
||||
const GameCard({
|
||||
super.key,
|
||||
required this.game,
|
||||
required this.onGameUpdated,
|
||||
this.onDelete,
|
||||
this.isNameEditable = false,
|
||||
});
|
||||
|
||||
@override
|
||||
State<GameCard> createState() => _GameCardState();
|
||||
}
|
||||
|
||||
class _GameCardState extends State<GameCard> with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _controller;
|
||||
bool _isLoading = false;
|
||||
int _deleteClickCount = 0;
|
||||
late TextEditingController _nameController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = AnimationController(
|
||||
duration: const Duration(seconds: 1),
|
||||
vsync: this,
|
||||
);
|
||||
_nameController = TextEditingController(text: widget.game.name);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
_nameController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _refreshVersion() async {
|
||||
setState(() => _isLoading = true);
|
||||
_controller.repeat();
|
||||
|
||||
final updatedGame = widget.game;
|
||||
await updatedGame.updateActualVersion();
|
||||
widget.onGameUpdated(updatedGame);
|
||||
|
||||
_controller.stop();
|
||||
_controller.reset();
|
||||
setState(() => _isLoading = false);
|
||||
}
|
||||
|
||||
void _handleDeleteClick() {
|
||||
setState(() {
|
||||
_deleteClickCount++;
|
||||
if (_deleteClickCount >= 2) {
|
||||
widget.onDelete?.call();
|
||||
_deleteClickCount = 0;
|
||||
}
|
||||
});
|
||||
|
||||
// Reset click count after a delay
|
||||
Future.delayed(const Duration(seconds: 1), () {
|
||||
if (mounted) {
|
||||
setState(() => _deleteClickCount = 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isUpToDate = widget.game.actualVersion == widget.game.lastPlayed;
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Expanded(
|
||||
child: widget.isNameEditable
|
||||
? TextField(
|
||||
controller: _nameController,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Game Name',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onChanged: (value) => widget.onGameUpdated(Game(
|
||||
name: value,
|
||||
versionRegex: widget.game.versionRegex,
|
||||
lastPlayed: widget.game.lastPlayed,
|
||||
rssFeedUrl: widget.game.rssFeedUrl,
|
||||
actualVersion: widget.game.actualVersion,
|
||||
lastUpdated: widget.game.lastUpdated,
|
||||
)),
|
||||
)
|
||||
: Text(
|
||||
widget.game.name,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
RotationTransition(
|
||||
turns: _controller,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _isLoading ? null : _refreshVersion,
|
||||
),
|
||||
),
|
||||
if (widget.onDelete != null)
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.delete,
|
||||
color: _deleteClickCount > 0 ? Colors.red : null,
|
||||
),
|
||||
onPressed: _handleDeleteClick,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 120,
|
||||
child: Text('Version:', style: Theme.of(context).textTheme.bodyLarge),
|
||||
),
|
||||
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),
|
||||
),
|
||||
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: TextEditingController(text: widget.game.versionRegex),
|
||||
decoration: const InputDecoration(labelText: 'Version Regex'),
|
||||
onChanged: (value) => widget.onGameUpdated(Game(
|
||||
name: widget.isNameEditable ? _nameController.text : widget.game.name,
|
||||
versionRegex: value,
|
||||
lastPlayed: widget.game.lastPlayed,
|
||||
rssFeedUrl: widget.game.rssFeedUrl,
|
||||
actualVersion: widget.game.actualVersion,
|
||||
lastUpdated: widget.game.lastUpdated,
|
||||
)),
|
||||
),
|
||||
TextField(
|
||||
controller: TextEditingController(text: widget.game.rssFeedUrl),
|
||||
decoration: const InputDecoration(labelText: 'RSS Feed URL'),
|
||||
onChanged: (value) => widget.onGameUpdated(Game(
|
||||
name: widget.isNameEditable ? _nameController.text : widget.game.name,
|
||||
versionRegex: widget.game.versionRegex,
|
||||
lastPlayed: widget.game.lastPlayed,
|
||||
rssFeedUrl: value,
|
||||
actualVersion: widget.game.actualVersion,
|
||||
lastUpdated: widget.game.lastUpdated,
|
||||
)),
|
||||
),
|
||||
TextField(
|
||||
controller: TextEditingController(text: widget.game.lastPlayed),
|
||||
decoration: const InputDecoration(labelText: 'Last Played'),
|
||||
onChanged: (value) => widget.onGameUpdated(Game(
|
||||
name: widget.isNameEditable ? _nameController.text : widget.game.name,
|
||||
versionRegex: widget.game.versionRegex,
|
||||
lastPlayed: value,
|
||||
rssFeedUrl: widget.game.rssFeedUrl,
|
||||
actualVersion: widget.game.actualVersion,
|
||||
lastUpdated: widget.game.lastUpdated,
|
||||
)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user