toast.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //import 'package:oktoast/oktoast.dart';
  2. import 'dart:async';
  3. import 'dart:math';
  4. import 'package:flustars/flustars.dart' as FlutterStars;
  5. import 'package:flutter/material.dart';
  6. import 'package:fluttertoast/fluttertoast.dart';
  7. import 'package:liftmanager/utils/fast_notification.dart';
  8. import 'package:liftmanager/utils/loading_dialog.dart';
  9. // import 'package:percent_indicator_example/sample_linear_page.dart';
  10. import 'package:percent_indicator/percent_indicator.dart';
  11. // import 'package:ftoast/ftoast.dart' as f;
  12. // import 'package:fsuper/fsuper.dart';
  13. /// Toast工具类
  14. //class Toast {
  15. // static show(String msg, {duration = 2000}) {
  16. // if (msg == null){
  17. // return;
  18. // }
  19. // showToast(
  20. // msg,
  21. // duration: Duration(milliseconds: duration),
  22. // dismissOtherToast: true
  23. // );
  24. // }
  25. //
  26. // static cancelToast() {
  27. // dismissAllToast();
  28. // }
  29. //}
  30. toastsF(context, String msg, {int timeInSecForIos}) {
  31. FToast().init(context);
  32. FToast().showToast(
  33. child: ClipRRect(
  34. borderRadius: BorderRadius.circular(50),
  35. child: Container(
  36. color: Color(0xffbbbbbb),
  37. padding: EdgeInsets.only(left: 15, right: 15, top: 8, bottom: 10),
  38. child: Text(
  39. msg,
  40. style: TextStyle(
  41. fontSize: 13,
  42. color: Color(0xffffffff),
  43. ),
  44. ),
  45. ),
  46. ),
  47. gravity: ToastGravity.BOTTOM,
  48. toastDuration: Duration(seconds: timeInSecForIos ?? 3),
  49. );
  50. }
  51. toasts(String msg, {int timeInSecForIos}) {
  52. Fluttertoast.showToast(
  53. msg: msg,
  54. backgroundColor: Color(0xffbbbbbb),
  55. textColor: Color(0xffffffff),
  56. fontSize: 13,
  57. toastLength: Toast.LENGTH_SHORT,
  58. gravity: ToastGravity.BOTTOM,
  59. timeInSecForIosWeb: timeInSecForIos);
  60. }
  61. showSnackBar(BuildContext context, String msg) {
  62. Scaffold.of(context).showSnackBar(SnackBar(content: Text('$msg')));
  63. }
  64. showLoading(BuildContext context, [String msg = '加载中...']) {
  65. showDialog<Null>(
  66. context: context, //BuildContext对象
  67. barrierDismissible: false,
  68. builder: (BuildContext context) {
  69. return LoadingDialog(
  70. //调用对话框
  71. text: msg,
  72. );
  73. });
  74. }
  75. showAlert(
  76. BuildContext context,
  77. String title,
  78. String content,
  79. String txt1,
  80. Function onPre1, {
  81. String txt2 = "",
  82. Function onPre2,
  83. String txt3 = "",
  84. Function onPre3,
  85. }) {
  86. showDialog(
  87. barrierDismissible: false,
  88. context: context,
  89. builder: (context) => new AlertDialog(
  90. title: new Text('${title}'),
  91. content: new Text('${content}'),
  92. actions: <Widget>[
  93. txt3.length > 0
  94. ? new FlatButton(
  95. onPressed: onPre3,
  96. child: new Text("${txt3}"),
  97. )
  98. : Container(),
  99. txt2.length > 0
  100. ? new FlatButton(
  101. onPressed: onPre2,
  102. child: new Text("${txt2}"),
  103. )
  104. : Container(),
  105. new FlatButton(
  106. onPressed: onPre1,
  107. child: new Text('${txt1}'),
  108. ),
  109. ],
  110. ),
  111. );
  112. }
  113. dismissLoading(BuildContext context) {
  114. Navigator.pop(context);
  115. }
  116. randomInt(int min, int max) {
  117. return new Random().nextInt(max) % (max - min + 1) + min;
  118. }
  119. showPercent(BuildContext context, Function fun, Function funBack) {
  120. double percent = 0.0;
  121. double width = MediaQuery.of(context).size.width;
  122. showDialog<Null>(
  123. context: context, //BuildContext对象
  124. barrierDismissible: false,
  125. builder: (BuildContext context) {
  126. return StatefulBuilder(builder: (context, state) {
  127. String textss = '上传中,请稍后~';
  128. Color col = Colors.white;
  129. FastNotification.addListener("percent", (obj) {
  130. print(obj);
  131. print("-----------/*/");
  132. print(FlutterStars.SpUtil.getString('uploadName'));
  133. // if(FlutterStars.SpUtil.getString('uploadName') == ""){
  134. // FlutterStars.SpUtil.putString('uploadName', obj["uploadName"]);
  135. // }
  136. if (obj["success"] == true) {
  137. state(() {
  138. percent = 1.0;
  139. textss = "上传成功!";
  140. print("这里是成功");
  141. // col = Colors.green;
  142. });
  143. // FastNotification.push("percent_back",percent);
  144. // funBack();
  145. new Future.delayed(Duration(milliseconds: 1000), () {
  146. funBack();
  147. });
  148. } else {
  149. state(() {
  150. textss = "上传失败!";
  151. col = Colors.red;
  152. });
  153. // fun();
  154. new Future.delayed(Duration(milliseconds: 1000), () {
  155. fun();
  156. });
  157. }
  158. print(
  159. "complate-----------------------------------------------------");
  160. });
  161. Timer _timer;
  162. const period = const Duration(seconds: 3);
  163. _timer = Timer.periodic(period, (timer) {
  164. if (percent >= 0.9) {
  165. _timer.cancel();
  166. } else {
  167. state(() {
  168. percent = double.parse((0.1 + percent).toStringAsFixed(2));
  169. print(percent);
  170. });
  171. }
  172. });
  173. return Center(
  174. child: Column(
  175. mainAxisAlignment: MainAxisAlignment.center,
  176. children: <Widget>[
  177. Container(
  178. child: new LinearPercentIndicator(
  179. alignment: MainAxisAlignment.center,
  180. width: 300,
  181. animation: false,
  182. animationDuration: 1000,
  183. lineHeight: 20.0,
  184. percent: percent,
  185. center: Text((percent * 100).toString() + '%',
  186. style:
  187. new TextStyle(fontSize: 12.0, color: Colors.white)),
  188. linearStrokeCap: LinearStrokeCap.roundAll,
  189. backgroundColor: Colors.grey,
  190. progressColor: Colors.blue,
  191. )),
  192. // Container(
  193. // padding: EdgeInsets.only(top:15),
  194. // child: Text(
  195. // textss,
  196. // style: new TextStyle(fontSize: 12.0,color:Colors.white,decoration: TextDecoration.none),
  197. // ),
  198. // ),
  199. ],
  200. ),
  201. );
  202. });
  203. });
  204. }