-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.dart
165 lines (148 loc) · 4.44 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
import 'package:flutter/material.dart';
import 'package:discord_api/discord_api.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'webview_page.dart';
const clientId = 'CLIENT_ID';
const clientSecret = 'CLIENT_SECRET';
const redirectUri = 'http://localhost:8080/callback';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Discord API Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const MyHomePage(title: 'Discord API Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _discordClient = DiscordClient(
clientId: clientId,
clientSecret: clientSecret,
redirectUri: redirectUri,
discordHttpClient:
DiscordDioProvider(clientId: clientId, clientSecret: clientSecret),
);
final _flutterWebviewPlugin = FlutterWebviewPlugin();
void _urlListener(String url) {
if (url.startsWith(redirectUri)) {
final code = Uri.parse(url).queryParameters['code'];
if (code != null) {
_discordClient.getAccessToken(code).then((token) {
_flutterWebviewPlugin.close();
setState(() {});
});
}
}
}
Future<DiscordToken?> _openConnectionPage(
{List<DiscordApiScope> scopes = const []}) {
_flutterWebviewPlugin.onUrlChanged.listen(_urlListener);
_flutterWebviewPlugin.onDestroy.listen((_) => Navigator.pop(context));
final url = _discordClient.authorizeUri(scopes);
return Navigator.push(
context,
MaterialPageRoute(
builder: (context) => WebViewPage(url.toString()),
),
).then((_) {
if (_discordClient.discordHttpClient.discordToken != null) {
debugPrint(
"TOKEN: ${_discordClient.discordHttpClient.discordToken?.accessToken}");
return _discordClient.discordHttpClient.discordToken;
}
return null;
});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance?.scheduleFrameCallback((timeStamp) {
_openConnectionPage(
scopes: [
DiscordApiScope.identify,
DiscordApiScope.email,
DiscordApiScope.guilds
],
).then((value) => setState(() {}));
});
}
void _displayDataAlert({
String? method,
String? data,
bool isImg = false,
bool? isOnline,
}) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(method ?? ''),
content: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
if (isOnline != null)
Text(
isOnline ? 'Online' : 'Offline',
style: TextStyle(
color: isOnline ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
if (!isImg && data != null) Text(data),
if (isImg && data != null)
Image.network(
data,
loadingBuilder: (_, __, ___) =>
const CircularProgressIndicator(),
),
],
),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
children: <Widget>[
// Text(
// 'Welcome user: ${_discordClient.discordHttpClient.discordToken?.userId}',
// ),
Text(
'Your Discord access token is: ${_discordClient.discordHttpClient.discordToken?.accessToken}',
),
ElevatedButton(
onPressed: () async {
final user = await _discordClient.getCurrentUser();
_displayDataAlert(
method: 'getCurrentUser',
data: user.toString(),
);
},
child: const Text('Get Me'),
),
],
),
);
}
}