-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Note: it's not responsive yet and doesn't look good on a small screen Signed-off-by: Philipp Hoenisch <philipp@coblox.tech>
- Loading branch information
Showing
9 changed files
with
269 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:get_10101/common/color.dart'; | ||
import 'package:get_10101/logger/logger.dart'; | ||
import 'package:get_10101/trade/open_position_service.dart'; | ||
import 'package:intl/intl.dart'; | ||
|
||
class OrderAndPositionTable extends StatefulWidget { | ||
const OrderAndPositionTable({super.key}); | ||
|
||
@override | ||
OrderAndPositionTableState createState() => OrderAndPositionTableState(); | ||
} | ||
|
||
class OrderAndPositionTableState extends State<OrderAndPositionTable> | ||
with SingleTickerProviderStateMixin { | ||
late final _tabController = TabController(length: 2, vsync: this); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
mainAxisAlignment: MainAxisAlignment.start, | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
children: <Widget>[ | ||
TabBar( | ||
unselectedLabelColor: Colors.black, | ||
labelColor: tenTenOnePurple, | ||
controller: _tabController, | ||
isScrollable: false, | ||
tabs: const [ | ||
Tab( | ||
text: 'Open', | ||
), | ||
Tab( | ||
text: 'Pending', | ||
), | ||
], | ||
), | ||
Container( | ||
constraints: const BoxConstraints( | ||
// Adding constraints to avoid unbounded height, 400 is a random number to avoid pixel overflow | ||
maxHeight: 200, | ||
), | ||
child: TabBarView( | ||
controller: _tabController, | ||
children: const <Widget>[ | ||
SimpleTableWidget(), | ||
Text("Pending"), | ||
], | ||
), | ||
), | ||
], | ||
); | ||
} | ||
} | ||
|
||
class SimpleTableWidget extends StatelessWidget { | ||
const SimpleTableWidget({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FutureBuilder<List<Position>>( | ||
future: OpenPositionsService.fetchOpenPositions(), | ||
builder: (context, snapshot) { | ||
if (snapshot.connectionState == ConnectionState.waiting) { | ||
return const Center(child: CircularProgressIndicator()); | ||
} else if (snapshot.hasError) { | ||
logger.i("received ${snapshot.error}"); | ||
return const Center(child: Text('Error loading data')); | ||
} else if (!snapshot.hasData || snapshot.data!.isEmpty) { | ||
return const Center(child: Text('No data available')); | ||
} else { | ||
return buildTable(snapshot.data!); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
Widget buildTable(List<Position> positions) { | ||
return Container( | ||
decoration: BoxDecoration( | ||
border: Border.all( | ||
width: 1, | ||
), | ||
borderRadius: const BorderRadius.all(Radius.circular(10)), | ||
), | ||
child: Table( | ||
border: TableBorder.symmetric(inside: const BorderSide(width: 2, color: Colors.black)), | ||
defaultVerticalAlignment: TableCellVerticalAlignment.middle, | ||
columnWidths: const { | ||
0: MinColumnWidth(FixedColumnWidth(100.0), FractionColumnWidth(0.1)), | ||
1: MinColumnWidth(FixedColumnWidth(100.0), FractionColumnWidth(0.1)), | ||
2: MinColumnWidth(FixedColumnWidth(100.0), FractionColumnWidth(0.1)), | ||
3: MinColumnWidth(FixedColumnWidth(100.0), FractionColumnWidth(0.1)), | ||
4: MinColumnWidth(FixedColumnWidth(100.0), FractionColumnWidth(0.1)), | ||
5: MinColumnWidth(FixedColumnWidth(100.0), FractionColumnWidth(0.1)), | ||
6: MinColumnWidth(FixedColumnWidth(100.0), FractionColumnWidth(0.1)), | ||
7: MinColumnWidth(FixedColumnWidth(200.0), FractionColumnWidth(0.2)), | ||
}, | ||
children: [ | ||
TableRow( | ||
decoration: BoxDecoration( | ||
color: tenTenOnePurple.shade300, | ||
border: Border.all( | ||
width: 1, | ||
), | ||
borderRadius: const BorderRadius.only( | ||
topLeft: Radius.circular(10), topRight: Radius.circular(10)), | ||
), | ||
children: [ | ||
buildHeaderCell('Symbol'), | ||
buildHeaderCell('Quantity'), | ||
buildHeaderCell('Entry Price'), | ||
buildHeaderCell('Liquidation Price'), | ||
buildHeaderCell('Margin'), | ||
buildHeaderCell('Leverage'), | ||
buildHeaderCell('Unrealized PnL'), | ||
buildHeaderCell('Expiry'), | ||
], | ||
), | ||
for (var position in positions) | ||
TableRow( | ||
children: [ | ||
buildTableCell(position.contractSymbol), | ||
buildTableCell(position.quantity.formatted()), | ||
buildTableCell(position.averageEntryPrice.formatted()), | ||
buildTableCell(position.liquidationPrice.formatted()), | ||
buildTableCell(position.collateral.formatted()), | ||
buildTableCell(position.leverage.formatted()), | ||
// TODO: we need to get the latest quote to be able to calculate this | ||
buildTableCell("0.0"), | ||
buildTableCell("${DateFormat('dd-MM-yyyy – kk:mm').format(position.expiry)} CET"), | ||
], | ||
), | ||
], | ||
), | ||
); | ||
} | ||
|
||
TableCell buildHeaderCell(String text) { | ||
return TableCell( | ||
child: Container( | ||
padding: const EdgeInsets.all(10), | ||
alignment: Alignment.center, | ||
child: Text(text, | ||
textAlign: TextAlign.center, | ||
style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.white)))); | ||
} | ||
|
||
TableCell buildTableCell(String text) => TableCell( | ||
child: Center( | ||
child: Container( | ||
padding: const EdgeInsets.all(10), alignment: Alignment.center, child: Text(text)))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'dart:convert'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:get_10101/common/http_client.dart'; | ||
import 'package:get_10101/common/model.dart'; | ||
|
||
class OpenPositionsService { | ||
const OpenPositionsService(); | ||
|
||
static Future<List<Position>> fetchOpenPositions() async { | ||
final response = await HttpClientManager.instance.get(Uri(path: '/api/positions')); | ||
|
||
if (response.statusCode == 200) { | ||
final List<dynamic> jsonData = jsonDecode(response.body); | ||
return jsonData.map((positionData) => Position.fromJson(positionData)).toList(); | ||
} else { | ||
throw FlutterError("Could not fetch positions"); | ||
} | ||
} | ||
} | ||
|
||
class Position { | ||
final Leverage leverage; | ||
final Usd quantity; | ||
final String contractSymbol; | ||
final String direction; | ||
final Usd averageEntryPrice; | ||
final Usd liquidationPrice; | ||
final String positionState; | ||
final Amount collateral; | ||
final DateTime expiry; | ||
final DateTime updated; | ||
final DateTime created; | ||
|
||
Position({ | ||
required this.leverage, | ||
required this.quantity, | ||
required this.contractSymbol, | ||
required this.direction, | ||
required this.averageEntryPrice, | ||
required this.liquidationPrice, | ||
required this.positionState, | ||
required this.collateral, | ||
required this.expiry, | ||
required this.updated, | ||
required this.created, | ||
}); | ||
|
||
factory Position.fromJson(Map<String, dynamic> json) { | ||
return Position( | ||
leverage: Leverage(json['leverage'] as double), | ||
quantity: Usd(json['quantity'] as double), | ||
contractSymbol: json['contract_symbol'] as String, | ||
direction: json['direction'] as String, | ||
averageEntryPrice: Usd(json['average_entry_price'] as double), | ||
liquidationPrice: Usd(json['liquidation_price'] as double), | ||
positionState: json['position_state'] as String, | ||
collateral: Amount(json['collateral']), | ||
expiry: DateTime.parse(json['expiry'] as String), | ||
updated: DateTime.parse(json['updated'] as String), | ||
created: DateTime.parse(json['created'] as String), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.