list traction and add leaderboard panel

This commit is contained in:
2025-07-25 17:36:26 +01:00
parent 985eddb35a
commit 3bef606d41
7 changed files with 191 additions and 66 deletions

View File

@@ -0,0 +1,70 @@
import 'package:flutter/material.dart';
import 'package:mileograph_flutter/services/dataService.dart';
import 'package:provider/provider.dart';
class LeaderboardPanel extends StatelessWidget {
Widget build(BuildContext context) {
final data = context.watch<DataService>();
return Padding(
padding: const EdgeInsets.all(10.0),
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Leaderboard",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
),
),
Column(
children: List.generate(
data.homepageStats?.leaderboard.length ?? 0,
(index) {
final leaderboardEntry =
data.homepageStats!.leaderboard[index];
return Container(
width: double.infinity,
child: Container(
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 8),
child: Padding(
padding: EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text.rich(
TextSpan(
children: [
TextSpan(
text: '${index + 1}. ',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: '${leaderboardEntry.userFullName}',
),
],
),
),
Text(
'${leaderboardEntry.mileage.toStringAsFixed(1)} mi',
),
],
),
),
),
);
},
),
),
],
),
),
);
}
}

View File

@@ -8,21 +8,29 @@ class TopTractionPanel extends StatelessWidget {
final data = context.watch<DataService>();
return Padding(
padding: const EdgeInsets.all(10.0),
child: Card( child: Column(
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Top Traction", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, decoration: TextDecoration.underline)),
Text(
"Top Traction",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
),
),
Column(
children: List.generate(
data.homepageStats?.topLocos.length ?? 0,
(index) {
children: List.generate(data.homepageStats?.topLocos.length ?? 0, (
index,
) {
final loco = data.homepageStats!.topLocos[index];
return Container(
width: double.infinity,
child: Card(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 8),
child: Padding(
padding: EdgeInsets.all(16),
padding: EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
@@ -33,13 +41,23 @@ class TopTractionPanel extends StatelessWidget {
Text.rich(
TextSpan(
children: [
TextSpan(text: '${index + 1}. ', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: '${loco.locoClass} ${loco.locoNumber}'),
TextSpan(
text: '${index + 1}. ',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
TextSpan(
text:
'${loco.locoClass} ${loco.locoNumber}',
),
],
),
),
Text('${loco.locoName}', style: TextStyle(fontStyle: FontStyle.italic)),
Text(
'${loco.locoName}',
style: TextStyle(fontStyle: FontStyle.italic),
),
],
),
Text('${loco.locoMileage?.toStringAsFixed(1)} mi'),
@@ -48,12 +66,11 @@ class TopTractionPanel extends StatelessWidget {
),
),
);
},
}),
),
)
],
)),
),
),
);
}
}

View File

@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:mileograph_flutter/components/dashboard/leaderboardPanel.dart';
import 'package:mileograph_flutter/services/authservice.dart';
import 'package:mileograph_flutter/services/dataService.dart';
import 'package:mileograph_flutter/components/dashboard/topTractionPanel.dart';
@@ -84,7 +85,7 @@ class DashboardHeader extends StatelessWidget {
Expanded(
child: ListView(
scrollDirection: Axis.vertical,
children: [TopTractionPanel()],
children: [TopTractionPanel(), LeaderboardPanel()],
),
),
],

View File

@@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
import 'package:mileograph_flutter/objects/objects.dart';
import 'package:provider/provider.dart';
import 'package:mileograph_flutter/services/dataService.dart';
class TractionPage extends StatelessWidget {
Widget build(BuildContext context) {
final data = context.watch<DataService>();
return ListView.builder(
itemCount: data.traction.length,
itemBuilder: (context, index) {
final loco = data.traction[index];
return Card(
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Padding(
padding: EdgeInsets.all(16),
child: Text('${loco.locoClass} ${loco.locoNumber}'),
),
);
},
);
}
}

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:mileograph_flutter/components/pages/traction.dart';
import 'package:provider/provider.dart';
import 'package:mileograph_flutter/components/pages/legs.dart';
@@ -125,7 +126,7 @@ class _MyHomePageState extends State<MyHomePage> {
Dashboard(),
Center(child: Text("Calculator Page")),
LegsPage(),
Center(child: Text("Traction Page")),
TractionPage(),
Center(child: Text("Trips Page")),
];
@@ -148,6 +149,9 @@ class _MyHomePageState extends State<MyHomePage> {
if (data.legs.isEmpty) {
data.fetchLegs();
}
if (data.traction.isEmpty) {
data.fetchHadTraction();
}
});
}
}
@@ -162,12 +166,7 @@ class _MyHomePageState extends State<MyHomePage> {
if (data.homepageStats != null) {
currentPage = contentPages[pageIndex];
} else {
currentPage = Center(
child: FilledButton(
onPressed: data.fetchHomepageStats,
child: CircularProgressIndicator(),
),
);
currentPage = Center(child: CircularProgressIndicator());
}
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.

View File

@@ -105,7 +105,7 @@ class LocoSummary {
locoType: json['loco_type'],
locoClass: json['loco_class'],
locoNumber: json['loco_number'],
locoName: json['loco_name'],
locoName: json['loco_name'] ?? "",
locoOperator: json['loco_operator'],
locoNotes: json['loco_notes'],
locoEvn: json['loco_evn'],
@@ -224,4 +224,3 @@ class Leg {
.toList(),
);
}

View File

@@ -13,6 +13,9 @@ class DataService extends ChangeNotifier {
List<Leg> _legs = [];
List<Leg> get legs => _legs;
List<LocoSummary> _traction = [];
List<LocoSummary> get traction => _traction;
bool _isHomepageLoading = false;
bool get isHomepageLoading => _isHomepageLoading;
@@ -38,7 +41,8 @@ class DataService extends ChangeNotifier {
String sortBy = 'date',
int sortDirection = 0,
}) async {
final query = '?sort_direction=$sortDirection&sort_by=$sortBy&offset=$offset&limit=$limit';
final query =
'?sort_direction=$sortDirection&sort_by=$sortBy&offset=$offset&limit=$limit';
final json = await api.get('/user/legs$query');
if (json is List) {
@@ -49,6 +53,18 @@ class DataService extends ChangeNotifier {
}
}
Future<void> fetchHadTraction({int offset = 0, int limit = 100}) async {
final query = '?offset=$offset&limit=$limit';
final json = await api.get('/loco/mileage$query');
if (json is List) {
_traction = json.map((e) => LocoSummary.fromJson(e)).toList();
notifyListeners();
} else {
throw Exception('Unexpected traction response: $json');
}
}
void clear() {
_homepageStats = null;
notifyListeners();