custom_label_dialog.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. backType: SmartBackType.block,
  29. clickMaskDismiss: true,
  30. maskColor: ColorName.black70,
  31. builder: (_) {
  32. return Scaffold(
  33. backgroundColor: Colors.transparent,
  34. body: Column(
  35. crossAxisAlignment: CrossAxisAlignment.center,
  36. mainAxisAlignment: MainAxisAlignment.center,
  37. children: [
  38. Container(
  39. width: double.infinity,
  40. margin: EdgeInsets.symmetric(horizontal: 31.w),
  41. decoration: ShapeDecoration(
  42. color: Colors.white,
  43. shape: RoundedRectangleBorder(
  44. borderRadius: BorderRadius.circular(20.r),
  45. ),
  46. ),
  47. child: Stack(
  48. children: [
  49. Container(
  50. padding: EdgeInsets.symmetric(
  51. horizontal: 16.w,
  52. vertical: 24.h,
  53. ),
  54. child: Column(
  55. mainAxisSize: MainAxisSize.min,
  56. crossAxisAlignment: CrossAxisAlignment.center,
  57. mainAxisAlignment: MainAxisAlignment.center,
  58. children: [
  59. Text(
  60. title ?? StringName.customLabel,
  61. style: TextStyle(
  62. color: Colors.black.withAlpha(204),
  63. fontSize: 16.sp,
  64. fontWeight: FontWeight.w500,
  65. ),
  66. ),
  67. SizedBox(height: 16.h),
  68. Container(
  69. height: 48.h,
  70. alignment: Alignment.center,
  71. decoration: ShapeDecoration(
  72. color: const Color(0xFFF5F4F9),
  73. shape: RoundedRectangleBorder(
  74. borderRadius: BorderRadius.circular(31.r),
  75. ),
  76. ),
  77. child: TextField(
  78. controller: textController,
  79. maxLength: maxLength,
  80. maxLines: 1,
  81. scrollPhysics: const BouncingScrollPhysics(),
  82. textAlign: TextAlign.center,
  83. textAlignVertical: TextAlignVertical.center,
  84. decoration: InputDecoration(
  85. isDense: true,
  86. counterText: "",
  87. hintText: hintText,
  88. hintStyle: TextStyle(
  89. color: Colors.black.withAlpha(66),
  90. ),
  91. border: OutlineInputBorder(
  92. borderRadius: BorderRadius.circular(10.r),
  93. borderSide: BorderSide.none, // 移除边框线
  94. ),
  95. filled: true,
  96. fillColor: const Color(0xFFF5F4F9),
  97. ),
  98. ),
  99. ),
  100. SizedBox(height: 24.h),
  101. Obx(() {
  102. return Container(
  103. height: 48.h,
  104. width: double.infinity,
  105. decoration:
  106. isTextEmpty.value
  107. ? Styles.getInactiveButtonDecoration(
  108. 31.r,
  109. )
  110. : Styles.getActivateButtonDecoration(
  111. 31.r,
  112. ),
  113. child: TextButton(
  114. onPressed: () {
  115. if (textController.text.isEmpty) {
  116. return ToastUtil.show(hintText);
  117. }
  118. if (textController.text.length > maxLength) {
  119. return ToastUtil.show(
  120. '最多$maxLength个字哦~');
  121. }
  122. clickCallback(textController.text.trim());
  123. SmartDialog.dismiss();
  124. },
  125. child: Text(
  126. StringName.customLabelSave,
  127. style: TextStyle(
  128. color: Colors.white,
  129. fontSize: 16.sp,
  130. fontWeight: FontWeight.w500,
  131. ),
  132. ),
  133. ),
  134. );
  135. }),
  136. ],
  137. ),
  138. ),
  139. Positioned(
  140. right: 14,
  141. top: 14,
  142. child: GestureDetector(
  143. onTap: () {
  144. SmartDialog.dismiss();
  145. },
  146. child: Assets.images.iconCustomDialogClose.image(
  147. width: 24.w,
  148. height: 24.h,
  149. ),
  150. ),
  151. ),
  152. ],
  153. ),
  154. ),
  155. ],
  156. ),
  157. );
  158. },
  159. );
  160. }
  161. }