ente/lib/ui/account/login_page.dart

241 lines
8.5 KiB
Dart
Raw Normal View History

2022-04-09 05:40:56 +00:00
import 'package:email_validator/email_validator.dart';
import "package:flutter/foundation.dart";
2021-03-30 11:08:41 +00:00
import 'package:flutter/material.dart';
2023-07-18 02:47:29 +00:00
import "package:logging/logging.dart";
2021-03-30 11:08:41 +00:00
import 'package:photos/core/configuration.dart';
import "package:photos/core/errors.dart";
2023-04-04 19:16:37 +00:00
import "package:photos/generated/l10n.dart";
2023-04-04 14:03:23 +00:00
import "package:photos/l10n/l10n.dart";
import "package:photos/models/api/user/srp.dart";
2021-03-30 11:08:41 +00:00
import 'package:photos/services/user_service.dart';
2023-07-18 02:47:29 +00:00
import "package:photos/ui/account/login_pwd_verification_page.dart";
2022-07-03 10:09:01 +00:00
import 'package:photos/ui/common/dynamic_fab.dart';
import 'package:photos/ui/common/web_page.dart';
2023-04-08 07:54:18 +00:00
import "package:styled_text/styled_text.dart";
2022-04-09 05:40:56 +00:00
2021-03-30 11:08:41 +00:00
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);
2021-03-30 11:08:41 +00:00
@override
2022-07-03 09:45:00 +00:00
State<LoginPage> createState() => _LoginPageState();
2021-03-30 11:08:41 +00:00
}
class _LoginPageState extends State<LoginPage> {
final _config = Configuration.instance;
2022-04-09 05:40:56 +00:00
bool _emailIsValid = false;
String? _email;
Color? _emailInputFieldColor;
2023-07-18 02:47:29 +00:00
final Logger _logger = Logger('_LoginPageState');
2021-03-30 11:08:41 +00:00
@override
void initState() {
if ((_config.getEmail() ?? '').isNotEmpty) {
updateEmail(_config.getEmail()!);
} else if (kDebugMode) {
updateEmail(const String.fromEnvironment("email"));
}
2021-03-30 11:08:41 +00:00
super.initState();
}
@override
Widget build(BuildContext context) {
2022-06-15 06:48:23 +00:00
final isKeypadOpen = MediaQuery.of(context).viewInsets.bottom > 100;
2022-04-09 05:40:56 +00:00
FloatingActionButtonLocation? fabLocation() {
2022-04-09 05:40:56 +00:00
if (isKeypadOpen) {
return null;
} else {
return FloatingActionButtonLocation.centerFloat;
}
}
2021-03-30 11:48:08 +00:00
return Scaffold(
2022-06-15 06:48:23 +00:00
resizeToAvoidBottomInset: isKeypadOpen,
2022-05-30 15:43:33 +00:00
appBar: AppBar(
elevation: 0,
leading: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(Icons.arrow_back),
2022-05-30 15:43:33 +00:00
color: Theme.of(context).iconTheme.color,
onPressed: () {
Navigator.of(context).pop();
2022-04-09 05:40:56 +00:00
},
),
2022-05-30 15:43:33 +00:00
),
body: _getBody(),
floatingActionButton: DynamicFAB(
key: const ValueKey("logInButton"),
2022-05-30 15:43:33 +00:00
isKeypadOpen: isKeypadOpen,
isFormValid: _emailIsValid,
2023-04-04 19:16:37 +00:00
buttonText: S.of(context).logInLabel,
2023-07-18 02:47:29 +00:00
onPressedFunction: () async {
2023-12-16 21:04:26 +00:00
await UserService.instance.setEmail(_email!);
SrpAttributes? attr;
bool isEmailVerificationEnabled = true;
2023-07-18 02:47:29 +00:00
try {
attr = await UserService.instance.getSrpAttributes(_email!);
isEmailVerificationEnabled = attr.isEmailMFAEnabled;
} catch (e) {
if (e is! SrpSetupNotCompleteError) {
_logger.severe('Error getting SRP attributes', e);
}
}
if (attr != null && !isEmailVerificationEnabled) {
2023-12-16 21:04:26 +00:00
// ignore: unawaited_futures
2023-07-18 02:47:29 +00:00
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return LoginPasswordVerificationPage(
srpAttributes: attr!,
2023-07-18 02:47:29 +00:00
);
},
),
);
} else {
2023-07-18 02:47:29 +00:00
await UserService.instance
.sendOtt(context, _email!, isCreateAccountScreen: false);
}
FocusScope.of(context).unfocus();
2022-05-30 15:43:33 +00:00
},
),
floatingActionButtonLocation: fabLocation(),
floatingActionButtonAnimator: NoScalingAnimation(),
);
2021-03-30 11:08:41 +00:00
}
2021-03-30 11:48:08 +00:00
Widget _getBody() {
2023-04-04 14:03:23 +00:00
final l10n = context.l10n;
2021-03-30 11:08:41 +00:00
return Column(
children: [
2022-04-09 05:40:56 +00:00
Expanded(
2022-07-14 13:28:37 +00:00
child: AutofillGroup(
child: ListView(
children: [
Padding(
padding:
const EdgeInsets.symmetric(vertical: 30, horizontal: 20),
child: Text(
2023-04-04 14:03:23 +00:00
l10n.accountWelcomeBack,
2023-06-13 06:41:31 +00:00
style: Theme.of(context).textTheme.headlineMedium,
2022-07-14 13:28:37 +00:00
),
2022-06-11 08:23:52 +00:00
),
2022-07-14 13:28:37 +00:00
Padding(
padding: const EdgeInsets.fromLTRB(20, 24, 20, 0),
child: TextFormField(
key: const ValueKey("emailInputField"),
2022-07-14 13:28:37 +00:00
autofillHints: const [AutofillHints.email],
decoration: InputDecoration(
fillColor: _emailInputFieldColor,
filled: true,
2023-04-04 14:03:23 +00:00
hintText: l10n.email,
2022-07-14 13:28:37 +00:00
contentPadding: const EdgeInsets.symmetric(
horizontal: 15,
vertical: 15,
),
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
),
suffixIcon: _emailIsValid
? Icon(
Icons.check,
size: 20,
color: Theme.of(context)
.inputDecorationTheme
.focusedBorder!
2022-07-14 13:28:37 +00:00
.borderSide
.color,
)
: null,
2022-06-11 08:23:52 +00:00
),
2022-07-14 13:28:37 +00:00
onChanged: (value) {
setState(() {
updateEmail(value);
2022-07-14 13:28:37 +00:00
});
},
autocorrect: false,
keyboardType: TextInputType.emailAddress,
initialValue: _email,
2022-07-14 13:28:37 +00:00
autofocus: true,
2021-03-30 11:08:41 +00:00
),
),
2022-07-14 13:28:37 +00:00
const Padding(
padding: EdgeInsets.symmetric(vertical: 18),
child: Divider(
thickness: 1,
),
2022-04-09 05:40:56 +00:00
),
2022-07-14 13:28:37 +00:00
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Row(
children: [
Expanded(
flex: 5,
2023-04-08 07:54:18 +00:00
child: StyledText(
text: S.of(context).loginTerms,
style: Theme.of(context)
.textTheme
2023-06-13 06:41:31 +00:00
.titleMedium!
2023-04-08 07:54:18 +00:00
.copyWith(fontSize: 12),
tags: {
'u-terms': StyledTextActionTag(
(String? text, Map<String?, String?> attrs) =>
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return WebPage(
S.of(context).termsOfServicesTitle,
"https://ente.io/terms",
);
},
2023-08-19 11:39:56 +00:00
),
),
2023-04-08 07:54:18 +00:00
style: const TextStyle(
decoration: TextDecoration.underline,
2022-07-14 13:28:37 +00:00
),
2023-04-08 07:54:18 +00:00
),
'u-policy': StyledTextActionTag(
(String? text, Map<String?, String?> attrs) =>
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return WebPage(
S.of(context).privacyPolicyTitle,
"https://ente.io/privacy",
);
},
2023-08-19 11:39:56 +00:00
),
),
2023-04-08 07:54:18 +00:00
style: const TextStyle(
decoration: TextDecoration.underline,
2022-04-09 05:40:56 +00:00
),
2023-08-19 11:39:56 +00:00
),
2023-04-08 07:54:18 +00:00
},
2021-03-30 11:08:41 +00:00
),
2022-04-09 05:40:56 +00:00
),
const Expanded(
2023-04-08 07:54:18 +00:00
flex: 1,
child: SizedBox.shrink(),
2023-08-19 11:39:56 +00:00
),
2022-07-14 13:28:37 +00:00
],
),
2021-03-30 11:08:41 +00:00
),
2022-07-14 13:28:37 +00:00
],
),
2021-03-30 11:08:41 +00:00
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(8)),
2021-03-30 11:08:41 +00:00
],
);
}
void updateEmail(String value) {
_email = value.trim();
_emailIsValid = EmailValidator.validate(_email!);
if (_emailIsValid) {
_emailInputFieldColor = const Color.fromRGBO(45, 194, 98, 0.2);
} else {
_emailInputFieldColor = null;
}
}
2021-03-30 11:08:41 +00:00
}