import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:productivity_measure_app/screens/dashboard.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../utils/SharedPreferenceHelper.dart'; class Login extends StatefulWidget { const Login({super.key}); @override State createState() => _LoginState(); } class _LoginState extends State { bool _obscureText = true; final TextEditingController _emailController = TextEditingController(); final TextEditingController _passwordController = TextEditingController(); final _formKey = GlobalKey(); bool isLoading = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Login'), ), body: Center( child: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset( 'assets/images/pro_icon_2.png', fit: BoxFit.fitWidth, // Adjust width to 80% of screen width height: 150.0, // Adjust the height as needed to match width ), SizedBox(height: 16.0), TextField( controller: _emailController, decoration: InputDecoration( labelText: 'User', border: OutlineInputBorder(), prefixIcon: Icon(Icons.person), ), ), SizedBox(height: 16.0), TextField( controller: _passwordController, decoration: InputDecoration( labelText: 'Password', border: OutlineInputBorder(), prefixIcon: Icon(Icons.lock), suffixIcon: IconButton( icon: Icon( _obscureText ? Icons.visibility : Icons.visibility_off, ), onPressed: () { setState(() { _obscureText = !_obscureText; }); }, ), ), obscureText: _obscureText, ), SizedBox(height: 24.0), SizedBox( width: double.infinity, child: ElevatedButton( onPressed: () { // Handle login logic here }, style: ElevatedButton.styleFrom( backgroundColor: Colors.lightGreen, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(4.0), ), minimumSize: Size(double.infinity, 48.0), ), child: Text( 'Login', style: TextStyle(color: Colors.white), ), ), ), ], ), ), ), ), ); } Future _login() async { if (_formKey.currentState!.validate()) { String email = _emailController.text.trim(); print(email); String password = _passwordController.text.trim(); print(password); setState(() { isLoading = true; }); try { final url = Uri.parse( 'https://cosmos.utopiadeals.com/cosmos/rest/application/authenticate-user'); final response = await http.post( url, headers: {"Content-Type": "application/json"}, body: jsonEncode({ "username": email, "password": password, }), ); if (response.statusCode == 200) { print('Login Successful!'); final Map jsonResponse = jsonDecode(response.body); SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setBool('isLoggedIn', true); // Deserialize JSON into DriverResponse object //final driverResponse = DriverResponse.fromJson(jsonResponse); //print('Trucker Name: ${driverResponse.truckerName}'); //await SharedPreferenceHelper.setPreferenceObject("driver_data", driverResponse.toJson()); //print("-----------------------------------"); /*DriverResponse? userResponse = await SharedPreferenceHelper.getPreferenceObject( "driver_data", (json) => DriverResponse.fromJson(json), );*/ //print(userResponse?.truckerName); await Future.delayed(const Duration(seconds: 2)); Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => Dashboard()), (Route route) => false, // This condition removes all previous routes ); } else { print('Login Failed! Status code: ${response.statusCode}'); } } catch (e) { // If an error occurs print('Exception: $e'); } finally { setState(() { isLoading = false; }); } } } }