Ajout vente
This commit is contained in:
@@ -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!;
|
||||
|
||||
@@ -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'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -39,5 +39,75 @@ class ShipProvider {
|
||||
|
||||
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