toast.dart 6.1 KB

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