account_section_widget.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'package:expandable/expandable.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_sodium/flutter_sodium.dart';
  4. import 'package:photos/core/configuration.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/ui/tools/app_lock.dart';
  13. import 'package:photos/utils/auth_util.dart';
  14. import 'package:photos/utils/dialog_util.dart';
  15. import 'package:photos/utils/navigation_util.dart';
  16. import 'package:photos/utils/toast_util.dart';
  17. class AccountSectionWidget extends StatefulWidget {
  18. const AccountSectionWidget({Key key}) : super(key: key);
  19. @override
  20. AccountSectionWidgetState createState() => AccountSectionWidgetState();
  21. }
  22. class AccountSectionWidgetState extends State<AccountSectionWidget> {
  23. @override
  24. Widget build(BuildContext context) {
  25. return ExpandablePanel(
  26. header: const SettingsSectionTitle("Account"),
  27. collapsed: Container(),
  28. expanded: _getSectionOptions(context),
  29. theme: getExpandableTheme(context),
  30. );
  31. }
  32. Column _getSectionOptions(BuildContext context) {
  33. return Column(
  34. children: [
  35. GestureDetector(
  36. behavior: HitTestBehavior.translucent,
  37. onTap: () async {
  38. AppLock.of(context).setEnabled(false);
  39. String reason = "Please authenticate to view your recovery key";
  40. final result = await requestAuthentication(reason);
  41. AppLock.of(context)
  42. .setEnabled(Configuration.instance.shouldShowLockScreen());
  43. if (!result) {
  44. showToast(context, reason);
  45. return;
  46. }
  47. String recoveryKey;
  48. try {
  49. recoveryKey = await _getOrCreateRecoveryKey();
  50. } catch (e) {
  51. showGenericErrorDialog(context);
  52. return;
  53. }
  54. routeToPage(
  55. context,
  56. RecoveryKeyPage(
  57. recoveryKey,
  58. "OK",
  59. showAppBar: true,
  60. onDone: () {},
  61. ),
  62. );
  63. },
  64. child: const SettingsTextItem(
  65. text: "Recovery key",
  66. icon: Icons.navigate_next,
  67. ),
  68. ),
  69. sectionOptionDivider,
  70. GestureDetector(
  71. behavior: HitTestBehavior.translucent,
  72. onTap: () async {
  73. AppLock.of(context).setEnabled(false);
  74. String reason = "Please authenticate to change your email";
  75. final result = await requestAuthentication(reason);
  76. AppLock.of(context)
  77. .setEnabled(Configuration.instance.shouldShowLockScreen());
  78. if (!result) {
  79. showToast(context, reason);
  80. return;
  81. }
  82. showDialog(
  83. context: context,
  84. builder: (BuildContext context) {
  85. return const ChangeEmailDialog();
  86. },
  87. barrierColor: Colors.black.withOpacity(0.85),
  88. barrierDismissible: false,
  89. );
  90. },
  91. child: const SettingsTextItem(
  92. text: "Change email",
  93. icon: Icons.navigate_next,
  94. ),
  95. ),
  96. sectionOptionDivider,
  97. GestureDetector(
  98. behavior: HitTestBehavior.translucent,
  99. onTap: () async {
  100. AppLock.of(context).setEnabled(false);
  101. String reason = "Please authenticate to change your password";
  102. final result = await requestAuthentication(reason);
  103. AppLock.of(context)
  104. .setEnabled(Configuration.instance.shouldShowLockScreen());
  105. if (!result) {
  106. showToast(context, reason);
  107. return;
  108. }
  109. Navigator.of(context).push(
  110. MaterialPageRoute(
  111. builder: (BuildContext context) {
  112. return const PasswordEntryPage(
  113. mode: PasswordEntryMode.update,
  114. );
  115. },
  116. ),
  117. );
  118. },
  119. child: const SettingsTextItem(
  120. text: "Change password",
  121. icon: Icons.navigate_next,
  122. ),
  123. ),
  124. ],
  125. );
  126. }
  127. Future<String> _getOrCreateRecoveryKey() async {
  128. return Sodium.bin2hex(
  129. await UserService.instance.getOrCreateRecoveryKey(context),
  130. );
  131. }
  132. }