dialog_widget.dart 6.0 KB

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