dialog_widget.dart 7.5 KB

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