dialog_widget.dart 8.4 KB

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