dialog.dart 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. class DialogController extends GetxController {
  4. var isDialogVisible = false.obs;
  5. void showErrorDialog() {
  6. isDialogVisible.value = true;
  7. }
  8. Future<String?> showInputDialog({
  9. required String title,
  10. required String content,
  11. String cancelText = "取消",
  12. String enterText = "确定",
  13. bool isPassword = false,
  14. }) {
  15. final controller = TextEditingController();
  16. return Get.dialog<String>(
  17. AlertDialog(
  18. title: Text(title),
  19. content: Column(
  20. mainAxisSize: MainAxisSize.min,
  21. crossAxisAlignment: CrossAxisAlignment.start,
  22. children: [
  23. Text(content),
  24. const SizedBox(height: 10),
  25. TextField(
  26. controller: controller,
  27. obscureText: isPassword,
  28. decoration: InputDecoration(
  29. border: const OutlineInputBorder(),
  30. hintText: isPassword ? "请输入密码" : "请输入",
  31. ),
  32. ),
  33. ],
  34. ),
  35. actions: [
  36. TextButton(
  37. onPressed: () => Get.back(),
  38. child: Text(cancelText),
  39. ),
  40. TextButton(
  41. onPressed: () => Get.back(result: controller.text),
  42. child: Text(enterText),
  43. ),
  44. ],
  45. ),
  46. );
  47. }
  48. Future<bool?> showNormalDialog({
  49. required String title,
  50. String? content,
  51. Widget? child,
  52. required String cancelText,
  53. required String enterText,
  54. bool Function()? validator,
  55. }) {
  56. assert(content != null || child != null);
  57. return Get.dialog(
  58. AlertDialog(
  59. title: Text(title),
  60. content: child ?? Text(content!),
  61. actions: [
  62. TextButton(
  63. onPressed: () => Get.back(result: false),
  64. child: Text(cancelText),
  65. ),
  66. TextButton(
  67. onPressed: () => Get.back(result: true),
  68. child: Text(enterText),
  69. ),
  70. ],
  71. ),
  72. );
  73. }
  74. void showToast(String message) {
  75. Get.showSnackbar(
  76. GetSnackBar(
  77. message: message,
  78. duration: const Duration(seconds: 2),
  79. ),
  80. );
  81. }
  82. }