import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:repertory/constants.dart' as constants; import 'package:repertory/models/auth.dart'; class AuthScreen extends StatefulWidget { final String title; const AuthScreen({super.key, required this.title}); @override State createState() => _AuthScreenState(); } class _AuthScreenState extends State { bool _enabled = true; final _passwordController = TextEditingController(); final _userController = TextEditingController(); @override Widget build(context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Consumer( builder: (context, auth, _) { if (auth.authenticated) { Navigator.of(context).pushReplacementNamed('/'); return SizedBox.shrink(); } return Center( child: Card( child: Padding( padding: const EdgeInsets.all(constants.padding), child: SizedBox( width: 400, child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text( 'Logon to Repertory Portal', textAlign: TextAlign.center, style: Theme.of(context).textTheme.titleLarge, ), const SizedBox(height: constants.padding), TextField( decoration: InputDecoration(labelText: 'Username'), controller: _userController, ), const SizedBox(height: constants.padding), TextField( obscureText: true, decoration: InputDecoration(labelText: 'Password'), controller: _passwordController, ), const SizedBox(height: constants.padding), ElevatedButton( onPressed: _enabled ? () async { setState(() => _enabled = false); await auth.authenticate( _userController.text, _passwordController.text, ); setState(() => _enabled = true); } : null, child: const Text('Login'), ), ], ), ), ), ), ); }, ), ); } @override void setState(VoidCallback fn) { if (!mounted) { return; } super.setState(fn); } }