toast.dart 7.2 KB

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