import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../providers/auth_provider.dart'; import 'dashboard_page.dart'; import 'register_page.dart'; class LoginPage extends StatefulWidget { const LoginPage({super.key}); @override State createState() => _LoginPageState(); } class _LoginPageState extends State { final _emailController = TextEditingController(); final _passwordController = TextEditingController(); final _formKey = GlobalKey(); Future login() async { if (!_formKey.currentState!.validate()) { return; } final auth = context.read(); final result = await auth.login( email: _emailController.text.trim(), password: _passwordController.text, ); if (!mounted) return; if (result == null) { Navigator.pushReplacement( context, MaterialPageRoute(builder: (_) => const DashboardPage()), ); } else { ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text(result))); } } @override Widget build(BuildContext context) { final auth = context.watch(); return Scaffold( body: SafeArea( child: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: Form( key: _formKey, child: Column( children: [ const Icon(Icons.air, size: 100, color: Colors.green), const SizedBox(height: 20), const Text( 'Smart Air Quality', style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold), ), const SizedBox(height: 50), TextFormField( controller: _emailController, decoration: const InputDecoration( labelText: 'Email', border: OutlineInputBorder(), ), validator: (value) { if (value == null || value.isEmpty) { return 'Email wajib diisi'; } return null; }, ), const SizedBox(height: 20), TextFormField( controller: _passwordController, obscureText: true, decoration: const InputDecoration( labelText: 'Password', border: OutlineInputBorder(), ), validator: (value) { if (value == null || value.isEmpty) { return 'Password wajib diisi'; } return null; }, ), const SizedBox(height: 30), SizedBox( width: double.infinity, height: 50, child: ElevatedButton( onPressed: auth.isLoading ? null : login, child: auth.isLoading ? const CircularProgressIndicator() : const Text('Login'), ), ), const SizedBox(height: 20), TextButton( onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (_) => const RegisterPage()), ); }, child: const Text('Belum punya akun? Register'), ), ], ), ), ), ), ), ); } }