account_section_widget.dart 4.2 KB

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