dialog_widget.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. import 'package:photos/core/constants.dart';
  4. import 'package:photos/models/typedefs.dart';
  5. import 'package:photos/theme/colors.dart';
  6. import 'package:photos/theme/effects.dart';
  7. import 'package:photos/theme/ente_theme.dart';
  8. import 'package:photos/ui/components/button_widget.dart';
  9. import 'package:photos/ui/components/models/button_type.dart';
  10. import 'package:photos/ui/components/text_input_widget.dart';
  11. import 'package:photos/utils/separators_util.dart';
  12. ///Will return null if dismissed by tapping outside
  13. Future<ButtonAction?> showDialogWidget({
  14. required BuildContext context,
  15. required String title,
  16. String? body,
  17. required List<ButtonWidget> buttons,
  18. IconData? icon,
  19. bool isDismissible = true,
  20. }) {
  21. return showDialog(
  22. barrierDismissible: isDismissible,
  23. barrierColor: backdropFaintDark,
  24. context: context,
  25. builder: (context) {
  26. final widthOfScreen = MediaQuery.of(context).size.width;
  27. final isMobileSmall = widthOfScreen <= mobileSmallThreshold;
  28. return Padding(
  29. padding: EdgeInsets.symmetric(horizontal: isMobileSmall ? 8 : 0),
  30. child: Dialog(
  31. insetPadding: EdgeInsets.zero,
  32. child: DialogWidget(
  33. title: title,
  34. body: body,
  35. buttons: buttons,
  36. icon: icon,
  37. ),
  38. ),
  39. );
  40. },
  41. );
  42. }
  43. class DialogWidget extends StatelessWidget {
  44. final String title;
  45. final String? body;
  46. final List<ButtonWidget> buttons;
  47. final IconData? icon;
  48. const DialogWidget({
  49. required this.title,
  50. this.body,
  51. required this.buttons,
  52. this.icon,
  53. super.key,
  54. });
  55. @override
  56. Widget build(BuildContext context) {
  57. final widthOfScreen = MediaQuery.of(context).size.width;
  58. final isMobileSmall = widthOfScreen <= mobileSmallThreshold;
  59. final colorScheme = getEnteColorScheme(context);
  60. return Container(
  61. width: min(widthOfScreen, 320),
  62. padding: isMobileSmall
  63. ? const EdgeInsets.all(0)
  64. : const EdgeInsets.fromLTRB(6, 8, 6, 6),
  65. decoration: BoxDecoration(
  66. color: colorScheme.backgroundElevated,
  67. boxShadow: shadowFloatLight,
  68. borderRadius: const BorderRadius.all(Radius.circular(8)),
  69. ),
  70. child: Padding(
  71. padding: const EdgeInsets.all(16),
  72. child: Column(
  73. mainAxisSize: MainAxisSize.min,
  74. children: [
  75. ContentContainer(
  76. title: title,
  77. body: body,
  78. icon: icon,
  79. ),
  80. const SizedBox(height: 36),
  81. Actions(buttons),
  82. ],
  83. ),
  84. ),
  85. );
  86. }
  87. }
  88. class ContentContainer extends StatelessWidget {
  89. final String title;
  90. final String? body;
  91. final IconData? icon;
  92. const ContentContainer({
  93. required this.title,
  94. this.body,
  95. this.icon,
  96. super.key,
  97. });
  98. @override
  99. Widget build(BuildContext context) {
  100. final textTheme = getEnteTextTheme(context);
  101. final colorScheme = getEnteColorScheme(context);
  102. return Column(
  103. mainAxisSize: MainAxisSize.min,
  104. crossAxisAlignment: CrossAxisAlignment.stretch,
  105. children: [
  106. icon == null
  107. ? const SizedBox.shrink()
  108. : Row(
  109. children: [
  110. Icon(
  111. icon,
  112. size: 32,
  113. ),
  114. ],
  115. ),
  116. icon == null ? const SizedBox.shrink() : const SizedBox(height: 19),
  117. Text(title, style: textTheme.largeBold),
  118. body != null ? const SizedBox(height: 19) : const SizedBox.shrink(),
  119. body != null
  120. ? Text(
  121. body!,
  122. style: textTheme.body.copyWith(color: colorScheme.textMuted),
  123. )
  124. : const SizedBox.shrink(),
  125. ],
  126. );
  127. }
  128. }
  129. class Actions extends StatelessWidget {
  130. final List<ButtonWidget> buttons;
  131. const Actions(this.buttons, {super.key});
  132. @override
  133. Widget build(BuildContext context) {
  134. return Column(
  135. children: addSeparators(
  136. buttons,
  137. const SizedBox(
  138. // In figma this white space is of height 8pts. But the Button
  139. // component has 1pts of invisible border by default in code. So two
  140. // 1pts borders will visually make the whitespace 8pts.
  141. // Height of button component in figma = 48, in code = 50 (2pts for
  142. // top + bottom border)
  143. height: 6,
  144. ),
  145. ),
  146. );
  147. }
  148. }
  149. class TextInputDialog extends StatefulWidget {
  150. final String title;
  151. final String? body;
  152. final String submitButtonLabel;
  153. final IconData? icon;
  154. final String? label;
  155. final String? message;
  156. final FutureVoidCallbackParamStr onSubmit;
  157. final String? hintText;
  158. final IconData? prefixIcon;
  159. final String? initialValue;
  160. final Alignment? alignMessage;
  161. final int? maxLength;
  162. final bool showOnlyLoadingState;
  163. final TextCapitalization? textCapitalization;
  164. final bool alwaysShowSuccessState;
  165. const TextInputDialog({
  166. required this.title,
  167. this.body,
  168. required this.submitButtonLabel,
  169. required this.onSubmit,
  170. this.icon,
  171. this.label,
  172. this.message,
  173. this.hintText,
  174. this.prefixIcon,
  175. this.initialValue,
  176. this.alignMessage,
  177. this.maxLength,
  178. this.textCapitalization,
  179. this.showOnlyLoadingState = false,
  180. this.alwaysShowSuccessState = false,
  181. super.key,
  182. });
  183. @override
  184. State<TextInputDialog> createState() => _TextInputDialogState();
  185. }
  186. class _TextInputDialogState extends State<TextInputDialog> {
  187. //the value of this ValueNotifier has no significance
  188. final _submitNotifier = ValueNotifier(false);
  189. @override
  190. void dispose() {
  191. _submitNotifier.dispose();
  192. super.dispose();
  193. }
  194. @override
  195. Widget build(BuildContext context) {
  196. final widthOfScreen = MediaQuery.of(context).size.width;
  197. final isMobileSmall = widthOfScreen <= mobileSmallThreshold;
  198. final colorScheme = getEnteColorScheme(context);
  199. return Container(
  200. width: min(widthOfScreen, 320),
  201. padding: isMobileSmall
  202. ? const EdgeInsets.all(0)
  203. : const EdgeInsets.fromLTRB(6, 8, 6, 6),
  204. decoration: BoxDecoration(
  205. color: colorScheme.backgroundElevated,
  206. boxShadow: shadowFloatLight,
  207. borderRadius: const BorderRadius.all(Radius.circular(8)),
  208. ),
  209. child: Padding(
  210. padding: const EdgeInsets.all(16),
  211. child: Column(
  212. mainAxisSize: MainAxisSize.min,
  213. children: [
  214. ContentContainer(
  215. title: widget.title,
  216. body: widget.body,
  217. icon: widget.icon,
  218. ),
  219. Padding(
  220. padding: const EdgeInsets.only(top: 19),
  221. child: TextInputWidget(
  222. label: widget.label,
  223. message: widget.message,
  224. hintText: widget.hintText,
  225. prefixIcon: widget.prefixIcon,
  226. initialValue: widget.initialValue,
  227. alignMessage: widget.alignMessage,
  228. autoFocus: true,
  229. maxLength: widget.maxLength,
  230. submitNotifier: _submitNotifier,
  231. onSubmit: widget.onSubmit,
  232. popNavAfterSubmission: true,
  233. showOnlyLoadingState: widget.showOnlyLoadingState,
  234. textCapitalization: widget.textCapitalization,
  235. alwaysShowSuccessState: widget.alwaysShowSuccessState,
  236. ),
  237. ),
  238. const SizedBox(height: 36),
  239. Row(
  240. mainAxisSize: MainAxisSize.min,
  241. children: [
  242. const Expanded(
  243. child: ButtonWidget(
  244. buttonType: ButtonType.secondary,
  245. buttonSize: ButtonSize.small,
  246. labelText: "Cancel",
  247. isInAlert: true,
  248. ),
  249. ),
  250. const SizedBox(width: 8),
  251. Expanded(
  252. child: ButtonWidget(
  253. buttonSize: ButtonSize.small,
  254. buttonType: ButtonType.neutral,
  255. labelText: widget.submitButtonLabel,
  256. onTap: () async {
  257. _submitNotifier.value = !_submitNotifier.value;
  258. },
  259. ),
  260. ),
  261. ],
  262. )
  263. ],
  264. ),
  265. ),
  266. );
  267. }
  268. }