Files
spaceTraders_API/lib/pages/ship_page.dart
2023-06-05 12:39:41 +02:00

210 lines
9.3 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:space_traders/models/ship_model.dart';
import 'package:space_traders/providers/ship_provider.dart';
class ShipPage extends StatefulWidget {
final ShipModel ship;
ShipPage({required this.ship});
@override
_ShipPageState createState() => _ShipPageState();
}
class _ShipPageState extends State<ShipPage> {
ShipModel? ship;
Timer? _timer;
@override
void initState() {
super.initState();
_startTimer();
ship = widget.ship;
}
@override
void dispose() {
_stopTimer();
super.dispose();
}
void _startTimer() {
_timer = Timer.periodic(Duration(seconds: 15), (_) {
setState(() {
_fetchShipData();
});
});
}
Future<void> _fetchShipData() async {
ShipModel refreshedShip = await ShipProvider.getShip(widget.ship.symbol!);
setState(() {
ship = refreshedShip;
});
}
void _stopTimer() {
_timer?.cancel();
}
ElevatedButton buildActionButton(String status) {
String buttonText = '';
VoidCallback? buttonAction;
if (status == 'IN_ORBIT') {
buttonText = 'Dock';
buttonAction = () {
// Action 1
print('Action 1');
};
} else if (status == 'Status2') {
buttonText = 'Action 2';
buttonAction = () {
// Action 2
print('Action 2');
};
} else {
buttonText = 'Default Action';
buttonAction = () {
// Default Action
print('Default Action');
};
}
return ElevatedButton(
onPressed: buttonAction,
child: Text(buttonText),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Ship Details'),
),
body: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
color: Colors.white,
child: Align(
alignment: Alignment.topLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Name: ${ship?.symbol}',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Row(
children: [
Text('${ship?.nav?.status ?? 'N/A'}'),
SizedBox(height: 32),
SizedBox(width: 32),
buildActionButton(ship?.nav?.status ?? ''),
],
),
Row(
children: [
Text('Fuel: ${ship?.fuel?.current ?? 'N/A'} / ${ship?.fuel?.capacity ?? 'N/A'}'),
SizedBox(height: 32),
SizedBox(width: 32),
ElevatedButton(
onPressed: () {
},
child: Text('Refuel'),
),
],
),
Row(
children: [
Column(
children:[
Text('${ship?.nav?.waypointSymbol ?? 'N/A'}'),
Text('${ship?.nav?.route?.destination?.type ?? 'N/A'}'),
],
),
SizedBox(height: 32),
SizedBox(width: 32),
ElevatedButton(
onPressed: () {
},
child: Text('Select Destination'),
),
SizedBox(width: 32),
ElevatedButton(
onPressed: () {
},
child: Text('View System'),
),
],
),
],
),
),
),
Card(
color: Colors.white,
child: Align(
alignment: Alignment.topLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Cargo: ${ship?.cargo?.units ?? 'N/A'} / ${ship?.cargo?.capacity ?? 'N/A'}',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
ElevatedButton(
onPressed: () {
},
child: Text('Markets'),
),
],
),
SizedBox(height: 10),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: ship?.cargo?.inventory?.map((item) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${item.name ?? 'N/A'} - ${item.units ?? 'N/A'}',
style: TextStyle(
fontSize: 16,
),
),
Divider(), // Add a line separator between each item
],
);
}).toList() ?? [],
),
],
),
),
),
],
),
),
),
);
}
}