Created
May 13, 2024 12:01
-
-
Save TasyaShark/2288226915cf9819bd32871081bf8597 to your computer and use it in GitHub Desktop.
LoginScreen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginScreen extends StatefulWidget { | |
const LoginScreen({super.key}); | |
@override | |
State<StatefulWidget> createState() => LoginScreenState(); | |
} | |
class LoginScreenState extends State<LoginScreen> { | |
final TextEditingController _loginEditingController = TextEditingController(); | |
final TextEditingController _passwordEditingController = | |
TextEditingController(); | |
bool _isLoginContinues = false; | |
@override | |
Widget build(BuildContext context) { | |
if (_isLoginContinues) { | |
return Scaffold( | |
body: Center( | |
child: Container( | |
margin: const EdgeInsets.only(left: 8), | |
height: 18, | |
width: 18, | |
child: const CircularProgressIndicator( | |
strokeWidth: 2, | |
), | |
), | |
), | |
); | |
} | |
return Scaffold( | |
body: Center( | |
child: SingleChildScrollView( | |
child: Container( | |
padding: const EdgeInsets.symmetric(horizontal: 16.0), | |
child: ConstrainedBox( | |
constraints: const BoxConstraints(maxWidth: 400), | |
child: Column( | |
children: <Widget>[ | |
TextField( | |
keyboardType: TextInputType.text, | |
controller: _loginEditingController, | |
decoration: const InputDecoration(labelText: 'Login'), | |
), | |
TextField( | |
keyboardType: TextInputType.visiblePassword, | |
controller: _passwordEditingController, | |
decoration: const InputDecoration(labelText: 'Password'), | |
obscureText: true, | |
enableSuggestions: false, | |
autocorrect: false, | |
onSubmitted: (_) { | |
_login(); | |
}, | |
), | |
Container( | |
margin: const EdgeInsets.only(top: 8), | |
child: ElevatedButton( | |
onPressed: _login, | |
child: const Text('Login'), | |
), | |
) | |
], | |
), | |
)), | |
), | |
), | |
); | |
} | |
void _login() { | |
var userToLogin = CubeUser() | |
..login = _loginEditingController.text | |
..password = _passwordEditingController.text; | |
setState(() { | |
_isLoginContinues = true; | |
}); | |
createSession(userToLogin).then((cubeSession) async { | |
print("createSession cubeSession: $cubeSession"); | |
CubeChatConnection.instance | |
.login(userToLogin..id = cubeSession.user!.id) | |
.then((cubeUser) { | |
setState(() { | |
_isLoginContinues = false; | |
}); | |
Navigator.pushReplacementNamed( | |
context, | |
'select_dialog', | |
arguments: {userArgName: cubeUser}, | |
); | |
}).catchError((error) { | |
// process creating Chat session error | |
print("loginChat error: $error"); | |
setState(() { | |
_isLoginContinues = false; | |
}); | |
}); | |
}).catchError((error) { | |
// process creating API session error | |
print("createSession error: $error"); | |
setState(() { | |
_isLoginContinues = false; | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment