Ajout fichiers
This commit is contained in:
27
lib/main.dart
Normal file
27
lib/main.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:space_traders/pages/home_page.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'SpaceTraders',
|
||||
initialRoute: 'home',
|
||||
routes: {
|
||||
'home': (BuildContext context) => HomePage(),
|
||||
},
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
30
lib/models/agent_model.dart
Normal file
30
lib/models/agent_model.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'dart:convert';
|
||||
|
||||
AgentModel agentModelFromJson(String str) =>
|
||||
AgentModel.fromJson(json.decode(str));
|
||||
|
||||
//String AgentModelToJson(ActivityModel data) => json.encode(data.toJson());
|
||||
|
||||
class AgentModel {
|
||||
AgentModel({
|
||||
this.accountId,
|
||||
this.symbol,
|
||||
this.headquarters,
|
||||
this.credits,
|
||||
this.startingFaction,
|
||||
});
|
||||
|
||||
String? accountId;
|
||||
String? symbol;
|
||||
String? headquarters;
|
||||
int? credits;
|
||||
String? startingFaction;
|
||||
|
||||
factory AgentModel.fromJson(Map<String, dynamic> json) => AgentModel(
|
||||
accountId: json["accountId"],
|
||||
symbol: json["symbol"],
|
||||
headquarters: json["headquarters"],
|
||||
credits: json["credits"],
|
||||
startingFaction: json["startingFaction"],
|
||||
);
|
||||
}
|
||||
219
lib/models/gpt_fleet_model.dart
Normal file
219
lib/models/gpt_fleet_model.dart
Normal file
@@ -0,0 +1,219 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class FleetModel {
|
||||
List<Data>? data;
|
||||
|
||||
FleetModel({this.data});
|
||||
|
||||
factory FleetModel.fromJson(Map<String, dynamic> json) {
|
||||
final dataList = json['data'] as List<dynamic>;
|
||||
|
||||
final List<Data> data = dataList.map((e) => Data.fromJson(e)).toList();
|
||||
|
||||
return FleetModel(data: data);
|
||||
}
|
||||
}
|
||||
|
||||
class Data {
|
||||
String? symbol;
|
||||
Nav? nav;
|
||||
Registration? registration;
|
||||
Fuel? fuel;
|
||||
//Frame? frame;
|
||||
//Reactor? reactor;
|
||||
//Engine? engine;
|
||||
//List<Module>? modules;
|
||||
//List<Mount>? mounts;
|
||||
Cargo? cargo;
|
||||
|
||||
Data({
|
||||
this.symbol,
|
||||
this.nav,
|
||||
this.registration,
|
||||
this.fuel,
|
||||
//this.frame,
|
||||
//this.reactor,
|
||||
//this.engine,
|
||||
//this.modules,
|
||||
//this.mounts,
|
||||
this.cargo,
|
||||
});
|
||||
|
||||
factory Data.fromJson(Map<String, dynamic> json) {
|
||||
return Data(
|
||||
symbol: json['symbol'],
|
||||
nav: Nav.fromJson(json['nav']),
|
||||
registration: Registration.fromJson(json['registration']),
|
||||
fuel: Fuel.fromJson(json['fuel']),
|
||||
//frame: Frame.fromJson(json['frame']),
|
||||
//reactor: Reactor.fromJson(json['reactor']),
|
||||
//engine: Engine.fromJson(json['engine']),
|
||||
//modules: List<Module>.from(json['modules']?.map((x) => Module.fromJson(x))),
|
||||
//mounts: List<Mount>.from(json['mounts']?.map((x) => Mount.fromJson(x))),
|
||||
cargo: Cargo.fromJson(json['cargo']),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Nav {
|
||||
String? systemSymbol;
|
||||
String? waypointSymbol;
|
||||
Route? route;
|
||||
String? status;
|
||||
String? flightMode;
|
||||
|
||||
Nav({
|
||||
this.systemSymbol,
|
||||
this.waypointSymbol,
|
||||
this.route,
|
||||
this.status,
|
||||
this.flightMode,
|
||||
});
|
||||
|
||||
factory Nav.fromJson(Map<String, dynamic> json) {
|
||||
return Nav(
|
||||
systemSymbol: json['systemSymbol'],
|
||||
waypointSymbol: json['waypointSymbol'],
|
||||
route: Route.fromJson(json['route']),
|
||||
status: json['status'],
|
||||
flightMode: json['flightMode'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Route {
|
||||
Departure? departure;
|
||||
Destination? destination;
|
||||
String? arrival;
|
||||
String? departureTime;
|
||||
|
||||
Route({
|
||||
this.departure,
|
||||
this.destination,
|
||||
this.arrival,
|
||||
this.departureTime,
|
||||
});
|
||||
|
||||
factory Route.fromJson(Map<String, dynamic> json) {
|
||||
return Route(
|
||||
departure: Departure.fromJson(json['departure']),
|
||||
destination: Destination.fromJson(json['destination']),
|
||||
arrival: json['arrival'],
|
||||
departureTime: json['departureTime'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Departure {
|
||||
String? symbol;
|
||||
String? type;
|
||||
String? systemSymbol;
|
||||
int? x;
|
||||
int? y;
|
||||
|
||||
Departure({
|
||||
this.symbol,
|
||||
this.type,
|
||||
this.systemSymbol,
|
||||
this.x,
|
||||
this.y,
|
||||
});
|
||||
|
||||
factory Departure.fromJson(Map<String, dynamic> json) {
|
||||
return Departure(
|
||||
symbol: json['symbol'],
|
||||
type: json['type'],
|
||||
systemSymbol: json['systemSymbol'],
|
||||
x: json['x'],
|
||||
y: json['y'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Destination {
|
||||
String? symbol;
|
||||
String? type;
|
||||
String? systemSymbol;
|
||||
int? x;
|
||||
int? y;
|
||||
|
||||
Destination({
|
||||
this.symbol,
|
||||
this.type,
|
||||
this.systemSymbol,
|
||||
this.x,
|
||||
this.y,
|
||||
});
|
||||
|
||||
factory Destination.fromJson(Map<String, dynamic> json) {
|
||||
return Destination(
|
||||
symbol: json['symbol'],
|
||||
type: json['type'],
|
||||
systemSymbol: json['systemSymbol'],
|
||||
x: json['x'],
|
||||
y: json['y'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Registration {
|
||||
String? name;
|
||||
String? factionSymbol;
|
||||
String? role;
|
||||
|
||||
Registration({this.name,this.factionSymbol,this.role});
|
||||
|
||||
factory Registration.fromJson(Map<String, dynamic> json) {
|
||||
return Registration(
|
||||
name: json['name'],
|
||||
factionSymbol: json['factionSymbol'],
|
||||
role: json['role'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Fuel {
|
||||
int? current;
|
||||
int? capacity;
|
||||
Consumed? consumed;
|
||||
|
||||
Fuel({this.current, this.capacity, this.consumed});
|
||||
|
||||
factory Fuel.fromJson(Map<String, dynamic> json) {
|
||||
return Fuel(
|
||||
current: json['current'],
|
||||
capacity: json['capacity'],
|
||||
consumed: Consumed.fromJson(json['consumed']),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Cargo {
|
||||
int? capacity;
|
||||
int? units;
|
||||
//List<Inventory>? inventory;
|
||||
|
||||
Cargo({this.capacity, this.units});//, this.inventory});
|
||||
|
||||
factory Cargo.fromJson(Map<String, dynamic> json) {
|
||||
return Cargo(
|
||||
capacity: json['capacity'],
|
||||
units: json['unit'],
|
||||
//inventory: json.decode(json['inventory']),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Consumed {
|
||||
int? amount;
|
||||
String? timestamp;
|
||||
|
||||
Consumed({this.amount, this.timestamp});
|
||||
|
||||
factory Consumed.fromJson(Map<String, dynamic> json) {
|
||||
return Consumed(
|
||||
amount: json['amount'],
|
||||
timestamp: json['timestamp'],
|
||||
);
|
||||
}
|
||||
}
|
||||
134
lib/models/ship_model.dart
Normal file
134
lib/models/ship_model.dart
Normal file
@@ -0,0 +1,134 @@
|
||||
import 'dart:convert';
|
||||
|
||||
|
||||
class Registration {
|
||||
String? name;
|
||||
String? factionSymbol;
|
||||
String? role;
|
||||
|
||||
Registration({this.name,this.factionSymbol,this.role});
|
||||
|
||||
factory Registration.fromJson(Map<String, dynamic> json) {
|
||||
return Registration(
|
||||
name: json['name'],
|
||||
factionSymbol: json['factionSymbol'],
|
||||
role: json['role'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Nav {
|
||||
String? systemSymbol;
|
||||
String? waypointSymbol;
|
||||
//TODO Route
|
||||
String? status;
|
||||
String? flightMode;
|
||||
|
||||
Nav({this.systemSymbol, this.waypointSymbol, this.status, this.flightMode});
|
||||
|
||||
factory Nav.fromJson(Map<String, dynamic> json) {
|
||||
return Nav(
|
||||
systemSymbol: json['systemSymbol'],
|
||||
waypointSymbol: json['waypointSymbol'],
|
||||
status: json['status'],
|
||||
flightMode: json['flightMode'],
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Inventory {
|
||||
String? symbol;
|
||||
String? name;
|
||||
String? description;
|
||||
int? units;
|
||||
|
||||
Inventory({this.symbol, this.name, this.description, this.units});
|
||||
|
||||
factory Inventory.fromJson(Map<String, dynamic> json) {
|
||||
return Inventory(
|
||||
symbol: json['symbol'],
|
||||
name: json['name'],
|
||||
description: json['description'],
|
||||
units: json['units'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Cargo {
|
||||
int? capacity;
|
||||
int? units;
|
||||
//List<Inventory>? inventory;
|
||||
|
||||
Cargo({this.capacity, this.units});//, this.inventory});
|
||||
|
||||
factory Cargo.fromJson(Map<String, dynamic> json) {
|
||||
return Cargo(
|
||||
capacity: json['capacity'],
|
||||
units: json['unit'],
|
||||
//inventory: json.decode(json['inventory']),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Consumed {
|
||||
int? amount;
|
||||
String? timestamp;
|
||||
|
||||
Consumed({this.amount, this.timestamp});
|
||||
|
||||
factory Consumed.fromJson(Map<String, dynamic> json) {
|
||||
return Consumed(
|
||||
amount: json['amount'],
|
||||
timestamp: json['timestamp'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Fuel {
|
||||
int? current;
|
||||
int? capacity;
|
||||
Consumed? consumed;
|
||||
|
||||
Fuel({this.current, this.capacity, this.consumed});
|
||||
|
||||
factory Fuel.fromJson(Map<String, dynamic> json) {
|
||||
return Fuel(
|
||||
current: json['current'],
|
||||
capacity: json['capacity'],
|
||||
consumed: Consumed.fromJson(json['consumed']),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ShipModel {
|
||||
ShipModel({
|
||||
this.symbol,
|
||||
this.registration,
|
||||
this.nav,
|
||||
// this.crew,
|
||||
// this.frame,
|
||||
// this.reactor,
|
||||
// this.engine,
|
||||
// this.modules,
|
||||
// this.mounts,
|
||||
this.cargo,
|
||||
this.fuel,
|
||||
});
|
||||
|
||||
String? symbol;
|
||||
Registration? registration;
|
||||
Nav? nav;
|
||||
Cargo? cargo;
|
||||
Fuel? fuel;
|
||||
|
||||
factory ShipModel.fromJson(Map<String, dynamic> json) {
|
||||
return ShipModel(
|
||||
symbol: json['symbol'],
|
||||
registration: Registration.fromJson(json['registration']),
|
||||
nav: Nav.fromJson(json['nav']),
|
||||
cargo: Cargo.fromJson(json['cargo']),
|
||||
fuel: Fuel.fromJson(json['fuel']),
|
||||
);
|
||||
}
|
||||
}
|
||||
97
lib/pages/home_page.dart
Normal file
97
lib/pages/home_page.dart
Normal file
@@ -0,0 +1,97 @@
|
||||
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';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
@override
|
||||
_HomePageState createState() => _HomePageState();
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
final agent = AgentProvider();
|
||||
ShipProvider shipProvider = ShipProvider();
|
||||
|
||||
Future<List<ShipModel>> getShips() async {
|
||||
List<ShipModel> ships = await shipProvider.getShips();
|
||||
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: getShips(),
|
||||
builder: (BuildContext context, AsyncSnapshot<List<ShipModel>> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
List<ShipModel> ships = snapshot.data!;
|
||||
return Column(
|
||||
children: ships.map((ship) {
|
||||
return Card(
|
||||
child: ListTile(
|
||||
title: Text(ship.symbol ?? 'N/A'),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}else if (snapshot.hasError) {
|
||||
return Text('Error: ${snapshot.error}');
|
||||
}else {
|
||||
return CircularProgressIndicator();
|
||||
}
|
||||
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
26
lib/providers/agent_provider.dart
Normal file
26
lib/providers/agent_provider.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:space_traders/models/agent_model.dart';
|
||||
|
||||
|
||||
class AgentProvider {
|
||||
|
||||
Future<AgentModel> getAgent() async {
|
||||
final url = 'https://api.spacetraders.io/v2/my/agent';
|
||||
final token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiVFJJUExFRkEiLCJ2ZXJzaW9uIjoidjIiLCJyZXNldF9kYXRlIjoiMjAyMy0wNS0yMCIsImlhdCI6MTY4NTA4MzgwNiwic3ViIjoiYWdlbnQtdG9rZW4ifQ.F90krgDq6p2yAiQtWIjFEYqMRqepaUQ8j6hnoKIrj9FrSLLvYhY2LEIII70ta6b97Fqsr5szAhmIP0AfXovCONXXq9EIR74SOojGsyt5-8LoNvZlNt6LrmNyuhiJgipogKNrSuUt0L3SEjLjUGeF_k-85rEMcH_Gts31IGfYD6mFWlj142ORJUIzF6gCqVYXYYKw99VObAvGwQnpbd3LrDGJShJn0Bjh5Fb0ACbaesTgFzktSW0FP_lxsf9m3uQastgnX3otioq1CnwfDTQC6uNwFK2KvCEcebnGqUU9l5GNrMEnqZ02YXnbFZx6b1icop_1wsyajsrJHsqNs6zliw';
|
||||
|
||||
final resp = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: {'Authorization': 'Bearer $token'},
|
||||
);
|
||||
|
||||
final decodedData = json.decode(utf8.decode(resp.bodyBytes));
|
||||
|
||||
final agent = AgentModel.fromJson(decodedData['data']);
|
||||
|
||||
return agent;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
29
lib/providers/ship_provider.dart
Normal file
29
lib/providers/ship_provider.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:space_traders/models/ship_model.dart';
|
||||
|
||||
|
||||
class ShipProvider {
|
||||
|
||||
Future<List<ShipModel>> getShips() async {
|
||||
final url = 'https://api.spacetraders.io/v2/my/ships';
|
||||
final token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiVFJJUExFRkEiLCJ2ZXJzaW9uIjoidjIiLCJyZXNldF9kYXRlIjoiMjAyMy0wNS0yMCIsImlhdCI6MTY4NTA4MzgwNiwic3ViIjoiYWdlbnQtdG9rZW4ifQ.F90krgDq6p2yAiQtWIjFEYqMRqepaUQ8j6hnoKIrj9FrSLLvYhY2LEIII70ta6b97Fqsr5szAhmIP0AfXovCONXXq9EIR74SOojGsyt5-8LoNvZlNt6LrmNyuhiJgipogKNrSuUt0L3SEjLjUGeF_k-85rEMcH_Gts31IGfYD6mFWlj142ORJUIzF6gCqVYXYYKw99VObAvGwQnpbd3LrDGJShJn0Bjh5Fb0ACbaesTgFzktSW0FP_lxsf9m3uQastgnX3otioq1CnwfDTQC6uNwFK2KvCEcebnGqUU9l5GNrMEnqZ02YXnbFZx6b1icop_1wsyajsrJHsqNs6zliw';
|
||||
final resp = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: {'Authorization': 'Bearer $token'},
|
||||
);
|
||||
|
||||
final decodedData = json.decode(utf8.decode(resp.bodyBytes));
|
||||
//final fleet = FleetModel.fromJson(decodedData['data']);
|
||||
final List<ShipModel> fleet = [];
|
||||
final List<dynamic> data = decodedData['data'];
|
||||
|
||||
data.forEach((ship) {
|
||||
final shipTemp = ShipModel.fromJson(ship);
|
||||
fleet.add(shipTemp);
|
||||
});
|
||||
|
||||
return fleet;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user