relatica/lib/screens/sign_in.dart

156 wiersze
5.4 KiB
Dart
Czysty Zwykły widok Historia

2022-11-09 02:28:48 +00:00
import 'package:flutter/material.dart';
import 'package:string_validator/string_validator.dart';
2022-11-09 02:28:48 +00:00
import '../controls/padding.dart';
import '../globals.dart';
import '../models/auth/credentials.dart';
2022-11-09 02:28:48 +00:00
import '../services/auth_service.dart';
import '../services/secrets_service.dart';
2022-11-09 02:28:48 +00:00
import '../utils/snackbar_builder.dart';
class SignInScreen extends StatefulWidget {
@override
State<SignInScreen> createState() => _SignInScreenState();
}
class _SignInScreenState extends State<SignInScreen> {
2022-11-09 02:28:48 +00:00
final formKey = GlobalKey<FormState>();
final usernameController = TextEditingController();
final serverNameController = TextEditingController();
2022-11-09 02:28:48 +00:00
final passwordController = TextEditingController();
2023-01-28 23:18:18 +00:00
var hidePassword = true;
2022-11-09 02:28:48 +00:00
@override
void initState() {
super.initState();
getIt<SecretsService>().credentials.andThenSuccess((credentials) {
if (credentials.isEmpty) {
return;
}
usernameController.text = credentials.username;
passwordController.text = credentials.password;
serverNameController.text = credentials.serverName;
});
}
2022-11-09 02:28:48 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2023-01-28 23:18:18 +00:00
title: const Text('Sign In'),
2022-11-09 02:28:48 +00:00
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Form(
key: formKey,
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: serverNameController,
validator: (value) =>
isFQDN(value ?? '') ? null : 'Not a valid server name',
decoration: InputDecoration(
prefixIcon: const Icon(Icons.alternate_email),
hintText: 'Username (user@example.com)',
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).backgroundColor,
),
borderRadius: BorderRadius.circular(5.0),
),
labelText: 'Username',
),
),
const VerticalPadding(),
2022-11-09 02:28:48 +00:00
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: usernameController,
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null) {
return null;
}
if (value.contains('@')) {
return isEmail(value ?? '')
? null
: 'Not a valid Friendica Account Address';
}
return isAlphanumeric(value.replaceAll('-', ''))
? null
: 'Username should be alpha-numeric';
},
2022-11-09 02:28:48 +00:00
decoration: InputDecoration(
2023-01-28 23:18:18 +00:00
prefixIcon: const Icon(Icons.alternate_email),
2022-11-09 02:28:48 +00:00
hintText: 'Username (user@example.com)',
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).backgroundColor,
2022-11-09 02:28:48 +00:00
),
borderRadius: BorderRadius.circular(5.0),
),
labelText: 'Username',
),
),
const VerticalPadding(),
TextFormField(
2023-01-28 23:18:18 +00:00
obscureText: hidePassword,
2022-11-09 02:28:48 +00:00
controller: passwordController,
decoration: InputDecoration(
2023-01-28 23:18:18 +00:00
prefixIcon: const Icon(Icons.password),
suffixIcon: IconButton(
onPressed: () {
setState(() {
hidePassword = !hidePassword;
});
},
icon: hidePassword
? const Icon(Icons.remove_red_eye_outlined)
: const Icon(Icons.remove_red_eye),
),
2022-11-09 02:28:48 +00:00
hintText: 'Password',
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).backgroundColor,
2022-11-09 02:28:48 +00:00
),
borderRadius: BorderRadius.circular(5.0),
),
labelText: 'Password',
),
),
const VerticalPadding(),
ElevatedButton(
onPressed: () => _signIn(context),
2023-01-28 23:18:18 +00:00
child: const Text('Signin'),
2022-11-09 02:28:48 +00:00
),
],
),
),
),
),
);
}
void _signIn(BuildContext context) async {
if (formKey.currentState?.validate() ?? false) {
final creds = Credentials(
username: usernameController.text,
password: passwordController.text,
serverName: serverNameController.text,
userId: '',
avatar: '');
final result = await getIt<AuthService>().signIn(creds);
if (result.isFailure) {
buildSnackbar(context, 'Error signing in: ${result.error}');
}
2022-11-09 02:28:48 +00:00
}
}
}