danger_section_widget.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import 'package:expandable/expandable.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_email_sender/flutter_email_sender.dart';
  4. import 'package:photos/services/user_service.dart';
  5. import 'package:photos/ui/settings/common_settings.dart';
  6. import 'package:photos/ui/settings/settings_section_title.dart';
  7. import 'package:photos/ui/settings/settings_text_item.dart';
  8. import 'package:url_launcher/url_launcher.dart';
  9. class DangerSectionWidget extends StatefulWidget {
  10. const DangerSectionWidget({Key key}) : super(key: key);
  11. @override
  12. State<DangerSectionWidget> createState() => _DangerSectionWidgetState();
  13. }
  14. class _DangerSectionWidgetState extends State<DangerSectionWidget> {
  15. @override
  16. Widget build(BuildContext context) {
  17. return ExpandablePanel(
  18. header: const SettingsSectionTitle("Exit", color: Colors.red),
  19. collapsed: Container(),
  20. expanded: _getSectionOptions(context),
  21. theme: getExpandableTheme(context),
  22. );
  23. }
  24. Widget _getSectionOptions(BuildContext context) {
  25. return Column(
  26. children: [
  27. GestureDetector(
  28. behavior: HitTestBehavior.translucent,
  29. onTap: () {
  30. _onLogoutTapped();
  31. },
  32. child:
  33. const SettingsTextItem(text: "Logout", icon: Icons.navigate_next),
  34. ),
  35. sectionOptionDivider,
  36. GestureDetector(
  37. behavior: HitTestBehavior.translucent,
  38. onTap: () {
  39. _onDeleteAccountTapped();
  40. },
  41. child: const SettingsTextItem(
  42. text: "Delete account",
  43. icon: Icons.navigate_next,
  44. ),
  45. ),
  46. ],
  47. );
  48. }
  49. Future<void> _onDeleteAccountTapped() async {
  50. AlertDialog alert = AlertDialog(
  51. title: const Text(
  52. "Delete account",
  53. style: TextStyle(
  54. color: Colors.red,
  55. ),
  56. ),
  57. content: RichText(
  58. text: TextSpan(
  59. children: [
  60. const TextSpan(
  61. text: "Please send an email to ",
  62. ),
  63. TextSpan(
  64. text: "account-deletion@ente.io",
  65. style: TextStyle(
  66. color: Colors.orange[300],
  67. ),
  68. ),
  69. const TextSpan(
  70. text:
  71. " from your registered email address.\n\nYour request will be processed within 72 hours.",
  72. ),
  73. ],
  74. style: TextStyle(
  75. color: Theme.of(context).colorScheme.onSurface,
  76. height: 1.5,
  77. fontSize: 16,
  78. ),
  79. ),
  80. ),
  81. actions: [
  82. TextButton(
  83. child: const Text(
  84. "Send email",
  85. style: TextStyle(
  86. color: Colors.red,
  87. ),
  88. ),
  89. onPressed: () async {
  90. Navigator.of(context, rootNavigator: true).pop('dialog');
  91. try {
  92. final Email email = Email(
  93. recipients: ['account-deletion@ente.io'],
  94. isHTML: false,
  95. );
  96. await FlutterEmailSender.send(email);
  97. } catch (e) {
  98. launch("mailto:account-deletion@ente.io");
  99. }
  100. },
  101. ),
  102. TextButton(
  103. child: Text(
  104. "Ok",
  105. style: TextStyle(
  106. color: Theme.of(context).colorScheme.onSurface,
  107. ),
  108. ),
  109. onPressed: () {
  110. Navigator.of(context, rootNavigator: true).pop('dialog');
  111. },
  112. ),
  113. ],
  114. );
  115. showDialog(
  116. context: context,
  117. builder: (BuildContext context) {
  118. return alert;
  119. },
  120. );
  121. }
  122. Future<void> _onLogoutTapped() async {
  123. AlertDialog alert = AlertDialog(
  124. title: const Text(
  125. "Logout",
  126. style: TextStyle(
  127. color: Colors.red,
  128. ),
  129. ),
  130. content: const Text("Are you sure you want to logout?"),
  131. actions: [
  132. TextButton(
  133. child: const Text(
  134. "Yes, logout",
  135. style: TextStyle(
  136. color: Colors.red,
  137. ),
  138. ),
  139. onPressed: () async {
  140. Navigator.of(context, rootNavigator: true).pop('dialog');
  141. await UserService.instance.logout(context);
  142. },
  143. ),
  144. TextButton(
  145. child: Text(
  146. "No",
  147. style: TextStyle(
  148. color: Theme.of(context).buttonColor,
  149. ),
  150. ),
  151. onPressed: () {
  152. Navigator.of(context, rootNavigator: true).pop('dialog');
  153. },
  154. ),
  155. ],
  156. );
  157. showDialog(
  158. context: context,
  159. builder: (BuildContext context) {
  160. return alert;
  161. },
  162. );
  163. }
  164. }