Files
spaceTraders_API/lib/pages/ship_page.dart
2023-06-05 16:46:14 +02:00

256 lines
12 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;
Timer? _timermin;
bool isButtonEnabled = true;
@override
void initState() {
super.initState();
_startTimer();
ship = widget.ship;
}
@override
void dispose() {
_stopTimer();
super.dispose();
}
void _startTimer() {
_timer = Timer.periodic(Duration(seconds: 5), (_) {
setState(() {
_fetchShipData();
});
});
}
Future<void> _fetchShipData() async {
ShipModel refreshedShip = await ShipProvider.getShip(widget.ship.symbol!);
setState(() {
ship = refreshedShip;
});
}
void _stopTimer() {
_timer?.cancel();
_timermin?.cancel();
}
ElevatedButton buildActionButton(String status) {
String buttonText = '';
VoidCallback? buttonAction;
if (status == 'IN_ORBIT') {
buttonText = 'Dock';
buttonAction = () async {
Nav newNav = await ShipProvider.dockShip(widget.ship.symbol!);
ship?.nav = newNav;
};
} else if (status == 'DOCKED') {
buttonText = 'Orbit';
buttonAction = () async{
Nav newNav = await ShipProvider.orbitShip(widget.ship.symbol!);
ship?.nav = newNav;
};
} else {
buttonText = '';
buttonAction = null;
}
return ElevatedButton(
onPressed: buttonAction,
child: Text(buttonText),
);
}
void _onButtonPressed() async {
int duration;
if (isButtonEnabled) {
setState(() {
isButtonEnabled = false;
});
duration = await ShipProvider.extract(widget.ship.symbol!);
final disabledDuration = Duration(seconds: duration);
_timermin = Timer(disabledDuration, () {
setState(() {
isButtonEnabled = true;
});
});
}
}
@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,
),
),
ElevatedButton(
onPressed: () async {
Cargo newCargo = await ShipProvider.sell(widget.ship.symbol!, item.name!, item.units!);
},
child: Text('Sell'),
),
Divider(),
],
);
}).toList() ?? [],
),
],
),
),
),
Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Mining',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
),
ElevatedButton(
onPressed: isButtonEnabled ? _onButtonPressed : null,
child: Text('Start Mining'),
),
],
),
),
],
),
),
),
);
}
}