Common.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_styled_toast/flutter_styled_toast.dart';
  5. /// 默认的图片尺寸
  6. double coverWidth = 210;
  7. double coverHeight = 315;
  8. String categoryTitle(String? categoryTitle) {
  9. return categoryTitle ?? "全分类";
  10. }
  11. /// 显示一个toast
  12. void defaultToast(BuildContext context, String title) {
  13. showToast(
  14. title,
  15. context: context,
  16. position: StyledToastPosition.center,
  17. animation: StyledToastAnimation.scale,
  18. reverseAnimation: StyledToastAnimation.fade,
  19. duration: const Duration(seconds: 4),
  20. animDuration: const Duration(seconds: 1),
  21. curve: Curves.elasticOut,
  22. reverseCurve: Curves.linear,
  23. );
  24. }
  25. /// 显示一个确认框, 用户关闭弹窗以及选择否都会返回false, 仅当用户选择确定时返回true
  26. Future<bool> confirmDialog(
  27. BuildContext context, String title, String content) async {
  28. return await showDialog(
  29. context: context,
  30. builder: (context) => AlertDialog(
  31. title: Text(title),
  32. content: SingleChildScrollView(
  33. child: ListBody(
  34. children: <Widget>[Text(content)],
  35. ),
  36. ),
  37. actions: <Widget>[
  38. MaterialButton(
  39. child: const Text('取消'),
  40. onPressed: () {
  41. Navigator.of(context).pop(false);
  42. },
  43. ),
  44. MaterialButton(
  45. child: const Text('确定'),
  46. onPressed: () {
  47. Navigator.of(context).pop(true);
  48. },
  49. ),
  50. ],
  51. )) ??
  52. false;
  53. }
  54. /// 显示一个消息提示框
  55. Future alertDialog(BuildContext context, String title, String content) {
  56. return showDialog(
  57. context: context,
  58. builder: (context) => AlertDialog(
  59. title: Text(title),
  60. content: SingleChildScrollView(
  61. child: ListBody(
  62. children: <Widget>[
  63. Text(content),
  64. ],
  65. ),
  66. ),
  67. actions: <Widget>[
  68. MaterialButton(
  69. child: const Text('确定'),
  70. onPressed: () {
  71. Navigator.of(context).pop();
  72. },
  73. ),
  74. ],
  75. ),
  76. );
  77. }
  78. /// stream-filter的替代方法
  79. List<T> filteredList<T>(List<T> list, bool Function(T) filter) {
  80. List<T> result = [];
  81. for (var element in list) {
  82. if (filter(element)) {
  83. result.add(element);
  84. }
  85. }
  86. return result;
  87. }
  88. /// 创建一个单选对话框, 用户取消选择返回null, 否则返回所选内容
  89. Future<T?> chooseListDialog<T>(
  90. BuildContext context, String title, List<T> items,
  91. {String? tips}) async {
  92. return showDialog<T>(
  93. context: context,
  94. builder: (BuildContext context) {
  95. return SimpleDialog(
  96. title: Text(title),
  97. children: [
  98. ...items.map((e) => SimpleDialogOption(
  99. onPressed: () {
  100. Navigator.of(context).pop(e);
  101. },
  102. child: Text('$e'),
  103. )),
  104. ...tips != null
  105. ? [
  106. Container(
  107. padding: const EdgeInsets.fromLTRB(15, 5, 15, 15),
  108. child: Text(tips),
  109. ),
  110. ]
  111. : [],
  112. ],
  113. );
  114. },
  115. );
  116. }
  117. /// 创建一个单选对话框, 用户取消选择返回null, 否则返回所选内容(value)
  118. Future<T?> chooseMapDialog<T>(
  119. BuildContext buildContext, Map<String, T> values, String title) async {
  120. return await showDialog<T>(
  121. context: buildContext,
  122. builder: (BuildContext context) {
  123. return SimpleDialog(
  124. title: Text(title),
  125. children: values.entries
  126. .map((e) => SimpleDialogOption(
  127. child: Text(e.key),
  128. onPressed: () {
  129. Navigator.of(context).pop(e.value);
  130. },
  131. ))
  132. .toList(),
  133. );
  134. },
  135. );
  136. }
  137. /// 输入对话框1
  138. var _controller =
  139. TextEditingController.fromValue(const TextEditingValue(text: ''));
  140. Future<String?> displayTextInputDialog(BuildContext context,
  141. {String? title,
  142. String src = "",
  143. String? hint,
  144. String? desc,
  145. bool isPasswd = false}) {
  146. _controller.text = src;
  147. return showDialog(
  148. context: context,
  149. builder: (context) {
  150. return AlertDialog(
  151. title: title == null ? null : Text(title),
  152. content: SingleChildScrollView(
  153. child: ListBody(
  154. children: [
  155. TextField(
  156. controller: _controller,
  157. decoration: InputDecoration(hintText: hint),
  158. obscureText: isPasswd,
  159. obscuringCharacter: '\u2022',
  160. ),
  161. ...(desc == null
  162. ? []
  163. : [
  164. Container(
  165. padding: const EdgeInsets.only(top: 20, bottom: 10),
  166. child: Text(
  167. desc,
  168. style: TextStyle(
  169. fontSize: 12,
  170. color: Theme.of(context)
  171. .textTheme
  172. .bodyText1
  173. ?.color
  174. ?.withOpacity(.5)),
  175. ),
  176. )
  177. ]),
  178. ],
  179. ),
  180. ),
  181. actions: <Widget>[
  182. MaterialButton(
  183. child: const Text('取消'),
  184. onPressed: () {
  185. Navigator.of(context).pop();
  186. },
  187. ),
  188. MaterialButton(
  189. child: const Text('确认'),
  190. onPressed: () {
  191. Navigator.of(context).pop(_controller.text);
  192. },
  193. ),
  194. ],
  195. );
  196. },
  197. );
  198. }
  199. /// 将字符串前面加0直至满足len位
  200. String add0(int num, int len) {
  201. var rsp = "$num";
  202. while (rsp.length < len) {
  203. rsp = "0$rsp";
  204. }
  205. return rsp;
  206. }
  207. /// 输入对话框2
  208. final TextEditingController _textEditController =
  209. TextEditingController(text: '');
  210. Future<String?> inputString(BuildContext context, String title,
  211. {String hint = "", String? defaultValue}) async {
  212. if (defaultValue != null) {
  213. _textEditController.text = defaultValue;
  214. } else {
  215. _textEditController.clear();
  216. }
  217. return showDialog(
  218. context: context,
  219. builder: (context) {
  220. return AlertDialog(
  221. content: Card(
  222. child: SingleChildScrollView(
  223. child: ListBody(
  224. children: [
  225. Text(title),
  226. TextField(
  227. controller: _textEditController,
  228. decoration: InputDecoration(
  229. labelText: hint,
  230. ),
  231. ),
  232. ],
  233. ),
  234. ),
  235. ),
  236. actions: <Widget>[
  237. MaterialButton(
  238. onPressed: () {
  239. Navigator.pop(context);
  240. },
  241. child: const Text('取消'),
  242. ),
  243. MaterialButton(
  244. onPressed: () {
  245. Navigator.pop(context, _textEditController.text);
  246. },
  247. child: const Text('确定'),
  248. ),
  249. ],
  250. );
  251. },
  252. );
  253. }