account_section_widget.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:expandable/expandable.dart';
  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/settings/common_settings.dart';
  10. import 'package:photos/ui/settings/settings_section_title.dart';
  11. import 'package:photos/ui/settings/settings_text_item.dart';
  12. import 'package:photos/utils/dialog_util.dart';
  13. import 'package:photos/utils/navigation_util.dart';
  14. class AccountSectionWidget extends StatefulWidget {
  15. const AccountSectionWidget({Key key}) : super(key: key);
  16. @override
  17. AccountSectionWidgetState createState() => AccountSectionWidgetState();
  18. }
  19. class AccountSectionWidgetState extends State<AccountSectionWidget> {
  20. @override
  21. Widget build(BuildContext context) {
  22. return ExpandablePanel(
  23. header: const SettingsSectionTitle("Account"),
  24. collapsed: Container(),
  25. expanded: _getSectionOptions(context),
  26. theme: getExpandableTheme(context),
  27. );
  28. }
  29. Column _getSectionOptions(BuildContext context) {
  30. return Column(
  31. children: [
  32. GestureDetector(
  33. behavior: HitTestBehavior.translucent,
  34. onTap: () async {
  35. final hasAuthenticatedOrNoLocalAuth =
  36. await LocalAuthenticationService.instance
  37. .requestLocalAuthentication(
  38. context,
  39. "Please authenticate to view your recovery key",
  40. );
  41. if (hasAuthenticatedOrNoLocalAuth) {
  42. String recoveryKey;
  43. try {
  44. recoveryKey = await _getOrCreateRecoveryKey();
  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. child: const SettingsTextItem(
  61. text: "Recovery key",
  62. icon: Icons.navigate_next,
  63. ),
  64. ),
  65. sectionOptionDivider,
  66. GestureDetector(
  67. behavior: HitTestBehavior.translucent,
  68. onTap: () async {
  69. final hasAuthenticatedOrNoLocalAuth =
  70. await LocalAuthenticationService.instance
  71. .requestLocalAuthentication(
  72. context,
  73. "Please authenticate to change your email",
  74. );
  75. if (hasAuthenticatedOrNoLocalAuth) {
  76. showDialog(
  77. context: context,
  78. builder: (BuildContext context) {
  79. return const ChangeEmailDialog();
  80. },
  81. barrierColor: Colors.black.withOpacity(0.85),
  82. barrierDismissible: false,
  83. );
  84. }
  85. },
  86. child: const SettingsTextItem(
  87. text: "Change email",
  88. icon: Icons.navigate_next,
  89. ),
  90. ),
  91. sectionOptionDivider,
  92. GestureDetector(
  93. behavior: HitTestBehavior.translucent,
  94. onTap: () async {
  95. final hasAuthenticatedOrNoLocalAuth =
  96. await LocalAuthenticationService.instance
  97. .requestLocalAuthentication(
  98. context,
  99. "Please authenticate to change your password",
  100. );
  101. if (hasAuthenticatedOrNoLocalAuth) {
  102. Navigator.of(context).push(
  103. MaterialPageRoute(
  104. builder: (BuildContext context) {
  105. return const PasswordEntryPage(
  106. mode: PasswordEntryMode.update,
  107. );
  108. },
  109. ),
  110. );
  111. }
  112. },
  113. child: const SettingsTextItem(
  114. text: "Change password",
  115. icon: Icons.navigate_next,
  116. ),
  117. ),
  118. ],
  119. );
  120. }
  121. Future<String> _getOrCreateRecoveryKey() async {
  122. return Sodium.bin2hex(
  123. await UserService.instance.getOrCreateRecoveryKey(context),
  124. );
  125. }
  126. }