Rafraichissement automatique page ship
This commit is contained in:
@@ -14,8 +14,8 @@ class _HomePageState extends State<HomePage> {
|
||||
final agent = AgentProvider();
|
||||
ShipProvider shipProvider = ShipProvider();
|
||||
|
||||
Future<List<ShipModel>> getShips() async {
|
||||
List<ShipModel> ships = await shipProvider.getShips();
|
||||
Future<List<ShipModel>> listShips() async {
|
||||
List<ShipModel> ships = await shipProvider.listShips();
|
||||
return ships;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ class _HomePageState extends State<HomePage> {
|
||||
Card(
|
||||
color: Colors.white,
|
||||
child: FutureBuilder<List<ShipModel>>(
|
||||
future: getShips(),
|
||||
future: listShips(),
|
||||
builder: (BuildContext context, AsyncSnapshot<List<ShipModel>> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
List<ShipModel> ships = snapshot.data!;
|
||||
|
||||
@@ -1,10 +1,52 @@
|
||||
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 StatelessWidget {
|
||||
final ShipModel ship;
|
||||
class ShipPage extends StatefulWidget {
|
||||
final ShipModel ship;
|
||||
|
||||
ShipPage({required this.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 = '';
|
||||
@@ -59,7 +101,7 @@ class ShipPage extends StatelessWidget {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Name: ${ship.symbol}',
|
||||
'Name: ${ship?.symbol}',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
@@ -67,15 +109,15 @@ class ShipPage extends StatelessWidget {
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Text('${ship.nav?.status ?? 'N/A'}'),
|
||||
Text('${ship?.nav?.status ?? 'N/A'}'),
|
||||
SizedBox(height: 32),
|
||||
SizedBox(width: 32),
|
||||
buildActionButton(ship.nav?.status ?? ''),
|
||||
buildActionButton(ship?.nav?.status ?? ''),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Text('Fuel: ${ship.fuel?.current ?? 'N/A'} / ${ship.fuel?.capacity ?? 'N/A'}'),
|
||||
Text('Fuel: ${ship?.fuel?.current ?? 'N/A'} / ${ship?.fuel?.capacity ?? 'N/A'}'),
|
||||
SizedBox(height: 32),
|
||||
SizedBox(width: 32),
|
||||
ElevatedButton(
|
||||
@@ -89,8 +131,8 @@ class ShipPage extends StatelessWidget {
|
||||
children: [
|
||||
Column(
|
||||
children:[
|
||||
Text('${ship.nav?.waypointSymbol ?? 'N/A'}'),
|
||||
Text('${ship.nav?.route?.destination?.type ?? 'N/A'}'),
|
||||
Text('${ship?.nav?.waypointSymbol ?? 'N/A'}'),
|
||||
Text('${ship?.nav?.route?.destination?.type ?? 'N/A'}'),
|
||||
],
|
||||
),
|
||||
SizedBox(height: 32),
|
||||
@@ -123,7 +165,7 @@ class ShipPage extends StatelessWidget {
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'Cargo: ${ship.cargo?.units ?? 'N/A'} / ${ship.cargo?.capacity ?? 'N/A'}',
|
||||
'Cargo: ${ship?.cargo?.units ?? 'N/A'} / ${ship?.cargo?.capacity ?? 'N/A'}',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
@@ -139,7 +181,7 @@ class ShipPage extends StatelessWidget {
|
||||
SizedBox(height: 10),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: ship.cargo?.inventory?.map((item) {
|
||||
children: ship?.cargo?.inventory?.map((item) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
|
||||
Reference in New Issue
Block a user