ente/mobile/lib/ui/account/email_entry_page.dart

528 lines
19 KiB
Dart
Raw Normal View History

import 'package:email_validator/email_validator.dart';
2020-08-25 04:10:05 +00:00
import 'package:flutter/material.dart';
2021-11-09 11:56:54 +00:00
import 'package:flutter/services.dart';
2022-05-29 09:43:21 +00:00
import 'package:password_strength/password_strength.dart';
2020-08-25 23:02:43 +00:00
import 'package:photos/core/configuration.dart';
2022-06-07 04:40:29 +00:00
import 'package:photos/ente_theme_data.dart';
2023-04-04 16:56:36 +00:00
import "package:photos/generated/l10n.dart";
import 'package:photos/services/user_service.dart';
2023-11-06 12:03:33 +00:00
import "package:photos/theme/ente_theme.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-11-06 12:03:33 +00:00
import "package:photos/utils/toast_util.dart";
2022-03-16 06:59:53 +00:00
import 'package:step_progress_indicator/step_progress_indicator.dart';
2023-04-08 08:00:58 +00:00
import "package:styled_text/styled_text.dart";
2020-08-25 04:10:05 +00:00
class EmailEntryPage extends StatefulWidget {
const EmailEntryPage({Key? key}) : super(key: key);
2020-08-25 04:10:05 +00:00
@override
2022-07-03 09:45:00 +00:00
State<EmailEntryPage> createState() => _EmailEntryPageState();
2020-08-25 04:10:05 +00:00
}
2022-04-09 05:40:56 +00:00
class _EmailEntryPageState extends State<EmailEntryPage> {
2022-05-29 09:43:21 +00:00
static const kMildPasswordStrengthThreshold = 0.4;
static const kStrongPasswordStrengthThreshold = 0.7;
2021-05-08 23:59:55 +00:00
2020-10-31 15:33:32 +00:00
final _config = Configuration.instance;
final _passwordController1 = TextEditingController();
final _passwordController2 = TextEditingController();
2022-07-04 06:02:17 +00:00
final Color _validFieldValueColor = const Color.fromRGBO(45, 194, 98, 0.2);
2021-05-08 23:59:55 +00:00
String? _email;
String? _password;
2022-06-17 09:55:38 +00:00
String _cnfPassword = '';
2023-11-06 12:03:33 +00:00
String _referralSource = '';
2022-05-29 09:43:21 +00:00
double _passwordStrength = 0.0;
bool _emailIsValid = false;
2022-12-30 15:42:03 +00:00
bool _hasAgreedToTOS = true;
bool _hasAgreedToE2E = false;
2021-05-09 19:08:27 +00:00
bool _password1Visible = false;
bool _password2Visible = false;
bool _passwordsMatch = false;
2022-05-29 09:43:21 +00:00
2021-07-26 14:04:11 +00:00
final _password1FocusNode = FocusNode();
final _password2FocusNode = FocusNode();
2021-05-09 19:08:27 +00:00
bool _password1InFocus = false;
bool _password2InFocus = false;
bool _passwordIsValid = false;
@override
void initState() {
_email = _config.getEmail();
2021-05-09 19:08:27 +00:00
_password1FocusNode.addListener(() {
setState(() {
_password1InFocus = _password1FocusNode.hasFocus;
});
});
_password2FocusNode.addListener(() {
setState(() {
_password2InFocus = _password2FocusNode.hasFocus;
});
});
super.initState();
}
2020-08-25 04:10:05 +00:00
@override
Widget build(BuildContext context) {
2022-06-15 06:48:23 +00:00
final isKeypadOpen = MediaQuery.of(context).viewInsets.bottom > 100;
2022-04-05 11:14:03 +00:00
FloatingActionButtonLocation? fabLocation() {
2022-04-05 11:14:03 +00:00
if (isKeypadOpen) {
return null;
} else {
return FloatingActionButtonLocation.centerFloat;
}
}
final appBar = AppBar(
2022-03-16 06:59:53 +00:00
elevation: 0,
leading: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(Icons.arrow_back),
color: Theme.of(context).iconTheme.color,
onPressed: () {
Navigator.of(context).pop();
},
2022-03-15 09:28:57 +00:00
),
title: Material(
2022-06-11 08:23:52 +00:00
type: MaterialType.transparency,
child: StepProgressIndicator(
totalSteps: 4,
currentStep: 1,
2022-07-12 06:30:02 +00:00
selectedColor: Theme.of(context).colorScheme.greenAlternative,
2022-07-04 06:02:17 +00:00
roundedEdges: const Radius.circular(10),
2022-06-11 08:23:52 +00:00
unselectedColor:
Theme.of(context).colorScheme.stepProgressUnselectedColor,
),
),
);
return Scaffold(
2022-06-15 06:48:23 +00:00
resizeToAvoidBottomInset: isKeypadOpen,
2022-05-30 15:43:33 +00:00
appBar: appBar,
body: _getBody(),
floatingActionButton: DynamicFAB(
isKeypadOpen: isKeypadOpen,
isFormValid: _isFormValid(),
2023-04-04 16:56:36 +00:00
buttonText: S.of(context).createAccount,
2022-05-30 15:43:33 +00:00
onPressedFunction: () {
_config.setVolatilePassword(_passwordController1.text);
UserService.instance.setEmail(_email!);
2023-11-06 12:03:33 +00:00
UserService.instance.setRefSource(_referralSource);
2022-05-30 15:43:33 +00:00
UserService.instance
.sendOtt(context, _email!, isCreateAccountScreen: true);
FocusScope.of(context).unfocus();
2022-05-30 15:43:33 +00:00
},
),
floatingActionButtonLocation: fabLocation(),
floatingActionButtonAnimator: NoScalingAnimation(),
);
2020-08-25 04:10:05 +00:00
}
2021-05-08 23:59:55 +00:00
Widget _getBody() {
2023-04-04 19:05:53 +00:00
var passwordStrengthText = S.of(context).weakStrength;
2022-05-29 09:43:21 +00:00
var passwordStrengthColor = Colors.redAccent;
if (_passwordStrength > kStrongPasswordStrengthThreshold) {
2023-04-04 19:05:53 +00:00
passwordStrengthText = S.of(context).strongStrength;
2022-05-29 09:43:21 +00:00
passwordStrengthColor = Colors.greenAccent;
} else if (_passwordStrength > kMildPasswordStrengthThreshold) {
2023-04-04 19:05:53 +00:00
passwordStrengthText = S.of(context).moderateStrength;
2022-05-29 09:43:21 +00:00
passwordStrengthColor = Colors.orangeAccent;
}
2021-05-08 23:59:55 +00:00
return Column(
children: [
2021-05-09 19:55:52 +00:00
Expanded(
2021-11-09 12:00:19 +00:00
child: AutofillGroup(
child: ListView(
children: [
Padding(
padding:
const EdgeInsets.symmetric(vertical: 30, horizontal: 20),
2022-06-11 08:23:52 +00:00
child: Text(
2023-04-04 16:56:36 +00:00
S.of(context).createNewAccount,
2023-06-13 06:41:31 +00:00
style: Theme.of(context).textTheme.headlineMedium,
2022-06-11 08:23:52 +00:00
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
2021-11-09 12:00:19 +00:00
child: TextFormField(
2023-06-13 06:41:31 +00:00
style: Theme.of(context).textTheme.titleMedium,
2022-05-29 09:43:21 +00:00
autofillHints: const [AutofillHints.email],
2021-11-09 12:00:19 +00:00
decoration: InputDecoration(
2022-05-29 09:43:21 +00:00
fillColor: _emailIsValid ? _validFieldValueColor : null,
filled: true,
2023-04-04 16:56:36 +00:00
hintText: S.of(context).email,
2022-07-04 06:02:17 +00:00
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
),
border: UnderlineInputBorder(
2022-06-11 08:23:52 +00:00
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
),
2022-04-06 20:42:45 +00:00
suffixIcon: _emailIsValid
? Icon(
Icons.check,
2022-04-06 20:42:45 +00:00
size: 20,
color: Theme.of(context)
.inputDecorationTheme
.focusedBorder!
.borderSide
.color,
)
: null,
2021-05-08 23:59:55 +00:00
),
2021-11-09 12:00:19 +00:00
onChanged: (value) {
2022-05-29 09:43:21 +00:00
_email = value.trim();
if (_emailIsValid != EmailValidator.validate(_email!)) {
2022-05-29 09:43:21 +00:00
setState(() {
_emailIsValid = EmailValidator.validate(_email!);
2022-05-29 09:43:21 +00:00
});
}
2021-11-09 12:00:19 +00:00
},
autocorrect: false,
keyboardType: TextInputType.emailAddress,
//initialValue: _email,
2021-11-09 12:00:19 +00:00
textInputAction: TextInputAction.next,
2020-12-12 00:31:06 +00:00
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(4)),
2021-11-09 12:00:19 +00:00
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
2021-11-09 12:00:19 +00:00
child: TextFormField(
keyboardType: TextInputType.text,
controller: _passwordController1,
obscureText: !_password1Visible,
enableSuggestions: true,
2022-05-29 09:43:21 +00:00
autofillHints: const [AutofillHints.newPassword],
2021-11-09 12:00:19 +00:00
decoration: InputDecoration(
2022-05-29 09:43:21 +00:00
fillColor:
_passwordIsValid ? _validFieldValueColor : null,
filled: true,
2023-04-04 16:56:36 +00:00
hintText: S.of(context).password,
2022-07-04 06:02:17 +00:00
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
),
2021-11-09 12:00:19 +00:00
suffixIcon: _password1InFocus
? IconButton(
icon: Icon(
_password1Visible
? Icons.visibility
: Icons.visibility_off,
color: Theme.of(context).iconTheme.color,
2021-11-09 12:00:19 +00:00
size: 20,
),
onPressed: () {
setState(() {
_password1Visible = !_password1Visible;
});
},
)
: _passwordIsValid
? Icon(
Icons.check,
color: Theme.of(context)
.inputDecorationTheme
.focusedBorder!
.borderSide
.color,
)
: null,
border: UnderlineInputBorder(
2022-06-11 08:23:52 +00:00
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
),
),
2021-11-09 12:00:19 +00:00
focusNode: _password1FocusNode,
onChanged: (password) {
2022-05-29 09:43:21 +00:00
if (password != _password) {
setState(() {
_password = password;
_passwordStrength =
estimatePasswordStrength(password);
_passwordIsValid = _passwordStrength >=
kMildPasswordStrengthThreshold;
2022-06-17 09:55:38 +00:00
_passwordsMatch = _password == _cnfPassword;
2022-05-29 09:43:21 +00:00
});
}
2021-11-09 12:00:19 +00:00
},
onEditingComplete: () {
_password1FocusNode.unfocus();
_password2FocusNode.requestFocus();
TextInput.finishAutofillContext();
},
2021-05-08 23:59:55 +00:00
),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: TextFormField(
keyboardType: TextInputType.visiblePassword,
controller: _passwordController2,
obscureText: !_password2Visible,
autofillHints: const [AutofillHints.newPassword],
onEditingComplete: () => TextInput.finishAutofillContext(),
decoration: InputDecoration(
fillColor: _passwordsMatch && _passwordIsValid
? _validFieldValueColor
: null,
filled: true,
2023-04-04 16:56:36 +00:00
hintText: S.of(context).confirmPassword,
2022-07-04 06:02:17 +00:00
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
),
suffixIcon: _password2InFocus
? IconButton(
icon: Icon(
_password2Visible
? Icons.visibility
: Icons.visibility_off,
color: Theme.of(context).iconTheme.color,
size: 20,
),
onPressed: () {
setState(() {
_password2Visible = !_password2Visible;
});
},
)
: _passwordsMatch
? Icon(
Icons.check,
color: Theme.of(context)
.inputDecorationTheme
.focusedBorder!
.borderSide
.color,
)
: null,
border: UnderlineInputBorder(
2022-06-11 08:23:52 +00:00
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
),
),
focusNode: _password2FocusNode,
onChanged: (cnfPassword) {
setState(() {
2022-06-17 09:55:38 +00:00
_cnfPassword = cnfPassword;
if (_password != null || _password != '') {
2022-06-17 09:55:38 +00:00
_passwordsMatch = _password == _cnfPassword;
}
});
},
),
),
Opacity(
opacity: (_password != '') && _password1InFocus ? 1 : 0,
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
child: Text(
2023-04-04 19:05:53 +00:00
S.of(context).passwordStrength(passwordStrengthText),
style: TextStyle(
color: passwordStrengthColor,
fontWeight: FontWeight.w500,
fontSize: 12,
),
),
),
2021-11-09 12:00:19 +00:00
),
const SizedBox(height: 4),
2023-11-06 12:03:33 +00:00
Padding(
padding:
const EdgeInsets.symmetric(vertical: 0, horizontal: 20),
child: Text(
S.of(context).hearUsWhereTitle,
style: getEnteTextTheme(context).smallFaint,
),
),
const SizedBox(height: 4),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: TextFormField(
style: Theme.of(context).textTheme.titleMedium,
decoration: InputDecoration(
fillColor: null,
filled: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
),
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
),
suffixIcon: InkWell(
onTap: () {
showToast(
context,
S.of(context).hearUsExplanation,
iosLongToastLengthInSec: 4,
);
},
child: Icon(
Icons.info_outline_rounded,
color: getEnteColorScheme(context).fillStrong,
),
),
),
onChanged: (value) {
_referralSource = value.trim();
},
autocorrect: false,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
),
),
const Divider(thickness: 1),
const SizedBox(height: 12),
2021-11-09 12:00:19 +00:00
_getAgreement(),
2022-06-15 06:48:23 +00:00
const SizedBox(height: 40),
2021-11-09 12:00:19 +00:00
],
),
2021-11-09 11:56:54 +00:00
),
2021-05-08 23:59:55 +00:00
),
],
);
}
Container _getAgreement() {
return Container(
padding: const EdgeInsets.only(left: 20, right: 20, bottom: 20),
2021-05-08 23:59:55 +00:00
child: Column(
children: [
_getTOSAgreement(),
_getPasswordAgreement(),
],
),
);
}
Widget _getTOSAgreement() {
return GestureDetector(
onTap: () {
setState(() {
2022-12-30 15:42:03 +00:00
_hasAgreedToTOS = !_hasAgreedToTOS;
2021-05-08 23:59:55 +00:00
});
},
behavior: HitTestBehavior.translucent,
child: Row(
children: [
Checkbox(
2022-06-11 08:23:52 +00:00
value: _hasAgreedToTOS,
side: CheckboxTheme.of(context).side,
onChanged: (value) {
setState(() {
2022-12-30 15:42:03 +00:00
_hasAgreedToTOS = value!;
2022-06-11 08:23:52 +00:00
});
},
),
2021-05-08 23:59:55 +00:00
Expanded(
2023-04-08 08:00:58 +00:00
child: StyledText(
text: S.of(context).signUpTerms,
2023-06-13 06:41:31 +00:00
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontSize: 12),
2023-04-08 08:00:58 +00:00
tags: {
'u-terms': StyledTextActionTag(
2023-11-06 12:03:33 +00:00
(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-11-06 12:03:33 +00:00
),
2023-04-08 08:00:58 +00:00
style: const TextStyle(
decoration: TextDecoration.underline,
2021-05-08 23:59:55 +00:00
),
2023-04-08 08:00:58 +00:00
),
'u-policy': StyledTextActionTag(
2023-11-06 12:03:33 +00:00
(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-11-06 12:03:33 +00:00
),
2023-04-08 08:00:58 +00:00
style: const TextStyle(
decoration: TextDecoration.underline,
2021-05-08 23:59:55 +00:00
),
2023-08-19 11:39:56 +00:00
),
2023-04-08 08:00:58 +00:00
},
2020-12-12 00:31:06 +00:00
),
2021-05-08 23:59:55 +00:00
),
],
),
);
}
Widget _getPasswordAgreement() {
return GestureDetector(
onTap: () {
setState(() {
2022-12-30 15:42:03 +00:00
_hasAgreedToE2E = !_hasAgreedToE2E;
2021-05-08 23:59:55 +00:00
});
},
behavior: HitTestBehavior.translucent,
child: Row(
children: [
Checkbox(
value: _hasAgreedToE2E,
side: CheckboxTheme.of(context).side,
onChanged: (value) {
setState(() {
2022-12-30 15:42:03 +00:00
_hasAgreedToE2E = value!;
});
},
),
2021-05-08 23:59:55 +00:00
Expanded(
2023-04-08 08:13:57 +00:00
child: StyledText(
text: S.of(context).ackPasswordLostWarning,
2023-06-13 06:41:31 +00:00
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontSize: 12),
2023-04-08 08:13:57 +00:00
tags: {
'underline': StyledTextActionTag(
2023-11-06 12:03:33 +00:00
(String? text, Map<String?, String?> attrs) =>
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return WebPage(
S.of(context).encryption,
"https://ente.io/architecture",
);
},
2023-08-19 11:39:56 +00:00
),
2023-11-06 12:03:33 +00:00
),
2023-04-08 08:13:57 +00:00
style: const TextStyle(
decoration: TextDecoration.underline,
2023-04-04 19:05:53 +00:00
),
2023-04-08 08:13:57 +00:00
),
},
),
2021-05-08 23:59:55 +00:00
),
],
2020-08-25 06:00:19 +00:00
),
2020-08-25 04:10:05 +00:00
);
}
2021-05-08 23:59:55 +00:00
bool _isFormValid() {
return _emailIsValid &&
_passwordsMatch &&
2022-12-30 15:42:03 +00:00
_hasAgreedToTOS &&
_hasAgreedToE2E &&
_passwordIsValid;
2021-05-08 23:59:55 +00:00
}
2020-08-25 04:10:05 +00:00
}