account_section_widget.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // @dart=2.9
  2. import 'package:expandable/expandable.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_sodium/flutter_sodium.dart';
  5. import 'package:photos/ente_theme_data.dart';
  6. import 'package:photos/services/local_authentication_service.dart';
  7. import 'package:photos/services/user_service.dart';
  8. import 'package:photos/ui/account/change_email_dialog.dart';
  9. import 'package:photos/ui/account/password_entry_page.dart';
  10. import 'package:photos/ui/account/recovery_key_page.dart';
  11. import 'package:photos/ui/components/captioned_text_widget.dart';
  12. import 'package:photos/ui/components/menu_item_widget.dart';
  13. import 'package:photos/ui/settings/common_settings.dart';
  14. import 'package:photos/utils/dialog_util.dart';
  15. import 'package:photos/utils/navigation_util.dart';
  16. class AccountSectionWidget extends StatefulWidget {
  17. const AccountSectionWidget({Key key}) : super(key: key);
  18. @override
  19. AccountSectionWidgetState createState() => AccountSectionWidgetState();
  20. }
  21. class AccountSectionWidgetState extends State<AccountSectionWidget> {
  22. final expandableController = ExpandableController(initialExpanded: false);
  23. @override
  24. void dispose() {
  25. expandableController.dispose();
  26. super.dispose();
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return ExpandablePanel(
  31. header: MenuItemWidget(
  32. captionedTextWidget: const CaptionedTextWidget(
  33. text: "Account",
  34. makeTextBold: true,
  35. ),
  36. isHeaderOfExpansion: true,
  37. leadingIcon: Icons.account_circle_outlined,
  38. trailingIcon: Icons.expand_more,
  39. menuItemColor:
  40. Theme.of(context).colorScheme.enteTheme.colorScheme.fillFaint,
  41. expandableController: expandableController,
  42. ),
  43. collapsed: const SizedBox.shrink(),
  44. expanded: _getSectionOptions(context),
  45. theme: getExpandableTheme(context),
  46. controller: expandableController,
  47. );
  48. }
  49. Column _getSectionOptions(BuildContext context) {
  50. return Column(
  51. children: [
  52. sectionOptionDivider,
  53. MenuItemWidget(
  54. captionedTextWidget: const CaptionedTextWidget(
  55. text: "Recovery key",
  56. ),
  57. trailingIcon: Icons.chevron_right_outlined,
  58. trailingIconIsMuted: true,
  59. onTap: () async {
  60. final hasAuthenticated = await LocalAuthenticationService.instance
  61. .requestLocalAuthentication(
  62. context,
  63. "Please authenticate to view your recovery key",
  64. );
  65. if (hasAuthenticated) {
  66. String recoveryKey;
  67. try {
  68. recoveryKey = await _getOrCreateRecoveryKey();
  69. } catch (e) {
  70. showGenericErrorDialog(context);
  71. return;
  72. }
  73. routeToPage(
  74. context,
  75. RecoveryKeyPage(
  76. recoveryKey,
  77. "OK",
  78. showAppBar: true,
  79. onDone: () {},
  80. ),
  81. );
  82. }
  83. },
  84. ),
  85. MenuItemWidget(
  86. captionedTextWidget: const CaptionedTextWidget(
  87. text: "Change email",
  88. ),
  89. trailingIcon: Icons.chevron_right_outlined,
  90. trailingIconIsMuted: true,
  91. onTap: () async {
  92. final hasAuthenticated = await LocalAuthenticationService.instance
  93. .requestLocalAuthentication(
  94. context,
  95. "Please authenticate to change your email",
  96. );
  97. if (hasAuthenticated) {
  98. showDialog(
  99. context: context,
  100. builder: (BuildContext context) {
  101. return const ChangeEmailDialog();
  102. },
  103. barrierColor: Colors.black.withOpacity(0.85),
  104. barrierDismissible: false,
  105. );
  106. }
  107. },
  108. ),
  109. MenuItemWidget(
  110. captionedTextWidget: const CaptionedTextWidget(
  111. text: "Change password",
  112. ),
  113. trailingIcon: Icons.chevron_right_outlined,
  114. trailingIconIsMuted: true,
  115. onTap: () async {
  116. final hasAuthenticated = await LocalAuthenticationService.instance
  117. .requestLocalAuthentication(
  118. context,
  119. "Please authenticate to change your password",
  120. );
  121. if (hasAuthenticated) {
  122. Navigator.of(context).push(
  123. MaterialPageRoute(
  124. builder: (BuildContext context) {
  125. return const PasswordEntryPage(
  126. mode: PasswordEntryMode.update,
  127. );
  128. },
  129. ),
  130. );
  131. }
  132. },
  133. ),
  134. ],
  135. );
  136. }
  137. Future<String> _getOrCreateRecoveryKey() async {
  138. return Sodium.bin2hex(
  139. await UserService.instance.getOrCreateRecoveryKey(context),
  140. );
  141. }
  142. }