dialog_widget.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. import 'package:photos/core/constants.dart';
  4. import 'package:photos/theme/colors.dart';
  5. import 'package:photos/theme/effects.dart';
  6. import 'package:photos/theme/ente_theme.dart';
  7. import 'package:photos/ui/components/button_widget.dart';
  8. import 'package:photos/ui/components/models/button_type.dart';
  9. import 'package:photos/utils/separators_util.dart';
  10. Future<ButtonAction?> showGenericErrorDialog({
  11. required BuildContext context,
  12. }) async {
  13. return showDialogWidget(
  14. context: context,
  15. title: "Error",
  16. icon: Icons.error_outline_outlined,
  17. body: "It looks like something went wrong. Please try again.",
  18. buttons: const [
  19. ButtonWidget(
  20. buttonType: ButtonType.secondary,
  21. labelText: "OK",
  22. isInAlert: true,
  23. ),
  24. ],
  25. );
  26. }
  27. Future<ButtonAction?> showNewChoiceDialog({
  28. required BuildContext context,
  29. required String title,
  30. required String body,
  31. required String firstButtonLabel,
  32. String secondButtonLabel = "Cancel",
  33. ButtonType firstButtonType = ButtonType.neutral,
  34. ButtonType secondButtonType = ButtonType.secondary,
  35. ButtonAction firstButtonAction = ButtonAction.first,
  36. ButtonAction secondButtonAction = ButtonAction.cancel,
  37. FutureVoidCallback? firstButtonOnTap,
  38. FutureVoidCallback? secondButtonOnTap,
  39. bool isCritical = false,
  40. IconData? icon,
  41. }) async {
  42. final buttons = [
  43. ButtonWidget(
  44. buttonType: isCritical ? ButtonType.critical : firstButtonType,
  45. labelText: firstButtonLabel,
  46. isInAlert: true,
  47. onTap: firstButtonOnTap,
  48. buttonAction: firstButtonAction,
  49. ),
  50. ButtonWidget(
  51. buttonType: secondButtonType,
  52. labelText: secondButtonLabel,
  53. isInAlert: true,
  54. onTap: secondButtonOnTap,
  55. buttonAction: secondButtonAction,
  56. ),
  57. ];
  58. return showDialogWidget(
  59. context: context,
  60. title: title,
  61. body: body,
  62. buttons: buttons,
  63. );
  64. }
  65. Future<ButtonAction?> showDialogWidget({
  66. required BuildContext context,
  67. required String title,
  68. required String body,
  69. required List<ButtonWidget> buttons,
  70. IconData? icon,
  71. }) {
  72. return showDialog(
  73. barrierDismissible: false,
  74. barrierColor: backdropFaintDark,
  75. context: context,
  76. builder: (context) {
  77. final widthOfScreen = MediaQuery.of(context).size.width;
  78. final isMobileSmall = widthOfScreen <= mobileSmallThreshold;
  79. return Padding(
  80. padding: EdgeInsets.symmetric(horizontal: isMobileSmall ? 8 : 0),
  81. child: Dialog(
  82. insetPadding: EdgeInsets.zero,
  83. child: DialogWidget(
  84. title: title,
  85. body: body,
  86. buttons: buttons,
  87. isMobileSmall: isMobileSmall,
  88. icon: icon,
  89. ),
  90. ),
  91. );
  92. },
  93. );
  94. }
  95. class DialogWidget extends StatelessWidget {
  96. final String title;
  97. final String body;
  98. final List<ButtonWidget> buttons;
  99. final IconData? icon;
  100. final bool isMobileSmall;
  101. const DialogWidget({
  102. required this.title,
  103. required this.body,
  104. required this.buttons,
  105. required this.isMobileSmall,
  106. this.icon,
  107. super.key,
  108. });
  109. @override
  110. Widget build(BuildContext context) {
  111. final widthOfScreen = MediaQuery.of(context).size.width;
  112. final colorScheme = getEnteColorScheme(context);
  113. return Container(
  114. width: min(widthOfScreen, 320),
  115. padding: isMobileSmall
  116. ? const EdgeInsets.all(0)
  117. : const EdgeInsets.fromLTRB(6, 8, 6, 6),
  118. decoration: BoxDecoration(
  119. color: colorScheme.backgroundElevated,
  120. boxShadow: shadowFloatLight,
  121. borderRadius: const BorderRadius.all(Radius.circular(8)),
  122. ),
  123. child: Padding(
  124. padding: const EdgeInsets.all(16),
  125. child: Column(
  126. mainAxisSize: MainAxisSize.min,
  127. children: [
  128. ContentContainer(
  129. title: title,
  130. body: body,
  131. icon: icon,
  132. ),
  133. const SizedBox(height: 36),
  134. Actions(buttons),
  135. ],
  136. ),
  137. ),
  138. );
  139. }
  140. }
  141. class ContentContainer extends StatelessWidget {
  142. final String title;
  143. final String body;
  144. final IconData? icon;
  145. const ContentContainer({
  146. required this.title,
  147. required this.body,
  148. this.icon,
  149. super.key,
  150. });
  151. @override
  152. Widget build(BuildContext context) {
  153. final textTheme = getEnteTextTheme(context);
  154. final colorScheme = getEnteColorScheme(context);
  155. return Column(
  156. mainAxisSize: MainAxisSize.min,
  157. crossAxisAlignment: CrossAxisAlignment.stretch,
  158. children: [
  159. icon == null
  160. ? const SizedBox.shrink()
  161. : Row(
  162. children: [
  163. Icon(
  164. icon,
  165. size: 48,
  166. ),
  167. ],
  168. ),
  169. icon == null ? const SizedBox.shrink() : const SizedBox(height: 19),
  170. Text(title, style: textTheme.h3Bold),
  171. const SizedBox(height: 19),
  172. Text(
  173. body,
  174. style: textTheme.body.copyWith(color: colorScheme.textMuted),
  175. ),
  176. ],
  177. );
  178. }
  179. }
  180. class Actions extends StatelessWidget {
  181. final List<ButtonWidget> buttons;
  182. const Actions(this.buttons, {super.key});
  183. @override
  184. Widget build(BuildContext context) {
  185. return Column(
  186. children: addSeparators(
  187. buttons,
  188. const SizedBox(
  189. height: 8,
  190. ),
  191. ),
  192. );
  193. }
  194. }