custom_label_dialog.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  6. import 'package:get/get.dart';
  7. import 'package:keyboard/resource/string.gen.dart';
  8. import 'package:keyboard/utils/toast_util.dart';
  9. import '../resource/assets.gen.dart';
  10. import '../resource/colors.gen.dart';
  11. import '../utils/styles.dart';
  12. class CustomLabelDialog {
  13. static const String tag = 'CustomLabelDialog';
  14. // 文本是否为空
  15. static void show({
  16. String? title,
  17. required Function(String) clickCallback,
  18. required String hintText,
  19. required int maxLength,
  20. }) {
  21. TextEditingController textController = TextEditingController();
  22. RxBool isTextEmpty = true.obs;
  23. textController.addListener(() {
  24. isTextEmpty.value = textController.text.trim().isEmpty;
  25. });
  26. SmartDialog.show(
  27. tag: tag,
  28. alignment: Alignment.center,
  29. backType: SmartBackType.block,
  30. clickMaskDismiss: true,
  31. maskColor: ColorName.black70,
  32. builder: (_) {
  33. return Container(
  34. width: double.infinity,
  35. margin: EdgeInsets.symmetric(horizontal: 31.w),
  36. decoration: ShapeDecoration(
  37. color: Colors.white,
  38. shape: RoundedRectangleBorder(
  39. borderRadius: BorderRadius.circular(20.r),
  40. ),
  41. ),
  42. child: Stack(
  43. children: [
  44. Container(
  45. padding: EdgeInsets.symmetric(
  46. horizontal: 16.w,
  47. vertical: 24.h,
  48. ),
  49. child: Column(
  50. mainAxisSize: MainAxisSize.min,
  51. crossAxisAlignment: CrossAxisAlignment.center,
  52. mainAxisAlignment: MainAxisAlignment.center,
  53. children: [
  54. Text(
  55. title ?? StringName.customLabel,
  56. style: TextStyle(
  57. color: Colors.black.withAlpha(204),
  58. fontSize: 16.sp,
  59. fontWeight: FontWeight.w500,
  60. ),
  61. ),
  62. SizedBox(height: 16.h),
  63. Container(
  64. height: 48.h,
  65. alignment: Alignment.center,
  66. decoration: ShapeDecoration(
  67. color: const Color(0xFFF5F4F9),
  68. shape: RoundedRectangleBorder(
  69. borderRadius: BorderRadius.circular(31.r),
  70. ),
  71. ),
  72. child: TextField(
  73. controller: textController,
  74. maxLength: maxLength,
  75. maxLines: 1,
  76. scrollPhysics: const BouncingScrollPhysics(),
  77. textAlign: TextAlign.center,
  78. textAlignVertical: TextAlignVertical.center,
  79. decoration: InputDecoration(
  80. isDense: true,
  81. counterText: "",
  82. hintText: hintText,
  83. hintStyle: TextStyle(
  84. color: Colors.black.withAlpha(66),
  85. ),
  86. border: OutlineInputBorder(
  87. borderRadius: BorderRadius.circular(10.r),
  88. borderSide: BorderSide.none, // 移除边框线
  89. ),
  90. filled: true,
  91. fillColor: const Color(0xFFF5F4F9),
  92. ),
  93. ),
  94. ),
  95. SizedBox(height: 24.h),
  96. Obx(() {
  97. return Container(
  98. height: 48.h,
  99. width: double.infinity,
  100. decoration:
  101. isTextEmpty.value
  102. ? Styles.getInactiveButtonDecoration(
  103. 31.r,
  104. )
  105. : Styles.getActivateButtonDecoration(
  106. 31.r,
  107. ),
  108. child: TextButton(
  109. onPressed: () {
  110. if (textController.text.isEmpty) {
  111. return ToastUtil.show(hintText);
  112. }
  113. if (textController.text.length > maxLength) {
  114. return ToastUtil.show(
  115. '最多$maxLength个字哦~');
  116. }
  117. clickCallback(textController.text.trim());
  118. SmartDialog.dismiss();
  119. },
  120. child: Text(
  121. StringName.customLabelSave,
  122. style: TextStyle(
  123. color: Colors.white,
  124. fontSize: 16.sp,
  125. fontWeight: FontWeight.w500,
  126. ),
  127. ),
  128. ),
  129. );
  130. }),
  131. ],
  132. ),
  133. ),
  134. Positioned(
  135. right: 14,
  136. top: 14,
  137. child: GestureDetector(
  138. onTap: () {
  139. SmartDialog.dismiss();
  140. },
  141. child: Assets.images.iconCustomDialogClose.image(
  142. width: 24.w,
  143. height: 24.h,
  144. ),
  145. ),
  146. ),
  147. ],
  148. ),
  149. );
  150. },
  151. );
  152. }
  153. }