dialog_widget.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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?> showErrorDialog(
  11. BuildContext context,
  12. String title,
  13. String body, {
  14. bool isDismissable = true,
  15. }) async {
  16. return showDialogWidget(
  17. context: context,
  18. title: title,
  19. body: body,
  20. isDismissible: isDismissable,
  21. buttons: const [
  22. ButtonWidget(
  23. buttonType: ButtonType.secondary,
  24. labelText: "OK",
  25. isInAlert: true,
  26. buttonAction: ButtonAction.first,
  27. ),
  28. ],
  29. );
  30. }
  31. ///Will return null if dismissed by tapping outside
  32. Future<ButtonAction?> showGenericErrorDialog({
  33. required BuildContext context,
  34. bool isDismissible = true,
  35. }) async {
  36. return showDialogWidget(
  37. context: context,
  38. title: "Error",
  39. icon: Icons.error_outline_outlined,
  40. body: "It looks like something went wrong. Please try again.",
  41. isDismissible: isDismissible,
  42. buttons: const [
  43. ButtonWidget(
  44. buttonType: ButtonType.secondary,
  45. labelText: "OK",
  46. isInAlert: true,
  47. ),
  48. ],
  49. );
  50. }
  51. ///Will return null if dismissed by tapping outside
  52. Future<ButtonAction?> showNewChoiceDialog({
  53. required BuildContext context,
  54. required String title,
  55. String? body,
  56. required String firstButtonLabel,
  57. String secondButtonLabel = "Cancel",
  58. ButtonType firstButtonType = ButtonType.neutral,
  59. ButtonType secondButtonType = ButtonType.secondary,
  60. ButtonAction firstButtonAction = ButtonAction.first,
  61. ButtonAction secondButtonAction = ButtonAction.cancel,
  62. FutureVoidCallback? firstButtonOnTap,
  63. FutureVoidCallback? secondButtonOnTap,
  64. bool isCritical = false,
  65. IconData? icon,
  66. bool isDismissible = true,
  67. }) async {
  68. final buttons = [
  69. ButtonWidget(
  70. buttonType: isCritical ? ButtonType.critical : firstButtonType,
  71. labelText: firstButtonLabel,
  72. isInAlert: true,
  73. onTap: firstButtonOnTap,
  74. buttonAction: firstButtonAction,
  75. ),
  76. ButtonWidget(
  77. buttonType: secondButtonType,
  78. labelText: secondButtonLabel,
  79. isInAlert: true,
  80. onTap: secondButtonOnTap,
  81. buttonAction: secondButtonAction,
  82. ),
  83. ];
  84. return showDialogWidget(
  85. context: context,
  86. title: title,
  87. body: body,
  88. buttons: buttons,
  89. icon: icon,
  90. isDismissible: isDismissible,
  91. );
  92. }
  93. ///Will return null if dismissed by tapping outside
  94. Future<ButtonAction?> showDialogWidget({
  95. required BuildContext context,
  96. required String title,
  97. String? body,
  98. required List<ButtonWidget> buttons,
  99. IconData? icon,
  100. bool isDismissible = true,
  101. }) {
  102. return showDialog(
  103. barrierDismissible: isDismissible,
  104. barrierColor: backdropFaintDark,
  105. context: context,
  106. builder: (context) {
  107. final widthOfScreen = MediaQuery.of(context).size.width;
  108. final isMobileSmall = widthOfScreen <= mobileSmallThreshold;
  109. return Padding(
  110. padding: EdgeInsets.symmetric(horizontal: isMobileSmall ? 8 : 0),
  111. child: Dialog(
  112. insetPadding: EdgeInsets.zero,
  113. child: DialogWidget(
  114. title: title,
  115. body: body,
  116. buttons: buttons,
  117. isMobileSmall: isMobileSmall,
  118. icon: icon,
  119. ),
  120. ),
  121. );
  122. },
  123. );
  124. }
  125. class DialogWidget extends StatelessWidget {
  126. final String title;
  127. final String? body;
  128. final List<ButtonWidget> buttons;
  129. final IconData? icon;
  130. final bool isMobileSmall;
  131. const DialogWidget({
  132. required this.title,
  133. this.body,
  134. required this.buttons,
  135. required this.isMobileSmall,
  136. this.icon,
  137. super.key,
  138. });
  139. @override
  140. Widget build(BuildContext context) {
  141. final widthOfScreen = MediaQuery.of(context).size.width;
  142. final colorScheme = getEnteColorScheme(context);
  143. return Container(
  144. width: min(widthOfScreen, 320),
  145. padding: isMobileSmall
  146. ? const EdgeInsets.all(0)
  147. : const EdgeInsets.fromLTRB(6, 8, 6, 6),
  148. decoration: BoxDecoration(
  149. color: colorScheme.backgroundElevated,
  150. boxShadow: shadowFloatLight,
  151. borderRadius: const BorderRadius.all(Radius.circular(8)),
  152. ),
  153. child: Padding(
  154. padding: const EdgeInsets.all(16),
  155. child: Column(
  156. mainAxisSize: MainAxisSize.min,
  157. children: [
  158. ContentContainer(
  159. title: title,
  160. body: body,
  161. icon: icon,
  162. ),
  163. const SizedBox(height: 36),
  164. Actions(buttons),
  165. ],
  166. ),
  167. ),
  168. );
  169. }
  170. }
  171. class ContentContainer extends StatelessWidget {
  172. final String title;
  173. final String? body;
  174. final IconData? icon;
  175. const ContentContainer({
  176. required this.title,
  177. this.body,
  178. this.icon,
  179. super.key,
  180. });
  181. @override
  182. Widget build(BuildContext context) {
  183. final textTheme = getEnteTextTheme(context);
  184. final colorScheme = getEnteColorScheme(context);
  185. return Column(
  186. mainAxisSize: MainAxisSize.min,
  187. crossAxisAlignment: CrossAxisAlignment.stretch,
  188. children: [
  189. icon == null
  190. ? const SizedBox.shrink()
  191. : Row(
  192. children: [
  193. Icon(
  194. icon,
  195. size: 48,
  196. ),
  197. ],
  198. ),
  199. icon == null ? const SizedBox.shrink() : const SizedBox(height: 19),
  200. Text(title, style: textTheme.h3Bold),
  201. body != null ? const SizedBox(height: 19) : const SizedBox.shrink(),
  202. body != null
  203. ? Text(
  204. body!,
  205. style: textTheme.body.copyWith(color: colorScheme.textMuted),
  206. )
  207. : const SizedBox.shrink(),
  208. ],
  209. );
  210. }
  211. }
  212. class Actions extends StatelessWidget {
  213. final List<ButtonWidget> buttons;
  214. const Actions(this.buttons, {super.key});
  215. @override
  216. Widget build(BuildContext context) {
  217. return Column(
  218. children: addSeparators(
  219. buttons,
  220. const SizedBox(
  221. // In figma this white space is of height 8pts. But the Button
  222. // component has 1pts of invisible border by default in code. So two
  223. // 1pts borders will visually make the whitespace 8pts.
  224. // Height of button component in figma = 48, in code = 50 (2pts for
  225. // top + bottom border)
  226. height: 6,
  227. ),
  228. ),
  229. );
  230. }
  231. }