dialog_widget.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. const TextInputDialog({
  167. required this.title,
  168. this.body,
  169. required this.submitButtonLabel,
  170. required this.onSubmit,
  171. this.icon,
  172. this.label,
  173. this.message,
  174. this.hintText,
  175. this.prefixIcon,
  176. this.initialValue,
  177. this.alignMessage,
  178. this.maxLength,
  179. this.textCapitalization,
  180. this.showOnlyLoadingState = false,
  181. this.alwaysShowSuccessState = false,
  182. super.key,
  183. });
  184. @override
  185. State<TextInputDialog> createState() => _TextInputDialogState();
  186. }
  187. class _TextInputDialogState extends State<TextInputDialog> {
  188. //the value of this ValueNotifier has no significance
  189. final _submitNotifier = ValueNotifier(false);
  190. @override
  191. void dispose() {
  192. _submitNotifier.dispose();
  193. super.dispose();
  194. }
  195. @override
  196. Widget build(BuildContext context) {
  197. final widthOfScreen = MediaQuery.of(context).size.width;
  198. final isMobileSmall = widthOfScreen <= mobileSmallThreshold;
  199. final colorScheme = getEnteColorScheme(context);
  200. return Container(
  201. width: min(widthOfScreen, 320),
  202. padding: isMobileSmall
  203. ? const EdgeInsets.all(0)
  204. : const EdgeInsets.fromLTRB(6, 8, 6, 6),
  205. decoration: BoxDecoration(
  206. color: colorScheme.backgroundElevated,
  207. boxShadow: shadowFloatLight,
  208. borderRadius: const BorderRadius.all(Radius.circular(8)),
  209. ),
  210. child: Padding(
  211. padding: const EdgeInsets.all(16),
  212. child: Column(
  213. mainAxisSize: MainAxisSize.min,
  214. children: [
  215. ContentContainer(
  216. title: widget.title,
  217. body: widget.body,
  218. icon: widget.icon,
  219. ),
  220. Padding(
  221. padding: const EdgeInsets.only(top: 19),
  222. child: TextInputWidget(
  223. label: widget.label,
  224. message: widget.message,
  225. hintText: widget.hintText,
  226. prefixIcon: widget.prefixIcon,
  227. initialValue: widget.initialValue,
  228. alignMessage: widget.alignMessage,
  229. autoFocus: true,
  230. maxLength: widget.maxLength,
  231. submitNotifier: _submitNotifier,
  232. onSubmit: widget.onSubmit,
  233. popNavAfterSubmission: true,
  234. showOnlyLoadingState: widget.showOnlyLoadingState,
  235. textCapitalization: widget.textCapitalization,
  236. alwaysShowSuccessState: widget.alwaysShowSuccessState,
  237. ),
  238. ),
  239. const SizedBox(height: 36),
  240. Row(
  241. mainAxisSize: MainAxisSize.min,
  242. children: [
  243. const Expanded(
  244. child: ButtonWidget(
  245. buttonType: ButtonType.secondary,
  246. buttonSize: ButtonSize.small,
  247. labelText: "Cancel",
  248. isInAlert: true,
  249. ),
  250. ),
  251. const SizedBox(width: 8),
  252. Expanded(
  253. child: ButtonWidget(
  254. buttonSize: ButtonSize.small,
  255. buttonType: ButtonType.neutral,
  256. labelText: widget.submitButtonLabel,
  257. onTap: () async {
  258. _submitNotifier.value = !_submitNotifier.value;
  259. },
  260. ),
  261. ),
  262. ],
  263. )
  264. ],
  265. ),
  266. ),
  267. );
  268. }
  269. }