-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.dart
208 lines (194 loc) · 5.74 KB
/
main.dart
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import 'dart:async';
import 'package:example/examples/authentication.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:strava_client/strava_client.dart';
import 'secret.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Strava Flutter',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: StravaFlutterPage(),
);
}
}
class StravaFlutterPage extends StatefulWidget {
@override
_StravaFlutterPageState createState() => _StravaFlutterPageState();
}
class _StravaFlutterPageState extends State<StravaFlutterPage> {
final TextEditingController _textEditingController = TextEditingController();
final DateFormat dateFormatter = DateFormat("HH:mm:ss");
late final StravaClient stravaClient;
bool isLoggedIn = false;
TokenResponse? token;
@override
void initState() {
stravaClient = StravaClient(secret: secret, clientId: clientId);
super.initState();
}
FutureOr<Null> showErrorMessage(dynamic error, dynamic stackTrace) {
if (error is Fault) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Did Receive Fault"),
content: Text(
"Message: ${error.message}\n-----------------\nErrors:\n${(error.errors ?? []).map((e) => "Code: ${e.code}\nResource: ${e.resource}\nField: ${e.field}\n").toList().join("\n----------\n")}"),
);
});
}
}
void testAuthentication() {
ExampleAuthentication(stravaClient).testAuthentication(
[
AuthenticationScope.profile_read_all,
AuthenticationScope.read_all,
AuthenticationScope.activity_read_all
],
"stravaflutter://redirect",
).then((token) {
setState(() {
isLoggedIn = true;
this.token = token;
});
_textEditingController.text = token.accessToken;
}).catchError(showErrorMessage);
}
void testDeauth() {
ExampleAuthentication(stravaClient).testDeauthorize().then((value) {
setState(() {
isLoggedIn = false;
this.token = null;
_textEditingController.clear();
});
}).catchError(showErrorMessage);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Strava Plugin"),
actions: [
Icon(
isLoggedIn
? Icons.radio_button_checked_outlined
: Icons.radio_button_off,
color: isLoggedIn ? Colors.white : Colors.red,
),
SizedBox(
width: 8,
)
],
),
body: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [_login(), _apiGroups()],
),
),
);
}
Widget _login() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
child: Text("Login With Strava"),
onPressed: testAuthentication,
),
ElevatedButton(
child: Text("De Authorize"),
onPressed: testDeauth,
)
],
),
SizedBox(
height: 8,
),
TextField(
minLines: 1,
maxLines: 3,
controller: _textEditingController,
decoration: InputDecoration(
border: OutlineInputBorder(),
label: Text("Access Token"),
suffixIcon: TextButton(
child: Text("Copy"),
onPressed: () {
Clipboard.setData(
ClipboardData(text: _textEditingController.text))
.then((value) =>
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Copied!"),
)));
},
)),
),
Divider()
],
);
}
Widget _apiGroups() {
return IgnorePointer(
ignoring: !isLoggedIn,
child: AnimatedOpacity(
opacity: isLoggedIn ? 1.0 : 0.4,
duration: Duration(milliseconds: 200),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
title: Text("Athletes"),
trailing: Icon(Icons.chevron_right),
),
ListTile(
title: Text("Clubs"),
trailing: Icon(Icons.chevron_right),
),
ListTile(
title: Text("Gears"),
trailing: Icon(Icons.chevron_right),
),
ListTile(
title: Text("Routes"),
trailing: Icon(Icons.chevron_right),
),
ListTile(
title: Text("Running Races"),
trailing: Icon(Icons.chevron_right),
),
ListTile(
title: Text("Segment Efforts"),
trailing: Icon(Icons.chevron_right),
),
ListTile(
title: Text("Segments"),
trailing: Icon(Icons.chevron_right),
),
ListTile(
title: Text("Streams"),
trailing: Icon(Icons.chevron_right),
),
ListTile(
title: Text("Uploads"),
trailing: Icon(Icons.chevron_right),
)
],
),
),
);
}
}