Ajout vente

This commit is contained in:
Gabriel
2023-06-05 16:46:14 +02:00
parent 4a53decc74
commit 150ca82f74
3 changed files with 147 additions and 21 deletions

View File

@@ -14,6 +14,16 @@ class _HomePageState extends State<HomePage> {
final agent = AgentProvider();
ShipProvider shipProvider = ShipProvider();
Future<List<ShipModel>>? shipsFuture;
@override
void didChangeDependencies() {
super.didChangeDependencies();
shipsFuture = listShips();
}
Future<List<ShipModel>> listShips() async {
List<ShipModel> ships = await shipProvider.listShips();
return ships;
@@ -67,7 +77,7 @@ class _HomePageState extends State<HomePage> {
Card(
color: Colors.white,
child: FutureBuilder<List<ShipModel>>(
future: listShips(),
future: shipsFuture,//listShips(),
builder: (BuildContext context, AsyncSnapshot<List<ShipModel>> snapshot) {
if (snapshot.hasData) {
List<ShipModel> ships = snapshot.data!;

View File

@@ -15,6 +15,8 @@ class ShipPage extends StatefulWidget {
class _ShipPageState extends State<ShipPage> {
ShipModel? ship;
Timer? _timer;
Timer? _timermin;
bool isButtonEnabled = true;
@override
void initState() {
@@ -30,7 +32,7 @@ class _ShipPageState extends State<ShipPage> {
}
void _startTimer() {
_timer = Timer.periodic(Duration(seconds: 15), (_) {
_timer = Timer.periodic(Duration(seconds: 5), (_) {
setState(() {
_fetchShipData();
});
@@ -46,6 +48,7 @@ class _ShipPageState extends State<ShipPage> {
void _stopTimer() {
_timer?.cancel();
_timermin?.cancel();
}
ElevatedButton buildActionButton(String status) {
@@ -54,22 +57,19 @@ class _ShipPageState extends State<ShipPage> {
if (status == 'IN_ORBIT') {
buttonText = 'Dock';
buttonAction = () {
// Action 1
print('Action 1');
buttonAction = () async {
Nav newNav = await ShipProvider.dockShip(widget.ship.symbol!);
ship?.nav = newNav;
};
} else if (status == 'Status2') {
buttonText = 'Action 2';
buttonAction = () {
// Action 2
print('Action 2');
} else if (status == 'DOCKED') {
buttonText = 'Orbit';
buttonAction = () async{
Nav newNav = await ShipProvider.orbitShip(widget.ship.symbol!);
ship?.nav = newNav;
};
} else {
buttonText = 'Default Action';
buttonAction = () {
// Default Action
print('Default Action');
};
buttonText = '';
buttonAction = null;
}
return ElevatedButton(
@@ -78,6 +78,24 @@ class _ShipPageState extends State<ShipPage> {
);
}
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(
@@ -171,11 +189,11 @@ class _ShipPageState extends State<ShipPage> {
fontWeight: FontWeight.bold,
),
),
ElevatedButton(
onPressed: () {
},
child: Text('Markets'),
),
//ElevatedButton(
// onPressed: () {
// },
// child: Text('Markets'),
// ),
],
),
SizedBox(height: 10),
@@ -191,7 +209,14 @@ class _ShipPageState extends State<ShipPage> {
fontSize: 16,
),
),
Divider(), // Add a line separator between each item
ElevatedButton(
onPressed: () async {
Cargo newCargo = await ShipProvider.sell(widget.ship.symbol!, item.name!, item.units!);
},
child: Text('Sell'),
),
Divider(),
],
);
}).toList() ?? [],
@@ -200,6 +225,27 @@ class _ShipPageState extends State<ShipPage> {
),
),
),
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'),
),
],
),
),
],
),
),