dialogs.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. FlowyText.medium(
  118. widget.title,
  119. fontSize: FontSizes.s16,
  120. color: Theme.of(context).colorScheme.tertiary,
  121. ),
  122. ],
  123. if (widget.confirm != null) ...[
  124. const VSpace(20),
  125. OkCancelButton(
  126. onOkPressed: () {
  127. widget.confirm?.call();
  128. Navigator.of(context).pop();
  129. },
  130. onCancelPressed: () {
  131. widget.cancel?.call();
  132. Navigator.of(context).pop();
  133. },
  134. )
  135. ]
  136. ],
  137. ),
  138. );
  139. }
  140. }
  141. class NavigatorOkCancelDialog extends StatelessWidget {
  142. final VoidCallback? onOkPressed;
  143. final VoidCallback? onCancelPressed;
  144. final String? okTitle;
  145. final String? cancelTitle;
  146. final String? title;
  147. final String message;
  148. final double? maxWidth;
  149. const NavigatorOkCancelDialog({
  150. Key? key,
  151. this.onOkPressed,
  152. this.onCancelPressed,
  153. this.okTitle,
  154. this.cancelTitle,
  155. this.title,
  156. required this.message,
  157. this.maxWidth,
  158. }) : super(key: key);
  159. @override
  160. Widget build(BuildContext context) {
  161. return StyledDialog(
  162. maxWidth: maxWidth ?? 500,
  163. child: Column(
  164. crossAxisAlignment: CrossAxisAlignment.start,
  165. children: <Widget>[
  166. if (title != null) ...[
  167. FlowyText.medium(
  168. title!.toUpperCase(),
  169. fontSize: FontSizes.s16,
  170. ),
  171. VSpace(Insets.sm * 1.5),
  172. Container(
  173. color: Theme.of(context).colorScheme.surfaceVariant,
  174. height: 1,
  175. ),
  176. VSpace(Insets.m * 1.5),
  177. ],
  178. FlowyText.medium(message),
  179. SizedBox(height: Insets.l),
  180. OkCancelButton(
  181. onOkPressed: () {
  182. onOkPressed?.call();
  183. Navigator.of(context).pop();
  184. },
  185. onCancelPressed: () {
  186. onCancelPressed?.call();
  187. Navigator.of(context).pop();
  188. },
  189. okTitle: okTitle?.toUpperCase(),
  190. cancelTitle: cancelTitle?.toUpperCase(),
  191. )
  192. ],
  193. ),
  194. );
  195. }
  196. }
  197. class OkCancelButton extends StatelessWidget {
  198. final VoidCallback? onOkPressed;
  199. final VoidCallback? onCancelPressed;
  200. final String? okTitle;
  201. final String? cancelTitle;
  202. final double? minHeight;
  203. final MainAxisAlignment alignment;
  204. const OkCancelButton({
  205. Key? key,
  206. this.onOkPressed,
  207. this.onCancelPressed,
  208. this.okTitle,
  209. this.cancelTitle,
  210. this.minHeight,
  211. this.alignment = MainAxisAlignment.spaceAround,
  212. }) : super(key: key);
  213. @override
  214. Widget build(BuildContext context) {
  215. return SizedBox(
  216. height: 48,
  217. child: Row(
  218. mainAxisAlignment: alignment,
  219. children: <Widget>[
  220. if (onCancelPressed != null)
  221. SecondaryTextButton(
  222. cancelTitle ?? LocaleKeys.button_Cancel.tr(),
  223. onPressed: onCancelPressed,
  224. mode: SecondaryTextButtonMode.big,
  225. ),
  226. HSpace(Insets.m),
  227. if (onOkPressed != null)
  228. PrimaryTextButton(
  229. okTitle ?? LocaleKeys.button_OK.tr(),
  230. onPressed: onOkPressed,
  231. bigMode: true,
  232. ),
  233. ],
  234. ),
  235. );
  236. }
  237. }