import 'dart:convert'; import 'main.dart'; import 'package:flutter/cupertino.dart'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'Timetable.dart'; import 'Login.dart'; Future getToken( String user, String password, String otp, String devId) async { var uri = Uri.http("localhost:3000", "/login"); String body = '{"user":"$user", "password": "$password", "otp": "$otp", "devid": "$devId"}'; print(uri); final response = await http.post(uri, body: body); if (response.statusCode == 200) { return jsonDecode(utf8.decode(response.bodyBytes))['token']; } else if(response.statusCode == 401) { var body = response.body; Fluttertoast.showToast( msg: "Fehler: $body", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, timeInSecForIosWeb: 1, backgroundColor: Colors.red, textColor: Colors.white, fontSize: 16.0 ); throw Exception('Failed to log in'); } else { throw Exception('Undefined error'); } } Future getUserInfo( String user, String password, String otp, String devId) async { var uri = Uri.http("localhost:3000", "/api/userinfo"); String body = '{"user":"$user", "password": "$password", "otp": "$otp", "devid": "$devId"}'; print(uri); final response = await http.post(uri, body: body); if (response.statusCode == 200) { return utf8.decode(response.bodyBytes); } else { throw Exception('Failed to log in'); } } Future fetchClassTimetable() async { SharedPreferences prefs = await SharedPreferences.getInstance(); var class_num; print(prefs.getString('class_num')); if (prefs.getString('class_num') != null) { class_num = prefs.getString('class_num')!; } else { class_num = '07_1'; } var api_key = prefs.getString('api_key'); var uri = Uri.https("mein.cantorgymnasium.de", "/api/timetable/$class_num"); var headers = {"x-api-key": "$api_key"}; print(uri); final response = http.get(uri, headers: headers); return response; } Widget buildClassTimetable() { return FutureBuilder( future: fetchClassTimetable(), builder: (context, snapshot) { if (snapshot.hasData) { int statusCode = snapshot.data!.statusCode; if (statusCode == 200) { ListView timetableView = ClassTimetableBuilder.buildView( jsonDecode(utf8.decode(snapshot.data!.bodyBytes))) .view; return timetableView; } else if(statusCode == 400) { Navigator.push(context, MaterialPageRoute(builder: (context) => Login())); } return Center(child: Text('Error $statusCode')); } else if (snapshot.hasError) { return Text('$snapshot.error'); } else { return Center(child: CircularProgressIndicator()); } }, ); } Future fetchClassesList() async { SharedPreferences prefs = await SharedPreferences.getInstance(); /* var class_num; print(prefs.getString('class_num')); if(prefs.getString('class_num') != null){ class_num = prefs.getString('class_num')!; } else { class_num = '07_1'; } */ // await prefs.setString('api_key', "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJHZW9yZy1DYW50b3ItR3ltbmFzaXVtIEhhbGxlKFNhYWxlKSIsInVzZXIiOiJkZW55cy5rb25vdmFsb3ZAcG0ubWUiLCJyb2xlcyI6WyJTdHVkZW50IiwiQWRtaW4iXSwiYmxhY2tsaXN0IjpbIi9jbGFzc2VzIl0sIndoaXRlbGlzdCI6WyIvaGVsbG8vc2Vuc2l0aXZlIl0sImppZCI6IkFwcERldiBBbHBoYSBkdW1teSBrZXlAMDIvMDgvMjAyMSAxOTowODowOSIsImV4cCI6MTY1OTQ2NzI4OX0.a7Q83PK3ybeV7Bui-_rX1o6IZx1cNa6vsvUGG-kfqtc"); var api_key = prefs.getString('api_key'); var uri = Uri.https("mein.cantorgymnasium.de", "/api/classes"); var headers = {"x-api-key": "$api_key"}; print(uri); final response = http.get(uri, headers: headers); return response; } Widget buildClassesChooser() { return FutureBuilder( future: fetchClassesList(), builder: (context, snapshot) { if (snapshot.hasData) { int statusCode = snapshot.data!.statusCode; if (statusCode == 200) { // List children = []; //ClassTimetable.fromJson(jsonDecode(utf8.decode(snapshot.data!.bodyBytes))).timetable.forEach((element) { //}); List items = []; jsonDecode(utf8.decode(snapshot.data!.bodyBytes)).forEach((value) { items.add(value..toString()); }); return ClassesChooser(items: items); } return Text('$statusCode'); } else if (snapshot.hasError) { return Text('$snapshot.error'); } else { return Center(child: CircularProgressIndicator()); } }, ); } class ClassesChooser extends StatefulWidget { final List items; const ClassesChooser({Key? key, required this.items}) : super(key: key); @override State createState() => _ClassesChooserState(items); } class _ClassesChooserState extends State { final List items; //final String dropdownValue; var dropdownValue; _ClassesChooserState(this.items); @override Widget build(BuildContext context) { // var dropdown_items = return DropdownButtonFormField( // value: dropdownValue, /*icon: const Icon(Icons.arrow_downward), iconSize: 24, elevation: 16,*/ // style: const TextStyle(color: Color(0xFFFFBC3B)), /*underline: Container( height: 2, color: Color(0xFFFFBC3B), ) */ decoration: InputDecoration( icon: Icon(CupertinoIcons.number), border: OutlineInputBorder(), labelText: 'Klasse (05_1, 07_3, 10_2...)', ), // icon: Icon(CupertinoIcons.number), onChanged: (String? newValue) { setState(() async { dropdownValue = newValue!; SharedPreferences prefs = await SharedPreferences.getInstance(); String class_num = newValue; print('Set new class to $class_num'); await prefs.setString('class_num', class_num); }); }, items: items.map>((String value) { return DropdownMenuItem( value: value, child: Text(value), ); }).toList(), ); } }