list traction and add leaderboard panel
This commit is contained in:
70
lib/components/dashboard/leaderboardPanel.dart
Normal file
70
lib/components/dashboard/leaderboardPanel.dart
Normal 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',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -8,52 +8,69 @@ class TopTractionPanel extends StatelessWidget {
|
||||
final data = context.watch<DataService>();
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
child: Card( child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text("Top Traction", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, decoration: TextDecoration.underline)),
|
||||
Column(
|
||||
children: List.generate(
|
||||
data.homepageStats?.topLocos.length ?? 0,
|
||||
(index) {
|
||||
final loco = data.homepageStats!.topLocos[index];
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
child: Card(
|
||||
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 8),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Card(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
"Top Traction",
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
Column(
|
||||
children: List.generate(data.homepageStats?.topLocos.length ?? 0, (
|
||||
index,
|
||||
) {
|
||||
final loco = data.homepageStats!.topLocos[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: [
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
TextSpan(text: '${index + 1}. ', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
TextSpan(text: '${loco.locoClass} ${loco.locoNumber}'),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
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.locoMileage?.toStringAsFixed(1)} mi'),
|
||||
],
|
||||
),
|
||||
),
|
||||
Text('${loco.locoName}', style: TextStyle(fontStyle: FontStyle.italic)),
|
||||
|
||||
],
|
||||
),
|
||||
Text('${loco.locoMileage?.toStringAsFixed(1)} mi'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
],
|
||||
)),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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()],
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
23
lib/components/pages/traction.dart
Normal file
23
lib/components/pages/traction.dart
Normal 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}'),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -33,21 +36,34 @@ class DataService extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> fetchLegs({
|
||||
int offset = 0,
|
||||
int limit = 100,
|
||||
String sortBy = 'date',
|
||||
int sortDirection = 0,
|
||||
}) async {
|
||||
final query = '?sort_direction=$sortDirection&sort_by=$sortBy&offset=$offset&limit=$limit';
|
||||
final json = await api.get('/user/legs$query');
|
||||
int offset = 0,
|
||||
int limit = 100,
|
||||
String sortBy = 'date',
|
||||
int sortDirection = 0,
|
||||
}) async {
|
||||
final query =
|
||||
'?sort_direction=$sortDirection&sort_by=$sortBy&offset=$offset&limit=$limit';
|
||||
final json = await api.get('/user/legs$query');
|
||||
|
||||
if (json is List) {
|
||||
_legs = json.map((e) => Leg.fromJson(e)).toList();
|
||||
notifyListeners();
|
||||
} else {
|
||||
throw Exception('Unexpected legs response: $json');
|
||||
if (json is List) {
|
||||
_legs = json.map((e) => Leg.fromJson(e)).toList();
|
||||
notifyListeners();
|
||||
} else {
|
||||
throw Exception('Unexpected legs response: $json');
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user