account_section_widget.dart 4.1 KB

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