// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, file_names, use_build_context_synchronously, library_private_types_in_public_api import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:smilet/SignUpPage/CreateUserPass.dart'; import 'dart:async'; import 'package:http/http.dart' as http; bool schoolExists = false; bool userExists = false; Map schoolDetails = {}; // "Future is a core Dart class for working with async operations." // Kolla om en skola är registrerad och hämta dess information Future checkSchool(String code) async { var response = await http .get(Uri.parse('https://group-15-3.pvt.dsv.su.se/school?code=$code')); if (response.statusCode == 200) { // If the server did return a 200 OK response, // then parse the JSON. schoolExists = true; schoolDetails = jsonDecode(utf8.decode(response.bodyBytes)) as Map; } else { // If the server did not return a 200 OK response, // then throw an exception. schoolExists = false; schoolDetails = jsonDecode(utf8.decode(response.bodyBytes)) as Map; } } // Kolla om en användare redan är registrerad med anmälningskoden Future checkUser(String id) async { var response = await http.get(Uri.parse('https://group-15-3.pvt.dsv.su.se/user/$id')); if (response.statusCode == 200) { // If the server did return a 200 OK response, // then parse the JSON. userExists = true; } else { // If the server did not return a 200 OK response, // then throw an exception. userExists = false; } } class SignUpPage extends StatefulWidget { const SignUpPage({ super.key, }); @override _SignUpPageState createState() => _SignUpPageState(); } class _SignUpPageState extends State { final _formKey = GlobalKey(); TextEditingController regNumController = TextEditingController(); TextEditingController schoolCodeController = TextEditingController(); int regNumber = 0; String schoolCode = ""; @override Widget build(BuildContext context) { final textHeadStyle = TextStyle(fontSize: 40.0, fontWeight: FontWeight.bold); return Scaffold( appBar: AppBar( centerTitle: true, title: Image.asset( 'assets/images/ml_logo.png', height: 40, ), ), // Form för att kunna använda TextFormField body: Form( key: _formKey, child: SingleChildScrollView( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Padding( padding: const EdgeInsets.only(bottom: 20.0), child: Text('Skapa konto', textAlign: TextAlign.center, style: textHeadStyle), ), Padding( padding: const EdgeInsets.all(8.0), child: Container( alignment: Alignment.center, child: SizedBox( width: 300, height: 100, child: TextFormField( maxLength: 8, onChanged: checkUser, controller: regNumController, keyboardType: TextInputType.number, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Midnattsloppet anmälningskod', hintText: 'Ange Midnattsloppet anmälningskod', ), // Kolla numrets giltighet validator: (String? value) { if (value == null) { return "Anmälningskod får inte vara tom!"; } else if (!isValidRegNumber(value)) { return 'Ogiltig anmälningskod!'; } else if (userExists) { return 'Användare med kod $value finns redan!'; } else { return null; } }, ), ), ), ), Padding( padding: const EdgeInsets.all(8.0), child: Container( alignment: Alignment.center, child: SizedBox( width: 300, height: 100, child: TextFormField( maxLength: 16, onChanged: checkSchool, controller: schoolCodeController, keyboardType: TextInputType.text, decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Skolinstitutionskod', hintText: 'Ange skolinstitutionskod', ), validator: (String? value) { return (value != null && isValidSchoolCode(value)) ? null : 'Skola ej registrerad!'; }, ), ), ), ), Padding( padding: const EdgeInsets.all(10.0), child: ElevatedButton( style: TextButton.styleFrom( backgroundColor: Color.fromARGB(255, 10, 179, 74), ), onPressed: () { if (_formKey.currentState!.validate()) { // Spara uppgifterna regNumber = int.parse(regNumController.text); schoolCode = schoolCodeController.text; ScaffoldMessenger.of(context).showSnackBar( SnackBar( duration: Duration(seconds: 1), behavior: SnackBarBehavior.floating, backgroundColor: Color.fromARGB(255, 12, 148, 64), content: Text( "Uppgifter giltiga!", style: TextStyle(color: Colors.white), )), ); Navigator.push( context, MaterialPageRoute( builder: (context) => CreateLoginPage( regNumber: regNumber, schoolCode: schoolCode, schoolDetails: schoolDetails, ))); } }, child: const Text('Fortsätt', style: TextStyle(color: Colors.white))), ), ], ), ))); } // Hjälparmetod för att avgöra om ett anmälningsnummer är giltigt bool isValidRegNumber(String value) { try { int number = int.parse(value); checkUser(value); return (number > 1000); } on Exception { return false; } } // Hjälparmetod för att avgöra om en skolkod är giltig bool isValidSchoolCode(String code) { try { checkSchool(code); // Om skola ej finns, ej valid kod return schoolExists; } on Exception { return false; } } }