account_section_widget.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @dart=2.9
  2. import 'dart:async';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_sodium/flutter_sodium.dart';
  5. import 'package:photos/services/local_authentication_service.dart';
  6. import 'package:photos/services/user_service.dart';
  7. import 'package:photos/theme/ente_theme.dart';
  8. import 'package:photos/ui/account/change_email_dialog.dart';
  9. import 'package:photos/ui/account/password_entry_page.dart';
  10. import 'package:photos/ui/account/recovery_key_page.dart';
  11. import 'package:photos/ui/components/captioned_text_widget.dart';
  12. import 'package:photos/ui/components/expandable_menu_item_widget.dart';
  13. import 'package:photos/ui/components/menu_item_widget.dart';
  14. import 'package:photos/ui/settings/common_settings.dart';
  15. import 'package:photos/utils/dialog_util.dart';
  16. import 'package:photos/utils/navigation_util.dart';
  17. class AccountSectionWidget extends StatelessWidget {
  18. const AccountSectionWidget({Key key}) : super(key: key);
  19. @override
  20. Widget build(BuildContext context) {
  21. return ExpandableMenuItemWidget(
  22. title: "Account",
  23. selectionOptionsWidget: _getSectionOptions(context),
  24. leadingIcon: Icons.account_circle_outlined,
  25. );
  26. }
  27. Column _getSectionOptions(BuildContext context) {
  28. return Column(
  29. children: [
  30. sectionOptionSpacing,
  31. MenuItemWidget(
  32. captionedTextWidget: const CaptionedTextWidget(
  33. title: "Recovery key",
  34. ),
  35. pressedColor: getEnteColorScheme(context).fillFaint,
  36. trailingIcon: Icons.chevron_right_outlined,
  37. trailingIconIsMuted: true,
  38. onTap: () async {
  39. final hasAuthenticated = await LocalAuthenticationService.instance
  40. .requestLocalAuthentication(
  41. context,
  42. "Please authenticate to view your recovery key",
  43. );
  44. if (hasAuthenticated) {
  45. String recoveryKey;
  46. try {
  47. recoveryKey = await _getOrCreateRecoveryKey(context);
  48. } catch (e) {
  49. await showGenericErrorDialog(context);
  50. return;
  51. }
  52. unawaited(
  53. routeToPage(
  54. context,
  55. RecoveryKeyPage(
  56. recoveryKey,
  57. "OK",
  58. showAppBar: true,
  59. onDone: () {},
  60. ),
  61. ),
  62. );
  63. }
  64. },
  65. ),
  66. sectionOptionSpacing,
  67. MenuItemWidget(
  68. captionedTextWidget: const CaptionedTextWidget(
  69. title: "Change email",
  70. ),
  71. pressedColor: getEnteColorScheme(context).fillFaint,
  72. trailingIcon: Icons.chevron_right_outlined,
  73. trailingIconIsMuted: true,
  74. onTap: () async {
  75. final hasAuthenticated = await LocalAuthenticationService.instance
  76. .requestLocalAuthentication(
  77. context,
  78. "Please authenticate to change your email",
  79. );
  80. if (hasAuthenticated) {
  81. showDialog(
  82. context: context,
  83. builder: (BuildContext context) {
  84. return const ChangeEmailDialog();
  85. },
  86. barrierColor: Colors.black.withOpacity(0.85),
  87. barrierDismissible: false,
  88. );
  89. }
  90. },
  91. ),
  92. sectionOptionSpacing,
  93. MenuItemWidget(
  94. captionedTextWidget: const CaptionedTextWidget(
  95. title: "Change password",
  96. ),
  97. pressedColor: getEnteColorScheme(context).fillFaint,
  98. trailingIcon: Icons.chevron_right_outlined,
  99. trailingIconIsMuted: true,
  100. onTap: () async {
  101. final hasAuthenticated = await LocalAuthenticationService.instance
  102. .requestLocalAuthentication(
  103. context,
  104. "Please authenticate to change your password",
  105. );
  106. if (hasAuthenticated) {
  107. Navigator.of(context).push(
  108. MaterialPageRoute(
  109. builder: (BuildContext context) {
  110. return const PasswordEntryPage(
  111. mode: PasswordEntryMode.update,
  112. );
  113. },
  114. ),
  115. );
  116. }
  117. },
  118. ),
  119. sectionOptionSpacing,
  120. ],
  121. );
  122. }
  123. Future<String> _getOrCreateRecoveryKey(BuildContext context) async {
  124. return Sodium.bin2hex(
  125. await UserService.instance.getOrCreateRecoveryKey(context),
  126. );
  127. }
  128. }