Ajout vente
This commit is contained in:
@@ -14,6 +14,16 @@ class _HomePageState extends State<HomePage> {
|
|||||||
final agent = AgentProvider();
|
final agent = AgentProvider();
|
||||||
ShipProvider shipProvider = ShipProvider();
|
ShipProvider shipProvider = ShipProvider();
|
||||||
|
|
||||||
|
Future<List<ShipModel>>? shipsFuture;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeDependencies() {
|
||||||
|
super.didChangeDependencies();
|
||||||
|
shipsFuture = listShips();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Future<List<ShipModel>> listShips() async {
|
Future<List<ShipModel>> listShips() async {
|
||||||
List<ShipModel> ships = await shipProvider.listShips();
|
List<ShipModel> ships = await shipProvider.listShips();
|
||||||
return ships;
|
return ships;
|
||||||
@@ -67,7 +77,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
Card(
|
Card(
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
child: FutureBuilder<List<ShipModel>>(
|
child: FutureBuilder<List<ShipModel>>(
|
||||||
future: listShips(),
|
future: shipsFuture,//listShips(),
|
||||||
builder: (BuildContext context, AsyncSnapshot<List<ShipModel>> snapshot) {
|
builder: (BuildContext context, AsyncSnapshot<List<ShipModel>> snapshot) {
|
||||||
if (snapshot.hasData) {
|
if (snapshot.hasData) {
|
||||||
List<ShipModel> ships = snapshot.data!;
|
List<ShipModel> ships = snapshot.data!;
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ class ShipPage extends StatefulWidget {
|
|||||||
class _ShipPageState extends State<ShipPage> {
|
class _ShipPageState extends State<ShipPage> {
|
||||||
ShipModel? ship;
|
ShipModel? ship;
|
||||||
Timer? _timer;
|
Timer? _timer;
|
||||||
|
Timer? _timermin;
|
||||||
|
bool isButtonEnabled = true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -30,7 +32,7 @@ class _ShipPageState extends State<ShipPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _startTimer() {
|
void _startTimer() {
|
||||||
_timer = Timer.periodic(Duration(seconds: 15), (_) {
|
_timer = Timer.periodic(Duration(seconds: 5), (_) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_fetchShipData();
|
_fetchShipData();
|
||||||
});
|
});
|
||||||
@@ -46,6 +48,7 @@ class _ShipPageState extends State<ShipPage> {
|
|||||||
|
|
||||||
void _stopTimer() {
|
void _stopTimer() {
|
||||||
_timer?.cancel();
|
_timer?.cancel();
|
||||||
|
_timermin?.cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
ElevatedButton buildActionButton(String status) {
|
ElevatedButton buildActionButton(String status) {
|
||||||
@@ -54,22 +57,19 @@ class _ShipPageState extends State<ShipPage> {
|
|||||||
|
|
||||||
if (status == 'IN_ORBIT') {
|
if (status == 'IN_ORBIT') {
|
||||||
buttonText = 'Dock';
|
buttonText = 'Dock';
|
||||||
buttonAction = () {
|
buttonAction = () async {
|
||||||
// Action 1
|
Nav newNav = await ShipProvider.dockShip(widget.ship.symbol!);
|
||||||
print('Action 1');
|
ship?.nav = newNav;
|
||||||
};
|
};
|
||||||
} else if (status == 'Status2') {
|
} else if (status == 'DOCKED') {
|
||||||
buttonText = 'Action 2';
|
buttonText = 'Orbit';
|
||||||
buttonAction = () {
|
buttonAction = () async{
|
||||||
// Action 2
|
Nav newNav = await ShipProvider.orbitShip(widget.ship.symbol!);
|
||||||
print('Action 2');
|
ship?.nav = newNav;
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
buttonText = 'Default Action';
|
buttonText = '';
|
||||||
buttonAction = () {
|
buttonAction = null;
|
||||||
// Default Action
|
|
||||||
print('Default Action');
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ElevatedButton(
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -171,11 +189,11 @@ class _ShipPageState extends State<ShipPage> {
|
|||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
ElevatedButton(
|
//ElevatedButton(
|
||||||
onPressed: () {
|
// onPressed: () {
|
||||||
},
|
// },
|
||||||
child: Text('Markets'),
|
// child: Text('Markets'),
|
||||||
),
|
// ),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
SizedBox(height: 10),
|
SizedBox(height: 10),
|
||||||
@@ -191,7 +209,14 @@ class _ShipPageState extends State<ShipPage> {
|
|||||||
fontSize: 16,
|
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() ?? [],
|
}).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'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -40,4 +40,74 @@ class ShipProvider {
|
|||||||
return ShipModel.fromJson(decodedData['data']);
|
return ShipModel.fromJson(decodedData['data']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<Nav> dockShip(String shipSymbol) async {
|
||||||
|
final url = 'https://api.spacetraders.io/v2/my/ships/$shipSymbol/dock';
|
||||||
|
final token ='eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiVEZBIiwidmVyc2lvbiI6InYyIiwicmVzZXRfZGF0ZSI6IjIwMjMtMDYtMDMiLCJpYXQiOjE2ODU5NTMwODksInN1YiI6ImFnZW50LXRva2VuIn0.PPGF4B1ZtgqyWXBuGiLR71aHo9XJw9cA5OxP8xxriVuje3RDjdDstP3nEt0NiXSk4yP6N15DHJqIFe9BHH2sG1yVxcYXOvXQeoYMfnfg-HzdsmCv_tZmyC7Ey0go9HiMbt0WeNyNQYgJBonA5XicmfoqAXiggI51kMAdxq-zerwQAfBvfgDLmIqb1QwD0cMEy-VugkWe-CUUQDAXdarDnFRYlqP2lVLdtBdWVArpMYYFniR-Id5FQjOCiCyrtJ5pYPs6Ih0O9Lab9JU9_lncCqrG_FllVOwyvrE2kV8ScSKpotKhfI0_qV3FL2T_z25ZBEvfad0WFqmiubiRGuo0XQ';
|
||||||
|
|
||||||
|
final resp = await http.post(
|
||||||
|
Uri.parse(url),
|
||||||
|
headers: {'Authorization': 'Bearer $token'},
|
||||||
|
);
|
||||||
|
|
||||||
|
final decodedData = json.decode(utf8.decode(resp.bodyBytes));
|
||||||
|
|
||||||
|
return Nav.fromJson(decodedData['data']['nav']);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<Nav> orbitShip(String shipSymbol) async {
|
||||||
|
final url = 'https://api.spacetraders.io/v2/my/ships/$shipSymbol/orbit';
|
||||||
|
final token ='eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiVEZBIiwidmVyc2lvbiI6InYyIiwicmVzZXRfZGF0ZSI6IjIwMjMtMDYtMDMiLCJpYXQiOjE2ODU5NTMwODksInN1YiI6ImFnZW50LXRva2VuIn0.PPGF4B1ZtgqyWXBuGiLR71aHo9XJw9cA5OxP8xxriVuje3RDjdDstP3nEt0NiXSk4yP6N15DHJqIFe9BHH2sG1yVxcYXOvXQeoYMfnfg-HzdsmCv_tZmyC7Ey0go9HiMbt0WeNyNQYgJBonA5XicmfoqAXiggI51kMAdxq-zerwQAfBvfgDLmIqb1QwD0cMEy-VugkWe-CUUQDAXdarDnFRYlqP2lVLdtBdWVArpMYYFniR-Id5FQjOCiCyrtJ5pYPs6Ih0O9Lab9JU9_lncCqrG_FllVOwyvrE2kV8ScSKpotKhfI0_qV3FL2T_z25ZBEvfad0WFqmiubiRGuo0XQ';
|
||||||
|
|
||||||
|
final resp = await http.post(
|
||||||
|
Uri.parse(url),
|
||||||
|
headers: {'Authorization': 'Bearer $token'},
|
||||||
|
);
|
||||||
|
|
||||||
|
final decodedData = json.decode(utf8.decode(resp.bodyBytes));
|
||||||
|
return Nav.fromJson(decodedData['data']['nav']);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<int> extract(String shipSymbol) async {
|
||||||
|
final url = 'https://api.spacetraders.io/v2/my/ships/$shipSymbol/extract';
|
||||||
|
final token ='eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiVEZBIiwidmVyc2lvbiI6InYyIiwicmVzZXRfZGF0ZSI6IjIwMjMtMDYtMDMiLCJpYXQiOjE2ODU5NTMwODksInN1YiI6ImFnZW50LXRva2VuIn0.PPGF4B1ZtgqyWXBuGiLR71aHo9XJw9cA5OxP8xxriVuje3RDjdDstP3nEt0NiXSk4yP6N15DHJqIFe9BHH2sG1yVxcYXOvXQeoYMfnfg-HzdsmCv_tZmyC7Ey0go9HiMbt0WeNyNQYgJBonA5XicmfoqAXiggI51kMAdxq-zerwQAfBvfgDLmIqb1QwD0cMEy-VugkWe-CUUQDAXdarDnFRYlqP2lVLdtBdWVArpMYYFniR-Id5FQjOCiCyrtJ5pYPs6Ih0O9Lab9JU9_lncCqrG_FllVOwyvrE2kV8ScSKpotKhfI0_qV3FL2T_z25ZBEvfad0WFqmiubiRGuo0XQ';
|
||||||
|
|
||||||
|
final resp = await http.post(
|
||||||
|
Uri.parse(url),
|
||||||
|
headers: {'Authorization': 'Bearer $token'},
|
||||||
|
);
|
||||||
|
|
||||||
|
final decodedData = json.decode(utf8.decode(resp.bodyBytes));
|
||||||
|
print(decodedData);
|
||||||
|
return decodedData['data']['cooldown']['remainingSeconds'];
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<Cargo> sell(String shipSymbol, String symbol, int units) async {
|
||||||
|
final url = 'https://api.spacetraders.io/v2/my/ships/$shipSymbol/sell';
|
||||||
|
final token ='eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiVEZBIiwidmVyc2lvbiI6InYyIiwicmVzZXRfZGF0ZSI6IjIwMjMtMDYtMDMiLCJpYXQiOjE2ODU5NTMwODksInN1YiI6ImFnZW50LXRva2VuIn0.PPGF4B1ZtgqyWXBuGiLR71aHo9XJw9cA5OxP8xxriVuje3RDjdDstP3nEt0NiXSk4yP6N15DHJqIFe9BHH2sG1yVxcYXOvXQeoYMfnfg-HzdsmCv_tZmyC7Ey0go9HiMbt0WeNyNQYgJBonA5XicmfoqAXiggI51kMAdxq-zerwQAfBvfgDLmIqb1QwD0cMEy-VugkWe-CUUQDAXdarDnFRYlqP2lVLdtBdWVArpMYYFniR-Id5FQjOCiCyrtJ5pYPs6Ih0O9Lab9JU9_lncCqrG_FllVOwyvrE2kV8ScSKpotKhfI0_qV3FL2T_z25ZBEvfad0WFqmiubiRGuo0XQ';
|
||||||
|
|
||||||
|
Map<String, String> headers = {
|
||||||
|
'Authorization': 'Bearer $token',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
};
|
||||||
|
|
||||||
|
String newsymbol = symbol.toUpperCase().replaceAll(' ', '_');
|
||||||
|
Map<String, dynamic> body = {
|
||||||
|
'symbol': '$newsymbol',
|
||||||
|
'units': units,
|
||||||
|
};
|
||||||
|
|
||||||
|
String jsonString = jsonEncode(body);
|
||||||
|
|
||||||
|
print(jsonString);
|
||||||
|
|
||||||
|
final resp = await http.post(
|
||||||
|
Uri.parse(url),
|
||||||
|
headers: headers,
|
||||||
|
body: jsonString,
|
||||||
|
);
|
||||||
|
|
||||||
|
final decodedData = json.decode(utf8.decode(resp.bodyBytes));
|
||||||
|
print(decodedData);
|
||||||
|
return Cargo.fromJson(decodedData['data']['cargo']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user