account_section_widget.dart 4.5 KB

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