dialogs.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import 'package:easy_localization/easy_localization.dart';
  2. import 'package:flowy_infra/text_style.dart';
  3. import 'package:flowy_infra/theme.dart';
  4. import 'package:flowy_infra_ui/style_widget/text.dart';
  5. import 'package:flowy_infra_ui/widget/buttons/primary_button.dart';
  6. import 'package:flowy_infra_ui/widget/buttons/secondary_button.dart';
  7. import 'package:flowy_infra_ui/widget/spacing.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:provider/provider.dart';
  10. import 'package:app_flowy/startup/tasks/app_widget.dart';
  11. import 'package:flowy_infra/size.dart';
  12. import 'package:flowy_infra_ui/style_widget/text_input.dart';
  13. import 'package:flowy_infra_ui/widget/dialog/styled_dialogs.dart';
  14. import 'package:textstyle_extensions/textstyle_extensions.dart';
  15. export 'package:flowy_infra_ui/widget/dialog/styled_dialogs.dart';
  16. import 'package:app_flowy/generated/locale_keys.g.dart';
  17. class TextFieldDialog extends StatefulWidget {
  18. final String value;
  19. final String title;
  20. final void Function()? cancel;
  21. final void Function(String) confirm;
  22. const TextFieldDialog({
  23. required this.title,
  24. required this.value,
  25. required this.confirm,
  26. this.cancel,
  27. Key? key,
  28. }) : super(key: key);
  29. @override
  30. State<TextFieldDialog> createState() => _CreateTextFieldDialog();
  31. }
  32. class _CreateTextFieldDialog extends State<TextFieldDialog> {
  33. String newValue = "";
  34. @override
  35. void initState() {
  36. newValue = widget.value;
  37. super.initState();
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. final theme = context.watch<AppTheme>();
  42. return StyledDialog(
  43. child: Column(
  44. crossAxisAlignment: CrossAxisAlignment.start,
  45. children: <Widget>[
  46. ...[
  47. FlowyText.medium(widget.title, color: theme.shader4),
  48. VSpace(Insets.sm * 1.5),
  49. ],
  50. FlowyFormTextInput(
  51. hintText: LocaleKeys.dialogCreatePageNameHint.tr(),
  52. initialValue: widget.value,
  53. textStyle:
  54. const TextStyle(fontSize: 24, fontWeight: FontWeight.w400),
  55. autoFocus: true,
  56. onChanged: (text) {
  57. newValue = text;
  58. },
  59. onEditingComplete: () {
  60. widget.confirm(newValue);
  61. AppGlobals.nav.pop();
  62. },
  63. ),
  64. const VSpace(10),
  65. OkCancelButton(
  66. onOkPressed: () {
  67. widget.confirm(newValue);
  68. },
  69. onCancelPressed: () {
  70. if (widget.cancel != null) {
  71. widget.cancel!();
  72. }
  73. },
  74. )
  75. ],
  76. ),
  77. );
  78. }
  79. }
  80. class FlowyAlertDialog extends StatefulWidget {
  81. final String title;
  82. final void Function()? cancel;
  83. final void Function()? confirm;
  84. const FlowyAlertDialog({
  85. required this.title,
  86. this.confirm,
  87. this.cancel,
  88. Key? key,
  89. }) : super(key: key);
  90. @override
  91. State<FlowyAlertDialog> createState() => _CreateFlowyAlertDialog();
  92. }
  93. class _CreateFlowyAlertDialog extends State<FlowyAlertDialog> {
  94. @override
  95. void initState() {
  96. super.initState();
  97. }
  98. @override
  99. Widget build(BuildContext context) {
  100. final theme = context.watch<AppTheme>();
  101. return StyledDialog(
  102. child: Column(
  103. crossAxisAlignment: CrossAxisAlignment.start,
  104. mainAxisAlignment: MainAxisAlignment.center,
  105. children: <Widget>[
  106. ...[
  107. FlowyText.medium(widget.title, color: theme.shader4),
  108. ],
  109. if (widget.confirm != null) ...[
  110. const VSpace(20),
  111. OkCancelButton(
  112. onOkPressed: widget.confirm!,
  113. onCancelPressed: widget.cancel,
  114. )
  115. ]
  116. ],
  117. ),
  118. );
  119. }
  120. }
  121. class OkCancelDialog extends StatelessWidget {
  122. final VoidCallback? onOkPressed;
  123. final VoidCallback? onCancelPressed;
  124. final String? okTitle;
  125. final String? cancelTitle;
  126. final String? title;
  127. final String message;
  128. final double? maxWidth;
  129. const OkCancelDialog(
  130. {Key? key,
  131. this.onOkPressed,
  132. this.onCancelPressed,
  133. this.okTitle,
  134. this.cancelTitle,
  135. this.title,
  136. required this.message,
  137. this.maxWidth})
  138. : super(key: key);
  139. @override
  140. Widget build(BuildContext context) {
  141. final theme = context.watch<AppTheme>();
  142. return StyledDialog(
  143. maxWidth: maxWidth ?? 500,
  144. child: Column(
  145. crossAxisAlignment: CrossAxisAlignment.start,
  146. children: <Widget>[
  147. if (title != null) ...[
  148. FlowyText.medium(title!.toUpperCase(), color: theme.shader1),
  149. VSpace(Insets.sm * 1.5),
  150. Container(color: theme.bg1, height: 1),
  151. VSpace(Insets.m * 1.5),
  152. ],
  153. Text(message, style: TextStyles.Body1.textHeight(1.5)),
  154. SizedBox(height: Insets.l),
  155. OkCancelButton(
  156. onOkPressed: onOkPressed,
  157. onCancelPressed: onCancelPressed,
  158. okTitle: okTitle?.toUpperCase(),
  159. cancelTitle: cancelTitle?.toUpperCase(),
  160. )
  161. ],
  162. ),
  163. );
  164. }
  165. }
  166. class OkCancelButton extends StatelessWidget {
  167. final VoidCallback? onOkPressed;
  168. final VoidCallback? onCancelPressed;
  169. final String? okTitle;
  170. final String? cancelTitle;
  171. final double? minHeight;
  172. const OkCancelButton(
  173. {Key? key,
  174. this.onOkPressed,
  175. this.onCancelPressed,
  176. this.okTitle,
  177. this.cancelTitle,
  178. this.minHeight})
  179. : super(key: key);
  180. @override
  181. Widget build(BuildContext context) {
  182. return SizedBox(
  183. height: 48,
  184. child: Row(
  185. mainAxisAlignment: MainAxisAlignment.end,
  186. children: <Widget>[
  187. if (onCancelPressed != null)
  188. SecondaryTextButton(
  189. cancelTitle ?? LocaleKeys.button_Cancel.tr(),
  190. onPressed: () {
  191. onCancelPressed!();
  192. Navigator.of(context).pop();
  193. },
  194. bigMode: true,
  195. ),
  196. HSpace(Insets.m),
  197. if (onOkPressed != null)
  198. PrimaryTextButton(
  199. okTitle ?? LocaleKeys.button_OK.tr(),
  200. onPressed: () {
  201. onOkPressed!();
  202. Navigator.of(context).pop();
  203. },
  204. bigMode: true,
  205. ),
  206. ],
  207. ),
  208. );
  209. }
  210. }