You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
7.6KB

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:fluttertoast/fluttertoast.dart';
  4. import 'package:get/get.dart';
  5. import 'package:loading_indicator/loading_indicator.dart';
  6. import '../../themes/app_dimension.dart';
  7. import '../../themes/app_colors.dart';
  8. import '../../themes/styles_text.dart';
  9. import '../models/item_dropdown.dart';
  10. import 'dialog_widget.dart';
  11. class UtilWidget {
  12. static void showLoading() {
  13. if (Get.isDialogOpen!) Get.back();
  14. Get.dialog(
  15. Center(
  16. child: Container(
  17. width: 60.w,
  18. height: 60.h,
  19. decoration: BoxDecoration(
  20. color: Colors.transparent,
  21. borderRadius: BorderRadius.circular(8),
  22. ),
  23. child: LoadingIndicator(
  24. indicatorType: Indicator.ballClipRotate,
  25. colors: [
  26. AppColors.primary1,
  27. ],
  28. ),
  29. ),
  30. ),
  31. barrierDismissible: false,
  32. );
  33. }
  34. static void hideLoading() {
  35. if (Get.isDialogOpen!) Get.back();
  36. }
  37. static void hideDialog() {
  38. if (Get.isDialogOpen!) Get.back();
  39. }
  40. static void showAlertConfirm({
  41. required BuildContext context,
  42. required String icon,
  43. required String title,
  44. required String message,
  45. required String confirmTitle,
  46. required Function onPressedConfirm,
  47. Function? onPressedCancel,
  48. String? cancelTitle,
  49. Function? onPressedOther,
  50. String? otherTitle,
  51. Color? colorConfirmButton,
  52. }) {
  53. showDialog(
  54. context: context,
  55. barrierDismissible: true,
  56. builder: (BuildContext context) {
  57. return Dialog(
  58. insetPadding: EdgeInsets.all(10.r),
  59. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)),
  60. child: DialogWidget(
  61. icon: icon,
  62. title: title,
  63. message: message,
  64. confirmTitle: confirmTitle,
  65. onPressedConfirm: onPressedConfirm,
  66. cancelTitle: cancelTitle,
  67. onPressedCancel: onPressedCancel,
  68. otherTitle: otherTitle,
  69. onPressedOther: onPressedOther,
  70. colorConfirmButton: colorConfirmButton,
  71. ),
  72. );
  73. },
  74. );
  75. }
  76. static void showPicker({
  77. required BuildContext context,
  78. required Map<String, String> values,
  79. required Function(int) onCallback,
  80. required int initIndex,
  81. }) {
  82. showModalBottomSheet(
  83. context: context,
  84. builder: (BuildContext context) {
  85. return Container(
  86. width: Get.width.w,
  87. height: 200.h,
  88. child: CupertinoPicker(
  89. scrollController: FixedExtentScrollController(
  90. initialItem: initIndex,
  91. ),
  92. backgroundColor: Colors.white,
  93. onSelectedItemChanged: (value) {
  94. onCallback(value);
  95. },
  96. itemExtent: 28.0.h,
  97. children: values.entries.map(
  98. (e) {
  99. return Text(e.value);
  100. },
  101. ).toList(),
  102. ),
  103. );
  104. },
  105. );
  106. }
  107. static void showBottomSheet({
  108. required BuildContext context,
  109. required List<ItemDropDown> dataSources,
  110. required Function(int) onSelected,
  111. required FixedExtentScrollController controller,
  112. }) {
  113. showModalBottomSheet(
  114. context: context,
  115. builder: (context) {
  116. return Container(
  117. padding: EdgeInsets.symmetric(
  118. vertical: 10.h,
  119. horizontal: 20.w,
  120. ),
  121. child: Column(
  122. mainAxisSize: MainAxisSize.min,
  123. children: <Widget>[
  124. Row(
  125. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  126. children: [
  127. InkWell(
  128. onTap: () {
  129. Get.back();
  130. },
  131. child: Text(
  132. 'Huỷ',
  133. style: StylesText.body2.copyWith(
  134. color: AppColors.neutral2,
  135. ),
  136. ),
  137. ),
  138. InkWell(
  139. onTap: () {
  140. Get.back();
  141. onSelected(controller.selectedItem);
  142. },
  143. child: Text(
  144. 'Chọn',
  145. style: StylesText.body2,
  146. ),
  147. )
  148. ],
  149. ),
  150. Row(
  151. mainAxisSize: MainAxisSize.min,
  152. children: <Widget>[
  153. Expanded(
  154. child: Container(
  155. height: 302.h,
  156. child: CupertinoPicker(
  157. magnification: 1.22,
  158. squeeze: 1.2,
  159. useMagnifier: true,
  160. itemExtent: 32.h,
  161. scrollController: controller,
  162. onSelectedItemChanged: (int selectedItem) {},
  163. children: List<Widget>.generate(dataSources.length, (index) {
  164. return Center(
  165. child: Text(
  166. dataSources[index].value ?? '',
  167. style: StylesText.body6,
  168. ),
  169. );
  170. }),
  171. ),
  172. ),
  173. ),
  174. ],
  175. ),
  176. ],
  177. ),
  178. );
  179. });
  180. }
  181. static void showNormalToast(String message) {
  182. Fluttertoast.cancel();
  183. Fluttertoast.showToast(msg: message);
  184. }
  185. static void showToastError(String message) {
  186. Fluttertoast.cancel();
  187. Fluttertoast.showToast(
  188. msg: message,
  189. backgroundColor: Colors.red,
  190. textColor: Colors.white,
  191. );
  192. }
  193. static void showToastWarning(String message) {
  194. Fluttertoast.cancel();
  195. Fluttertoast.showToast(
  196. msg: message,
  197. backgroundColor: Colors.orange[400],
  198. textColor: Colors.white,
  199. );
  200. }
  201. static void showToastSuccess(String message) {
  202. Fluttertoast.cancel();
  203. Fluttertoast.showToast(
  204. msg: message,
  205. backgroundColor: AppColors.primary1,
  206. textColor: Colors.white,
  207. );
  208. }
  209. static void showCustomDialog({
  210. required BuildContext context,
  211. required Widget content,
  212. bool? isDismissible,
  213. Function()? onCloseDialog,
  214. }) {
  215. showDialog(
  216. context: context,
  217. barrierDismissible: isDismissible ?? true,
  218. builder: (BuildContext context) {
  219. return Dialog(
  220. insetPadding: const EdgeInsets.all(2),
  221. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
  222. child: Stack(
  223. children: [
  224. Container(
  225. margin: const EdgeInsets.only(
  226. top: 4,
  227. left: 4,
  228. right: 4,
  229. bottom: 4,
  230. ),
  231. child: content,
  232. ),
  233. Positioned(
  234. top: -10,
  235. right: -10,
  236. child: Material(
  237. color: Colors.transparent,
  238. child: IconButton(
  239. icon: Icon(
  240. Icons.highlight_off,
  241. size: 30,
  242. color: AppColors.black,
  243. ),
  244. onPressed: onCloseDialog ??
  245. () {
  246. Get.back();
  247. },
  248. ),
  249. ),
  250. ),
  251. ],
  252. ),
  253. );
  254. },
  255. );
  256. }
  257. }