account_section_widget.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.requestAuthentication(
  37. context,
  38. "Please authenticate to view your recovery key",
  39. );
  40. if (hasAuthenticatedOrNoLocalAuth) {
  41. String recoveryKey;
  42. try {
  43. recoveryKey = await _getOrCreateRecoveryKey();
  44. } catch (e) {
  45. showGenericErrorDialog(context);
  46. return;
  47. }
  48. routeToPage(
  49. context,
  50. RecoveryKeyPage(
  51. recoveryKey,
  52. "OK",
  53. showAppBar: true,
  54. onDone: () {},
  55. ),
  56. );
  57. }
  58. },
  59. child: const SettingsTextItem(
  60. text: "Recovery key",
  61. icon: Icons.navigate_next,
  62. ),
  63. ),
  64. sectionOptionDivider,
  65. GestureDetector(
  66. behavior: HitTestBehavior.translucent,
  67. onTap: () async {
  68. final hasAuthenticatedOrNoLocalAuth =
  69. await LocalAuthenticationService.instance.requestAuthentication(
  70. context,
  71. "Please authenticate to change your email",
  72. );
  73. if (hasAuthenticatedOrNoLocalAuth) {
  74. showDialog(
  75. context: context,
  76. builder: (BuildContext context) {
  77. return const ChangeEmailDialog();
  78. },
  79. barrierColor: Colors.black.withOpacity(0.85),
  80. barrierDismissible: false,
  81. );
  82. }
  83. },
  84. child: const SettingsTextItem(
  85. text: "Change email",
  86. icon: Icons.navigate_next,
  87. ),
  88. ),
  89. sectionOptionDivider,
  90. GestureDetector(
  91. behavior: HitTestBehavior.translucent,
  92. onTap: () async {
  93. final hasAuthenticatedOrNoLocalAuth =
  94. await LocalAuthenticationService.instance.requestAuthentication(
  95. context,
  96. "Please authenticate to change your password",
  97. );
  98. if (hasAuthenticatedOrNoLocalAuth) {
  99. Navigator.of(context).push(
  100. MaterialPageRoute(
  101. builder: (BuildContext context) {
  102. return const PasswordEntryPage(
  103. mode: PasswordEntryMode.update,
  104. );
  105. },
  106. ),
  107. );
  108. }
  109. },
  110. child: const SettingsTextItem(
  111. text: "Change password",
  112. icon: Icons.navigate_next,
  113. ),
  114. ),
  115. ],
  116. );
  117. }
  118. Future<String> _getOrCreateRecoveryKey() async {
  119. return Sodium.bin2hex(
  120. await UserService.instance.getOrCreateRecoveryKey(context),
  121. );
  122. }
  123. }