import 'dart:io'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:desktopapp/widgets/components/link.dart' as link; class Step3 extends StatelessWidget { const Step3({Key? key}) : super(key: key); Widget buildContent(BuildContext context, SharedPreferences prefs) { var clientId = prefs.getString('client_id'); return Column(children: [ const Padding( padding: EdgeInsets.symmetric(vertical: 20), child: Text('Étape 3:Authorisation de l\'application', style: TextStyle(fontSize: 20)), ), link.Link( uri: 'https://streamlabs.com/api/v1.0/authorize?redirect_uri=http://localhost:1234/&client_id=$clientId&response_type=code&scope=alerts.create', label: 'Cliquez ici pour vous connecter à Streamlabs', ) ]); } Future waitAuthorizationCallback() async { var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 1214); var request = await server.first; var uri = Uri.dataFromString(request.requestedUri.toString()); var query = uri.queryParameters; var code = query['code']; if (code != null) { var prefs = await SharedPreferences.getInstance(); prefs.setString('code', code); } request.response ..headers.contentType = ContentType.text ..write('You can close tab now') ..close(); // SEND TOKEN ASK TO API // POST https://streamlabs.com/api/v1.0/token // { // "grant_type": "authorization_code", // "code": "CODE", // "redirect_uri": "http://localhost:1234/", // "client_id": "CLIENT_ID", // "client_secret": "CLIENT_SECRET" // } // SAVE RESPONSE TOKEN AS CREDENTIALS IN SHARED PREFERENCES } @override Widget build(BuildContext context) { waitAuthorizationCallback(); return Column( children: [ Expanded( child: FutureBuilder( future: SharedPreferences.getInstance(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasData) { final prefs = snapshot.data!; return buildContent(context, prefs); } else { return const Center(child: CircularProgressIndicator()); } })), Padding( padding: const EdgeInsets.symmetric(vertical: 20), child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ TextButton( onPressed: () {}, child: const Text('Étape précédente')), TextButton(onPressed: () {}, child: const Text('Étape suivante')) ], ), ) ], ); } }