import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'networking.dart'; import 'Dashboard.dart'; Future checkKey() async { SharedPreferences prefs = await SharedPreferences.getInstance(); String? api_key = await prefs.getString('api_key'); return api_key != null && api_key.isNotEmpty; } class Login extends StatelessWidget { final userController = TextEditingController(); final passwordController = TextEditingController(); final otpController = TextEditingController(); final devIdController = TextEditingController(); @override Widget build(BuildContext context) { return LayoutBuilder( builder: (context, constraints) { double widgetWidth = constraints.maxWidth; double widgetHeight = constraints.maxHeight; var factor; if (widgetWidth <= 600) { factor = 1; } else if (widgetWidth <= 1400) { factor = 2; } else if (widgetWidth <= 2000) { factor = 3; } return Scaffold( appBar: AppBar( title: Text("Anmelden"), ), body: Center( child: SingleChildScrollView( child: Padding( padding: EdgeInsets.fromLTRB(20, 20, 20, 20), child: Container( constraints: BoxConstraints( maxWidth: MediaQuery.of(context).size.width / factor, ), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset("assets/images/meincantor_r.png", height: 192, width: 192), Divider(), AutofillGroup( child: Column( children: [ TextField( autofillHints: [AutofillHints.username], decoration: InputDecoration( icon: Icon(CupertinoIcons.person), border: OutlineInputBorder(), labelText: 'Benutzername', ), controller: userController, ), Divider(), TextField( autofillHints: [AutofillHints.password], decoration: InputDecoration( icon: Icon(CupertinoIcons.lock), border: OutlineInputBorder(), labelText: 'Passwort', ), obscureText: true, controller: passwordController, ), ], )), Divider(), TextField( decoration: InputDecoration( icon: Icon(CupertinoIcons.lock), border: OutlineInputBorder(), labelText: '2F2-Code (OTP) [falls aktiviert]', ), obscureText: true, controller: otpController, ), Divider(), TextField( decoration: InputDecoration( icon: Icon(CupertinoIcons.device_laptop), border: OutlineInputBorder(), labelText: 'Gerätebezeichnung', ), controller: devIdController, ), Divider(), OutlinedButton( onPressed: () async { SharedPreferences prefs = await SharedPreferences.getInstance(); String api_key = await getToken( userController.text, passwordController.text, otpController.text, devIdController.text); print('Set new API key to $api_key'); await prefs.setString('api_key', api_key); dynamic userinfo = jsonDecode( await getUserInfo( userController.text, passwordController.text, otpController.text, devIdController.text)); await prefs.setString( 'user', userinfo['preferred_username']); await prefs.setString( 'name', userinfo['name']); if (prefs.getString('api_key') != null && prefs .getString('api_key')! .isNotEmpty) { Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) => Dashboard()), ); } }, child: Text("Anmelden")) ], ), ) ) ) ) ); }, ); } }