This repository was archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathstats_page.dart
More file actions
156 lines (146 loc) · 5.34 KB
/
stats_page.dart
File metadata and controls
156 lines (146 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mentorship_client/remote/models/task.dart';
import 'package:mentorship_client/screens/home/pages/stats/bloc/bloc.dart';
import 'package:mentorship_client/widgets/loading_indicator.dart';
import 'dart:async';
import 'package:mentorship_client/auth/bloc.dart';
/// First page from the left on the HomeScreen. Displays welcome message to the user
/// and provides some information on latest achievements.
///
class StatsPage extends StatefulWidget {
@override
_StatsPageState createState() => _StatsPageState();
}
class _StatsPageState extends State<StatsPage> {
Completer<void> _refreshCompleter;
@override
void initState() {
super.initState();
_refreshCompleter = Completer<void>();
}
@override
Widget build(BuildContext context) {
return BlocConsumer<StatsPageBloc, StatsPageState>(
listener: (context, state) {
if (state is StatsPageShowed) {
_refreshCompleter?.complete();
_refreshCompleter = Completer();
}
},
builder: (context, state) {
return BlocBuilder<StatsPageBloc, StatsPageState>(builder: (context, state) {
if (state is StatsPageSuccess) {
return RefreshIndicator(
onRefresh: () {
BlocProvider.of<StatsPageBloc>(context).add(
StatsPageRefresh(),
);
return _refreshCompleter.future;
},
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 8),
child: ListView(
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Text(
"Welcome, ${state.homeStats.name}!",
textScaleFactor: 2,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
Column(
children: [
_buildRow("Pending Requests", state.homeStats.pendingRequests),
_buildRow("Accepted Requests", state.homeStats.acceptedRequests),
_buildRow("Rejected Requests", state.homeStats.rejectedRequests),
_buildRow("Completed Relations", state.homeStats.completedRelations),
Padding(
padding: const EdgeInsets.fromLTRB(0, 24, 0, 12),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"Recent Achievements",
style: Theme.of(context).textTheme.headline6,
)),
),
for (Task achievement in state.homeStats.achievements)
Column(
children: <Widget>[
Row(
children: [
Checkbox(
value: true,
onChanged: (status) => null,
hoverColor: Theme.of(context).accentColor,
),
Text(achievement.description),
],
),
Divider(),
],
),
],
)
],
),
),
);
}
if (state is StatsPageFailure) {
if (state.message == "The token has expired! Please, login again or refresh it.") {
Future.delayed(Duration.zero, () => _showTokenExpiredDialog(context));
return Text(state.message);
} else {
return Text(state.message);
}
}
if (state is StatsPageLoading) {
return LoadingIndicator();
} else
return Text("an error occurred");
});
},
);
}
Widget _buildRow(String text, int count) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
text,
style: Theme.of(context).textTheme.headline6.apply(color: Colors.grey[600]),
),
Text(
count.toString(),
style: Theme.of(context).textTheme.subtitle2.copyWith(fontWeight: FontWeight.bold),
),
],
),
);
}
}
void _showTokenExpiredDialog(BuildContext context) {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return AlertDialog(
title: Text("Token expired"),
content: Text("Your session has expired! Please login again to access the Mentorship System features."),
actions: <Widget>[
FlatButton(
child: Text('Relogin'),
onPressed: () {
BlocProvider.of<AuthBloc>(context).add(JustLoggedOut());
Navigator.of(context).pop();
},
),
],
);
},
);
}