import 'dart:convert'; import 'dart:io'; import 'package:desktopapp/classes/routes.dart'; import 'package:desktopapp/utils/logger.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; class HomePage extends StatelessWidget { const HomePage({Key? key}) : super(key: key); Widget buildContent(BuildContext context, SharedPreferences prefs) { return Column(children: [ const Padding( padding: EdgeInsets.symmetric(vertical: 20), child: Text('Appuyez sur le bouton pour envoyer une alerte de test', style: TextStyle(fontSize: 20)), ), TextButton( onPressed: () async { Dio dio = Dio(BaseOptions( contentType: Headers.jsonContentType, responseType: ResponseType.json, validateStatus: (_) => true)); var accessToken = prefs.getString('credentials.access_token'); var data = jsonEncode({ 'type': 'donation', 'message': '*MrDev023* *Break* Test !', 'access_token': accessToken, 'special_text_color': '#ff00ff', 'image_href': 'https://postimgs.org/img/logo.png', 'sound_href': '' }); var response = await dio .post('https://streamlabs.com/api/v1.0/alerts', data: data); if (response.statusCode == HttpStatus.ok) { prefs.setString('credentials', response.data.toString()); Logger.log(LoggerType.info, this, 'Authorization success : ${response.data}'); showDialog( context: context, builder: (_) => AlertDialog( title: const Text('Info'), content: const Text('Envoie réussie'), actions: [ TextButton( onPressed: () { Navigator.pop(context); }, child: const Text('OK')) ], )); } else { Logger.log(LoggerType.error, this, 'Authorization error : ${response.data} with $data and ${response.requestOptions.headers}'); showDialog( context: context, builder: (_) => AlertDialog( title: const Text('Erreur'), content: const Text('Impossible de se connecter.'), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('OK')) ], )); } }, child: const Text('Envoyer une alerte de test')), ]); } @override Widget build(BuildContext context) { return Scaffold( body: FutureBuilder( future: SharedPreferences.getInstance(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasData) { final prefs = snapshot.data!; Future(() async { if (!prefs.containsKey('credentials.access_token')) { Navigator.of(context) .pushReplacementNamed(Routes.streamlabsTutorial.path); } }); return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Row( mainAxisAlignment: MainAxisAlignment.center, children: [buildContent(context, prefs)], ) ]); } else { return const Center(child: CircularProgressIndicator()); } })); } }