dialogs.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import 'package:easy_localization/easy_localization.dart';
  2. import 'package:flowy_infra_ui/style_widget/text.dart';
  3. import 'package:flowy_infra_ui/widget/buttons/primary_button.dart';
  4. import 'package:flowy_infra_ui/widget/buttons/secondary_button.dart';
  5. import 'package:flowy_infra_ui/widget/spacing.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:appflowy/startup/tasks/app_widget.dart';
  8. import 'package:flowy_infra/size.dart';
  9. import 'package:flowy_infra_ui/style_widget/text_input.dart';
  10. import 'package:flowy_infra_ui/widget/dialog/styled_dialogs.dart';
  11. export 'package:flowy_infra_ui/widget/dialog/styled_dialogs.dart';
  12. import 'package:appflowy/generated/locale_keys.g.dart';
  13. class NavigatorTextFieldDialog extends StatefulWidget {
  14. const NavigatorTextFieldDialog({
  15. super.key,
  16. required this.title,
  17. this.autoSelectAllText = false,
  18. required this.value,
  19. required this.confirm,
  20. this.cancel,
  21. });
  22. final String value;
  23. final String title;
  24. final void Function()? cancel;
  25. final void Function(String) confirm;
  26. final bool autoSelectAllText;
  27. @override
  28. State<NavigatorTextFieldDialog> createState() =>
  29. _NavigatorTextFieldDialogState();
  30. }
  31. class _NavigatorTextFieldDialogState extends State<NavigatorTextFieldDialog> {
  32. String newValue = "";
  33. final controller = TextEditingController();
  34. @override
  35. void initState() {
  36. newValue = widget.value;
  37. controller.text = newValue;
  38. if (widget.autoSelectAllText) {
  39. controller.selection = TextSelection(
  40. baseOffset: 0,
  41. extentOffset: newValue.length,
  42. );
  43. }
  44. super.initState();
  45. }
  46. @override
  47. Widget build(BuildContext context) {
  48. return StyledDialog(
  49. child: Column(
  50. children: <Widget>[
  51. FlowyText.medium(
  52. widget.title,
  53. color: Theme.of(context).colorScheme.tertiary,
  54. fontSize: FontSizes.s16,
  55. ),
  56. VSpace(Insets.m),
  57. FlowyFormTextInput(
  58. textAlign: TextAlign.center,
  59. hintText: LocaleKeys.dialogCreatePageNameHint.tr(),
  60. controller: controller,
  61. textStyle: Theme.of(context).textTheme.bodySmall?.copyWith(
  62. fontSize: FontSizes.s16,
  63. ),
  64. autoFocus: true,
  65. onChanged: (text) {
  66. newValue = text;
  67. },
  68. onEditingComplete: () {
  69. widget.confirm(newValue);
  70. AppGlobals.nav.pop();
  71. },
  72. ),
  73. VSpace(Insets.xl),
  74. OkCancelButton(
  75. onOkPressed: () {
  76. widget.confirm(newValue);
  77. Navigator.of(context).pop();
  78. },
  79. onCancelPressed: () {
  80. if (widget.cancel != null) {
  81. widget.cancel!();
  82. }
  83. Navigator.of(context).pop();
  84. },
  85. )
  86. ],
  87. ),
  88. );
  89. }
  90. }
  91. class NavigatorAlertDialog extends StatefulWidget {
  92. final String title;
  93. final void Function()? cancel;
  94. final void Function()? confirm;
  95. const NavigatorAlertDialog({
  96. required this.title,
  97. this.confirm,
  98. this.cancel,
  99. Key? key,
  100. }) : super(key: key);
  101. @override
  102. State<NavigatorAlertDialog> createState() => _CreateFlowyAlertDialog();
  103. }
  104. class _CreateFlowyAlertDialog extends State<NavigatorAlertDialog> {
  105. @override
  106. void initState() {
  107. super.initState();
  108. }
  109. @override
  110. Widget build(BuildContext context) {
  111. return StyledDialog(
  112. child: Column(
  113. crossAxisAlignment: CrossAxisAlignment.start,
  114. mainAxisAlignment: MainAxisAlignment.center,
  115. children: <Widget>[
  116. ...[
  117. ConstrainedBox(
  118. constraints: const BoxConstraints(
  119. maxWidth: 400,
  120. maxHeight: 260,
  121. ),
  122. child: FlowyText.medium(
  123. widget.title,
  124. fontSize: FontSizes.s16,
  125. color: Theme.of(context).colorScheme.tertiary,
  126. maxLines: null,
  127. ),
  128. ),
  129. ],
  130. if (widget.confirm != null) ...[
  131. const VSpace(20),
  132. OkCancelButton(
  133. onOkPressed: () {
  134. widget.confirm?.call();
  135. Navigator.of(context).pop();
  136. },
  137. onCancelPressed: () {
  138. widget.cancel?.call();
  139. Navigator.of(context).pop();
  140. },
  141. )
  142. ]
  143. ],
  144. ),
  145. );
  146. }
  147. }
  148. class NavigatorOkCancelDialog extends StatelessWidget {
  149. final VoidCallback? onOkPressed;
  150. final VoidCallback? onCancelPressed;
  151. final String? okTitle;
  152. final String? cancelTitle;
  153. final String? title;
  154. final String message;
  155. final double? maxWidth;
  156. const NavigatorOkCancelDialog({
  157. Key? key,
  158. this.onOkPressed,
  159. this.onCancelPressed,
  160. this.okTitle,
  161. this.cancelTitle,
  162. this.title,
  163. required this.message,
  164. this.maxWidth,
  165. }) : super(key: key);
  166. @override
  167. Widget build(BuildContext context) {
  168. return StyledDialog(
  169. maxWidth: maxWidth ?? 500,
  170. child: Column(
  171. crossAxisAlignment: CrossAxisAlignment.start,
  172. children: <Widget>[
  173. if (title != null) ...[
  174. FlowyText.medium(
  175. title!.toUpperCase(),
  176. fontSize: FontSizes.s16,
  177. ),
  178. VSpace(Insets.sm * 1.5),
  179. Container(
  180. color: Theme.of(context).colorScheme.surfaceVariant,
  181. height: 1,
  182. ),
  183. VSpace(Insets.m * 1.5),
  184. ],
  185. FlowyText.medium(message),
  186. SizedBox(height: Insets.l),
  187. OkCancelButton(
  188. onOkPressed: () {
  189. onOkPressed?.call();
  190. Navigator.of(context).pop();
  191. },
  192. onCancelPressed: () {
  193. onCancelPressed?.call();
  194. Navigator.of(context).pop();
  195. },
  196. okTitle: okTitle?.toUpperCase(),
  197. cancelTitle: cancelTitle?.toUpperCase(),
  198. )
  199. ],
  200. ),
  201. );
  202. }
  203. }
  204. class OkCancelButton extends StatelessWidget {
  205. final VoidCallback? onOkPressed;
  206. final VoidCallback? onCancelPressed;
  207. final String? okTitle;
  208. final String? cancelTitle;
  209. final double? minHeight;
  210. final MainAxisAlignment alignment;
  211. final TextButtonMode mode;
  212. const OkCancelButton({
  213. Key? key,
  214. this.onOkPressed,
  215. this.onCancelPressed,
  216. this.okTitle,
  217. this.cancelTitle,
  218. this.minHeight,
  219. this.alignment = MainAxisAlignment.spaceAround,
  220. this.mode = TextButtonMode.big,
  221. }) : super(key: key);
  222. @override
  223. Widget build(BuildContext context) {
  224. return SizedBox(
  225. height: 48,
  226. child: Row(
  227. mainAxisAlignment: alignment,
  228. children: <Widget>[
  229. if (onCancelPressed != null)
  230. SecondaryTextButton(
  231. cancelTitle ?? LocaleKeys.button_Cancel.tr(),
  232. onPressed: onCancelPressed,
  233. mode: mode,
  234. ),
  235. HSpace(Insets.m),
  236. if (onOkPressed != null)
  237. PrimaryTextButton(
  238. okTitle ?? LocaleKeys.button_OK.tr(),
  239. onPressed: onOkPressed,
  240. mode: mode,
  241. ),
  242. ],
  243. ),
  244. );
  245. }
  246. }