modal.dart 5.2 KB

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