Skip to content

Commit

Permalink
First take at Integrate leaderboard #16
Browse files Browse the repository at this point in the history
  • Loading branch information
fredfalcon committed Apr 6, 2021
1 parent 8ce76e6 commit e4978f3
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 5 deletions.
29 changes: 29 additions & 0 deletions lib/data/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,32 @@ class Results {
screenshot: parsedJson['screenshot']);
}
}

class Leaders {
final String id;
final int rank;
final String user;
final int score;
final String image;
final int title;

Leaders({
required this.id,
required this.rank,
required this.user,
required this.score,
required this.image,
required this.title,
});

factory Leaders.fromJson(Map<String, dynamic> parsedJson) {
return Leaders(
id: parsedJson['id'].toString(),
rank: parsedJson['rank'] ?? 0,
user: parsedJson['User'] ?? "",
score: parsedJson['score']['total_score'] ?? 0,
image: parsedJson['image']['user_avatar'] ?? "",
title: parsedJson['title_type']['title'] ?? 0,
);
}
}
165 changes: 160 additions & 5 deletions lib/pages/leaderboard.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,172 @@
import 'package:bugheist/services/api.dart';
import 'package:flutter/material.dart';
import 'dart:async';

class LeaderBoard extends StatefulWidget {
@override
_LeaderBoardState createState() => _LeaderBoardState();
}

class _LeaderBoardState extends State<LeaderBoard> {
int i = 0;
Color my = Colors.brown;
late Future _getObj;

@override
void initState() {
var paginatedUrl = 'https://www.bugheist.com/api/v1/userscore/';
_getObj = ApiBackend().getLeaderData(paginatedUrl);
super.initState();
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text(
'Leaderboard Page'
),),
var r = TextStyle(color: Colors.purpleAccent, fontSize: 34);
return Stack(
children: <Widget>[
Scaffold(
body: Container(
margin: EdgeInsets.only(top: 65.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 15.0),
child: Text(
'Global Leaderboard ',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
Flexible(
child: FutureBuilder(
future: _getObj,
builder: (context, snapshot) {
print(snapshot);
if (snapshot.data != null) {
final list = snapshot.data as List;
i = 0;
return ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 5.0, vertical: 5.0),
child: InkWell(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: i == 0
? Colors.amber
: i == 1
? Colors.grey
: i == 2
? Colors.brown
: Colors.white,
width: 3.0,
style: BorderStyle.solid),
borderRadius:
BorderRadius.circular(5.0)),
width: MediaQuery.of(context).size.width,
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
top: 10.0, left: 15.0),
child: Row(
children: <Widget>[
CircleAvatar(
child: Container(
decoration:
BoxDecoration(
shape:
BoxShape.circle,
image: DecorationImage(
image: NetworkImage(
"https://bhfiles.storage.googleapis.com/" +
list[index]
.image),
fit: BoxFit.fill),
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(
left: 20.0, top: 10.0),
child: Column(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Container(
alignment:
Alignment.centerLeft,
child: Text(
list[index].user,
style: TextStyle(
color: Colors
.deepPurple,
fontWeight:
FontWeight
.w500),
maxLines: 6,
),
),
Text("Points: " +
list[index]
.score
.toString()),
],
),
),
Flexible(child: Container()),
i == 0
? Text("🥇", style: r)
: i == 1
? Text(
"🥈",
style: r,
)
: i == 2
? Text(
"🥉",
style: r,
)
: Text(''),
Padding(
padding: EdgeInsets.only(
left: 20.0,
top: 13.0,
right: 20.0),
),
],
),
],
),
),
),
);
});
} else {
return Center(
child: CircularProgressIndicator(),
);
}
}))
],
),
)),
],
);
}
}
}
13 changes: 13 additions & 0 deletions lib/services/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ class ApiBackend {
return data;
});
}

Future<List> getLeaderData(String paginatedUrl) async {
return http
.get(
Uri.parse(paginatedUrl),
)
.then((http.Response response) {
List<Leaders> leaders = (json.decode(response.body) as List)
.map((data) => Leaders.fromJson(data))
.toList();
return leaders;
});
}
}

0 comments on commit e4978f3

Please sign in to comment.