modal.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:fluttertoast/fluttertoast.dart';
  5. import 'package:smartledz_wifi_test/widgets/animations/animation_for_rotation.dart';
  6. class Modal {
  7. // 构建模态框标题
  8. static Widget buildTitle({
  9. required String title,
  10. double? size,
  11. }){
  12. return Padding(
  13. padding: EdgeInsets.only(bottom: 5.sp),
  14. child: Center(
  15. child: Text(
  16. title,
  17. style: TextStyle(
  18. fontWeight: FontWeight.bold,
  19. fontSize: size ?? 15.sp,
  20. color: Colors.black,
  21. )
  22. ),
  23. ),
  24. );
  25. }
  26. // 构建文字内容
  27. static Widget buildContent({
  28. required String content,
  29. TextAlign? textAlign,
  30. }){
  31. return Text(
  32. content,
  33. textAlign: textAlign,
  34. style: TextStyle(
  35. fontSize: 15.sp,
  36. color: const Color(0xAA000000)
  37. )
  38. );
  39. }
  40. // 构建带边框的输入框
  41. static Widget buildInputForBorder({
  42. String? initValue,
  43. String? hintText,
  44. List<TextInputFormatter>? inputFormatters,
  45. TextEditingController? controller,
  46. void Function(String)? onChanged,
  47. }){
  48. return Container(
  49. height: 40.sp,
  50. margin: EdgeInsets.symmetric(vertical: 15.sp),
  51. decoration: BoxDecoration(
  52. border: Border.all(
  53. color: const Color(0xAA000000),
  54. width: .5
  55. )
  56. ),
  57. child: TextFormField(
  58. controller: controller,
  59. initialValue: initValue,
  60. onChanged: onChanged,
  61. cursorColor: const Color(0xFF00AAFF),
  62. decoration: InputDecoration(
  63. hintText: hintText,
  64. hintStyle: TextStyle(
  65. fontSize: 15.sp,
  66. color: Colors.grey.shade500
  67. ),
  68. border: const OutlineInputBorder(borderSide: BorderSide.none),
  69. contentPadding: EdgeInsets.symmetric(horizontal: 10.sp)
  70. ),
  71. inputFormatters: inputFormatters,
  72. style: TextStyle(fontSize: 15.sp),
  73. ),
  74. );
  75. }
  76. // 构建两个一组的按钮
  77. static Widget buildButtonForTwo({
  78. required String leftBtnTitle,
  79. required String rightBtnTitle,
  80. void Function()? onLeftBtnTap,
  81. void Function()? onRightBtnTap,
  82. }){
  83. return Row(
  84. children: [
  85. GestureDetector(
  86. onTap: onLeftBtnTap,
  87. child: Container(
  88. color: const Color(0xFFAAAAAA),
  89. padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 10.w),
  90. child: Text(leftBtnTitle, style: TextStyle(color: Colors.white, fontSize: 15.sp)),
  91. ),
  92. ),
  93. Expanded(
  94. child: Align(
  95. alignment: Alignment.centerRight,
  96. child: GestureDetector(
  97. onTap: onRightBtnTap,
  98. child: Container(
  99. color: const Color(0xFF00AAFF),
  100. padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 10.w),
  101. child: Text(rightBtnTitle, style: TextStyle(color: Colors.white, fontSize: 15.sp)
  102. ),
  103. ),
  104. ),
  105. ),
  106. )
  107. ],
  108. );
  109. }
  110. // 构建自撑宽度的按钮
  111. static Widget buildButtonForFull({
  112. required String title,
  113. void Function()? onTap,
  114. }){
  115. return GestureDetector(
  116. onTap: onTap,
  117. child: Container(
  118. alignment: Alignment.center,
  119. color: const Color(0xFF00AAFF),
  120. padding: EdgeInsets.symmetric(vertical: 8.h),
  121. child: Text(title, style: TextStyle(color: Colors.white, fontSize: 15.sp)
  122. ),
  123. ),
  124. );
  125. }
  126. // 打开并显示模态框
  127. static void open({
  128. required BuildContext context,
  129. required Widget Function(BuildContext) builder,
  130. double? width,
  131. EdgeInsetsGeometry? padding,
  132. bool disableShadeClose = false, // 禁用遮罩点击关闭
  133. bool disableReturnKey = false, // 禁用返回键
  134. }){
  135. showDialog(
  136. barrierDismissible: !disableShadeClose,
  137. context: context,
  138. builder: (BuildContext context){
  139. Widget child = UnconstrainedBox(
  140. child: Container(
  141. width: width ?? 300.w,
  142. color: Colors.white,
  143. padding: padding ?? EdgeInsets.all(15.w),
  144. child: builder(context),
  145. ),
  146. );
  147. if(disableReturnKey == true){
  148. return WillPopScope(child: child, onWillPop: () async => false);
  149. }
  150. return child;
  151. }
  152. );
  153. }
  154. // 打开加载遮罩层
  155. static void openLoading({
  156. required BuildContext context,
  157. required String tip,
  158. }){
  159. showDialog(
  160. barrierDismissible: false,
  161. context: context,
  162. builder: (BuildContext context){
  163. return WillPopScope(
  164. onWillPop: () async => false,
  165. child: UnconstrainedBox(
  166. child: Row(
  167. children: [
  168. AnimationForRotation(
  169. duration: const Duration(milliseconds: 400),
  170. child: Icon(Icons.refresh, color: const Color(0xFF00AAFF), size: 45.sp),
  171. ),
  172. SizedBox(width: 10.sp),
  173. Text(tip, style: TextStyle(color: Colors.white, fontSize: 23.sp, fontWeight: FontWeight.bold))
  174. ],
  175. ),
  176. ),
  177. );
  178. }
  179. );
  180. }
  181. // Toast消息提示
  182. static void toast({
  183. required String msg
  184. }){
  185. Fluttertoast.showToast(
  186. msg: msg,
  187. gravity: ToastGravity.TOP,
  188. backgroundColor: const Color(0xFFEEEEEE),
  189. textColor: const Color(0xFF00AAFF)
  190. );
  191. }
  192. }