ente/lib/ui/email_entry_page.dart

522 lines
17 KiB
Dart
Raw Normal View History

2021-05-08 23:59:55 +00:00
import 'dart:io';
2020-08-25 04:10:05 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
2020-08-25 04:10:05 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2021-05-08 23:59:55 +00:00
import 'package:flutter_password_strength/flutter_password_strength.dart';
2020-08-25 23:02:43 +00:00
import 'package:photos/core/configuration.dart';
import 'package:photos/models/billing_plan.dart';
import 'package:photos/services/billing_service.dart';
2020-10-03 17:56:18 +00:00
import 'package:photos/services/user_service.dart';
2020-10-24 21:07:12 +00:00
import 'package:photos/ui/common_elements.dart';
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/web_page.dart';
2021-02-01 11:02:30 +00:00
import 'package:photos/utils/data_util.dart';
2020-10-28 19:07:39 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/email_util.dart';
2021-08-15 19:35:16 +00:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2020-08-25 04:10:05 +00:00
class EmailEntryPage extends StatefulWidget {
2020-08-25 23:02:43 +00:00
EmailEntryPage({Key key}) : super(key: key);
2020-08-25 04:10:05 +00:00
@override
_EmailEntryPageState createState() => _EmailEntryPageState();
}
class _EmailEntryPageState extends State<EmailEntryPage> {
2021-05-08 23:59:55 +00:00
static const kPasswordStrengthThreshold = 0.4;
2020-10-31 15:33:32 +00:00
final _config = Configuration.instance;
2021-05-08 23:59:55 +00:00
final _passwordController1 = TextEditingController(),
_passwordController2 = TextEditingController();
2020-11-01 08:10:35 +00:00
String _email;
2021-05-08 23:59:55 +00:00
double _passwordStrength = 0;
bool _hasAgreedToTOS = true;
bool _hasAgreedToE2E = false;
2021-05-09 19:08:27 +00:00
bool _password1Visible = false;
bool _password2Visible = false;
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;
@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) {
final appBar = AppBar(
2021-05-12 05:24:37 +00:00
title: Hero(
tag: "sign_up",
child: Material(
type: MaterialType.transparency,
child: Text(
2021-08-15 19:35:16 +00:00
AppLocalizations.of(context).sign_up,
2021-05-12 05:24:37 +00:00
style: TextStyle(
fontSize: 18,
2021-05-12 06:03:41 +00:00
letterSpacing: 0.6,
2021-05-12 05:24:37 +00:00
),
),
),
2020-08-25 04:10:05 +00:00
),
);
return Scaffold(
appBar: appBar,
2021-05-08 23:59:55 +00:00
body: _getBody(),
2021-05-09 19:55:52 +00:00
// resizeToAvoidBottomInset: false,
2020-08-25 04:10:05 +00:00
);
}
2021-05-08 23:59:55 +00:00
Widget _getBody() {
return Column(
children: [
FlutterPasswordStrength(
password: _passwordController1.text,
backgroundColor: Colors.white.withOpacity(0.1),
strengthCallback: (strength) {
_passwordStrength = strength;
},
2021-05-12 16:37:14 +00:00
strengthColors: passwordStrengthColors,
2021-05-08 23:59:55 +00:00
),
2021-05-09 19:55:52 +00:00
Expanded(
child: ListView(
children: [
Padding(padding: EdgeInsets.all(40)),
Padding(
padding: const EdgeInsets.fromLTRB(32, 0, 32, 0),
child: TextFormField(
decoration: InputDecoration(
hintText: 'email',
hintStyle: TextStyle(
color: Colors.white30,
2021-05-08 23:59:55 +00:00
),
2021-05-09 19:55:52 +00:00
contentPadding: EdgeInsets.all(12),
2020-12-12 00:31:06 +00:00
),
2021-05-09 19:55:52 +00:00
onChanged: (value) {
setState(() {
2021-05-29 22:48:23 +00:00
_email = value.trim();
2021-05-09 19:55:52 +00:00
});
},
autocorrect: false,
keyboardType: TextInputType.emailAddress,
initialValue: _email,
2021-05-12 16:37:14 +00:00
textInputAction: TextInputAction.next,
2020-12-12 00:31:06 +00:00
),
2021-05-09 19:55:52 +00:00
),
Padding(padding: EdgeInsets.all(8)),
Padding(
padding: const EdgeInsets.fromLTRB(32, 0, 32, 0),
child: TextFormField(
keyboardType: TextInputType.text,
controller: _passwordController1,
obscureText: !_password1Visible,
decoration: InputDecoration(
hintText: "password",
hintStyle: TextStyle(
color: Colors.white30,
),
2021-05-09 19:55:52 +00:00
contentPadding: EdgeInsets.all(12),
suffixIcon: _password1InFocus
? IconButton(
icon: Icon(
_password1Visible
? Icons.visibility
: Icons.visibility_off,
color: Colors.white.withOpacity(0.5),
size: 20,
),
onPressed: () {
setState(() {
_password1Visible = !_password1Visible;
});
},
)
: null,
2021-05-08 23:59:55 +00:00
),
2021-05-09 19:55:52 +00:00
focusNode: _password1FocusNode,
onChanged: (_) {
setState(() {});
},
2021-05-13 15:19:20 +00:00
onEditingComplete: () {
_password1FocusNode.unfocus();
_password2FocusNode.requestFocus();
},
2021-05-08 23:59:55 +00:00
),
2021-05-09 19:55:52 +00:00
),
Padding(padding: EdgeInsets.all(8)),
Padding(
padding: const EdgeInsets.fromLTRB(32, 0, 32, 0),
child: TextFormField(
keyboardType: TextInputType.text,
controller: _passwordController2,
obscureText: !_password2Visible,
decoration: InputDecoration(
hintText: "confirm password",
hintStyle: TextStyle(
color: Colors.white30,
),
2021-05-09 19:55:52 +00:00
contentPadding: EdgeInsets.all(12),
suffixIcon: _password2InFocus
? IconButton(
icon: Icon(
_password2Visible
? Icons.visibility
: Icons.visibility_off,
color: Colors.white.withOpacity(0.5),
size: 20,
),
onPressed: () {
setState(() {
_password2Visible = !_password2Visible;
});
},
)
: null,
),
2021-05-09 19:55:52 +00:00
focusNode: _password2FocusNode,
),
2021-05-09 19:55:52 +00:00
),
Padding(
padding: EdgeInsets.all(20),
),
_getAgreement(),
2021-05-12 15:59:07 +00:00
Padding(padding: EdgeInsets.all(20)),
2021-05-09 19:55:52 +00:00
Container(
width: double.infinity,
height: 64,
padding: const EdgeInsets.fromLTRB(80, 0, 80, 0),
child: button(
2021-08-15 19:55:39 +00:00
AppLocalizations.of(context).sign_up,
2021-05-09 19:55:52 +00:00
onPressed: _isFormValid()
? () {
if (!isValidEmail(_email)) {
2021-05-12 15:12:28 +00:00
showErrorDialog(context, "invalid email",
2021-05-09 19:55:52 +00:00
"please enter a valid email address.");
} else if (_passwordController1.text !=
_passwordController2.text) {
showErrorDialog(context, "uhm...",
"the passwords you entered don't match");
} else if (_passwordStrength <
kPasswordStrengthThreshold) {
showErrorDialog(context, "weak password",
"the password you have chosen is too simple, please choose another one");
} else {
_config
.setVolatilePassword(_passwordController1.text);
_config.setEmail(_email);
UserService.instance.getOtt(context, _email);
2021-05-08 23:59:55 +00:00
}
2021-05-09 19:55:52 +00:00
}
: null,
fontSize: 18,
2021-05-08 23:59:55 +00:00
),
2021-05-09 19:55:52 +00:00
),
],
2021-05-08 23:59:55 +00:00
),
),
],
);
}
Container _getAgreement() {
return Container(
padding: const EdgeInsets.only(left: 20, right: 20),
child: Column(
children: [
_getTOSAgreement(),
_getPasswordAgreement(),
],
),
);
}
Widget _getTOSAgreement() {
return GestureDetector(
onTap: () {
setState(() {
_hasAgreedToTOS = !_hasAgreedToTOS;
});
},
behavior: HitTestBehavior.translucent,
child: Row(
children: [
Checkbox(
value: _hasAgreedToTOS,
onChanged: (value) {
setState(() {
_hasAgreedToTOS = value;
});
}),
Expanded(
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: "I agree to the ",
),
TextSpan(
text: "terms of service",
style: TextStyle(
color: Colors.blue,
fontFamily: 'Ubuntu',
),
recognizer: TapGestureRecognizer()
..onTap = () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return WebPage("terms", "https://ente.io/terms");
},
),
);
},
),
TextSpan(text: " and "),
TextSpan(
text: "privacy policy",
style: TextStyle(
color: Colors.blue,
fontFamily: 'Ubuntu',
),
recognizer: TapGestureRecognizer()
..onTap = () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return WebPage(
"privacy", "https://ente.io/privacy");
},
),
);
},
),
],
style: TextStyle(
height: 1.25,
fontSize: 12,
fontFamily: 'Ubuntu',
color: Colors.white70,
),
),
2021-05-08 23:59:55 +00:00
textAlign: TextAlign.left,
2020-12-12 00:31:06 +00:00
),
2021-05-08 23:59:55 +00:00
),
],
),
);
}
Widget _getPasswordAgreement() {
return GestureDetector(
onTap: () {
setState(() {
_hasAgreedToE2E = !_hasAgreedToE2E;
});
},
behavior: HitTestBehavior.translucent,
child: Row(
children: [
Checkbox(
value: _hasAgreedToE2E,
onChanged: (value) {
setState(() {
_hasAgreedToE2E = value;
});
}),
Expanded(
child: RichText(
text: TextSpan(
children: [
TextSpan(
text:
"I understand that if I lose my password, I may lose my data since my data is ",
),
2021-05-08 23:59:55 +00:00
TextSpan(
text: "end-to-end encrypted",
style: TextStyle(
color: Colors.blue,
fontFamily: 'Ubuntu',
),
recognizer: TapGestureRecognizer()
..onTap = () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return WebPage(
2021-08-11 08:23:43 +00:00
"encryption", "https://ente.io/architecture");
2021-05-08 23:59:55 +00:00
},
),
);
},
),
TextSpan(text: " with ente"),
],
style: TextStyle(
height: 1.5,
fontSize: 12,
fontFamily: 'Ubuntu',
color: Colors.white70,
),
),
2021-05-08 23:59:55 +00:00
textAlign: TextAlign.left,
),
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 _email != null &&
_email.isNotEmpty &&
_passwordController1.text.isNotEmpty &&
_passwordController2.text.isNotEmpty &&
_hasAgreedToTOS &&
_hasAgreedToE2E;
}
2020-08-25 04:10:05 +00:00
}
2021-01-03 14:19:30 +00:00
class PricingWidget extends StatelessWidget {
const PricingWidget({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
2021-03-02 06:35:10 +00:00
return FutureBuilder<BillingPlans>(
future: BillingService.instance.getBillingPlans(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return _buildPlans(context, snapshot.data);
} else if (snapshot.hasError) {
return Text("Oops, something went wrong.");
}
2021-02-05 11:44:52 +00:00
return loadWidget;
},
);
}
2021-03-02 06:35:10 +00:00
Container _buildPlans(BuildContext context, BillingPlans plans) {
2021-07-26 14:04:11 +00:00
final planWidgets = <BillingPlanWidget>[];
2021-03-02 06:35:10 +00:00
for (final plan in plans.plans) {
2021-05-08 23:59:55 +00:00
final productID = Platform.isAndroid ? plan.androidID : plan.iosID;
if (productID != null && productID.isNotEmpty) {
planWidgets.add(BillingPlanWidget(plan));
}
}
2021-03-10 03:08:45 +00:00
final freePlan = plans.freePlan;
2021-01-03 14:19:30 +00:00
return Container(
height: 280,
2021-02-05 11:44:52 +00:00
color: Theme.of(context).cardColor,
2021-01-03 14:19:30 +00:00
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(
"pricing",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
2021-05-08 23:59:55 +00:00
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: planWidgets,
),
2021-01-03 14:19:30 +00:00
),
2021-05-08 23:59:55 +00:00
Text("we offer a free trial of " +
2021-03-10 03:08:45 +00:00
convertBytesToReadableFormat(freePlan.storage) +
" for " +
freePlan.duration.toString() +
" " +
freePlan.period),
2021-01-03 14:19:30 +00:00
GestureDetector(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
2021-07-26 14:04:11 +00:00
children: const [
2021-01-03 14:19:30 +00:00
Icon(
Icons.close,
size: 12,
color: Colors.white38,
),
Padding(padding: EdgeInsets.all(1)),
Text(
"close",
style: TextStyle(
color: Colors.white38,
),
),
],
),
onTap: () => Navigator.pop(context),
)
],
),
);
}
}
2021-01-06 08:47:18 +00:00
class BillingPlanWidget extends StatelessWidget {
final BillingPlan plan;
const BillingPlanWidget(
this.plan, {
2021-01-03 14:19:30 +00:00
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
2021-02-05 11:08:01 +00:00
return Padding(
2021-02-05 11:44:52 +00:00
padding: const EdgeInsets.all(2.0),
2021-02-05 11:08:01 +00:00
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
2021-05-12 18:41:31 +00:00
color: Colors.black.withOpacity(0.2),
2021-02-05 11:08:01 +00:00
child: Container(
2021-02-05 11:44:52 +00:00
padding: EdgeInsets.fromLTRB(12, 20, 12, 20),
2021-02-05 11:08:01 +00:00
child: Column(
children: [
Text(
convertBytesToGBs(plan.storage, precision: 0).toString() +
" GB",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
2021-01-03 14:19:30 +00:00
),
2021-02-05 11:08:01 +00:00
Padding(
padding: EdgeInsets.all(4),
2021-01-03 14:19:30 +00:00
),
2021-02-05 11:08:01 +00:00
Text(
plan.price + " / " + plan.period,
style: TextStyle(
fontSize: 12,
2021-02-05 11:44:52 +00:00
color: Colors.white70,
2021-02-05 11:08:01 +00:00
),
),
],
),
2021-01-03 14:19:30 +00:00
),
),
);
}
}