2024-04-05 15:57:46 +00:00
|
|
|
import 'dart:html';
|
|
|
|
|
2024-04-04 14:35:33 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-04-05 15:14:56 +00:00
|
|
|
import 'package:grpc/grpc_web.dart';
|
|
|
|
import 'package:softplayer_web/api/grpc/accounts.dart';
|
2024-04-04 14:35:33 +00:00
|
|
|
|
|
|
|
class SignInForm extends StatefulWidget {
|
2024-04-05 15:14:56 +00:00
|
|
|
const SignInForm({
|
|
|
|
super.key,
|
|
|
|
required this.accountsGrpc,
|
|
|
|
});
|
2024-04-04 14:35:33 +00:00
|
|
|
|
2024-04-05 15:14:56 +00:00
|
|
|
final AccountsGrpc accountsGrpc;
|
2024-04-04 14:35:33 +00:00
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _SignInFormState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SignInFormState extends State<SignInForm> {
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
2024-04-05 15:14:56 +00:00
|
|
|
final usernameCtrl = TextEditingController();
|
|
|
|
final passwordCtrl = TextEditingController();
|
2024-04-04 14:35:33 +00:00
|
|
|
static const dialogName = "Sign In";
|
2024-04-05 15:14:56 +00:00
|
|
|
void submitForm() {
|
|
|
|
// Validate returns true if the form is valid, or false otherwise.
|
|
|
|
if (_formKey.currentState!.validate()) {
|
|
|
|
final username = usernameCtrl.text;
|
|
|
|
final password = passwordCtrl.text;
|
2024-04-19 11:19:44 +00:00
|
|
|
widget.accountsGrpc.signIn(username, "", password).then((rs) {
|
|
|
|
window.localStorage["token"] = rs.token;
|
|
|
|
window.localStorage["uuid"] = rs.uuid;
|
|
|
|
Navigator.of(context, rootNavigator: true).pop();
|
|
|
|
}).catchError((e) {
|
|
|
|
GrpcError error = e;
|
|
|
|
String msg;
|
|
|
|
if (error.message != null) {
|
|
|
|
msg = error.message!;
|
|
|
|
} else {
|
|
|
|
msg = error.toString();
|
|
|
|
}
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(msg),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
showCloseIcon: true,
|
|
|
|
behavior: SnackBarBehavior.floating,
|
|
|
|
));
|
|
|
|
passwordCtrl.clear();
|
|
|
|
});
|
2024-04-05 15:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:35:33 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => AlertDialog(
|
|
|
|
title: const Text(dialogName),
|
2024-04-04 21:40:00 +00:00
|
|
|
content: SizedBox(
|
2024-04-05 15:14:56 +00:00
|
|
|
width: 420,
|
|
|
|
height: 140,
|
|
|
|
child: Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: Center(
|
|
|
|
child: Column(children: [
|
|
|
|
TextFormField(
|
|
|
|
controller: usernameCtrl,
|
|
|
|
autofocus: true,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
hintText: "Enter your username or email",
|
|
|
|
icon: Icon(Icons.account_circle),
|
|
|
|
label: Text("Username"),
|
|
|
|
),
|
|
|
|
cursorWidth: 1,
|
|
|
|
cursorHeight: 18,
|
|
|
|
cursorRadius: const Radius.circular(10),
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
controller: passwordCtrl,
|
|
|
|
obscureText: true,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
hintText: "Enter your password",
|
|
|
|
icon: Icon(Icons.password),
|
|
|
|
label: Text("Password")),
|
|
|
|
cursorWidth: 1,
|
|
|
|
cursorHeight: 18,
|
|
|
|
cursorRadius: const Radius.circular(10),
|
|
|
|
onFieldSubmitted: (v) {
|
|
|
|
if (usernameCtrl.text.isEmpty) {
|
|
|
|
FocusScope.of(context).nextFocus();
|
|
|
|
} else {
|
|
|
|
submitForm();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
])))),
|
2024-04-04 14:35:33 +00:00
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(context, 'Cancel'),
|
|
|
|
child: const Text('Cancel'),
|
|
|
|
),
|
|
|
|
TextButton(
|
2024-04-05 15:14:56 +00:00
|
|
|
onPressed: submitForm,
|
2024-04-04 14:35:33 +00:00
|
|
|
child: const Text('OK'),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|