185 lines
10 KiB
Dart
185 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:space_traders/models/agent_model.dart';
|
|
import 'package:space_traders/providers/agent_provider.dart';
|
|
import 'package:space_traders/models/ship_model.dart';
|
|
import 'package:space_traders/providers/ship_provider.dart';
|
|
import 'package:space_traders/pages/ship_page.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
@override
|
|
_HomePageState createState() => _HomePageState();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
title: Text('SpaceTraders API'),
|
|
),
|
|
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: FutureBuilder<AgentModel>(
|
|
future: agent.getAgent(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData) {
|
|
final agent = snapshot.data!;
|
|
return Text('Username : ${agent.symbol} -- Credits : ${agent.credits}');
|
|
} else if (snapshot.hasError) {
|
|
return Text('Error: ${snapshot.error}');
|
|
} else {
|
|
return CircularProgressIndicator();
|
|
}
|
|
},
|
|
),
|
|
),
|
|
Card(
|
|
color: Colors.white,
|
|
child: Align(
|
|
alignment: Alignment.center,
|
|
child: Text(
|
|
'Ships',
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
)
|
|
),
|
|
Card(
|
|
color: Colors.white,
|
|
child: FutureBuilder<List<ShipModel>>(
|
|
future: shipsFuture,//listShips(),
|
|
builder: (BuildContext context, AsyncSnapshot<List<ShipModel>> snapshot) {
|
|
if (snapshot.hasData) {
|
|
List<ShipModel> ships = snapshot.data!;
|
|
return SingleChildScrollView(
|
|
scrollDirection: Axis.horizontal,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Container(
|
|
width: MediaQuery.of(context).size.width, // Utiliser la largeur de l'écran
|
|
child: Card(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Table(
|
|
defaultColumnWidth: IntrinsicColumnWidth(),
|
|
children: ships.map((ship) {
|
|
return TableRow(
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ShipPage(ship: ship),
|
|
),
|
|
);
|
|
},
|
|
child: Container(
|
|
alignment: Alignment.centerLeft,
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text('${ship.symbol ?? 'N/A'}'),
|
|
),
|
|
),
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ShipPage(ship: ship),
|
|
),
|
|
);
|
|
},
|
|
child: Container(
|
|
alignment: Alignment.centerLeft,
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text('${ship.nav?.status ?? 'N/A'}'),
|
|
),
|
|
),
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ShipPage(ship: ship),
|
|
),
|
|
);
|
|
},
|
|
child: Container(
|
|
alignment: Alignment.centerLeft,
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text('Fuel: ${ship.fuel?.current ?? 'N/A'} / ${ship.fuel?.capacity ?? 'N/A'}'),
|
|
),
|
|
),
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ShipPage(ship: ship),
|
|
),
|
|
);
|
|
},
|
|
child: Container(
|
|
alignment: Alignment.centerLeft,
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text('Cargo: ${ship.cargo?.units ?? 'N/A'} / ${ship.cargo?.capacity ?? 'N/A'}'),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}).toList(),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
} else if (snapshot.hasError) {
|
|
return Text('Error: ${snapshot.error}');
|
|
} else {
|
|
return CircularProgressIndicator();
|
|
}
|
|
},
|
|
),
|
|
),
|
|
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|