//import 'package:oktoast/oktoast.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/utils/loading_dialog.dart';
// import 'package:percent_indicator_example/sample_linear_page.dart';
import 'package:percent_indicator/percent_indicator.dart';
import 'dart:async';
import 'package:liftmanager/utils/fast_notification.dart';
import 'dart:math';
import 'package:flustars/flustars.dart' as FlutterStars;
/// Toast工具类
//class Toast {
//  static show(String msg, {duration = 2000}) {
//    if (msg == null){
//      return;
//    }
//    showToast(
//        msg,
//        duration: Duration(milliseconds: duration),
//        dismissOtherToast: true
//    );
//  }
//
//  static cancelToast() {
//    dismissAllToast();
//  }
//}

toasts(String msg,{int timeInSecForIos}) {
  Fluttertoast.showToast(
      msg: msg,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
      timeInSecForIos: timeInSecForIos);
}


showSnackBar(BuildContext context, String msg){
  Scaffold.of(context)
      .showSnackBar(SnackBar(content: Text('$msg')));
}

showLoading(BuildContext context, [String msg = '加载中...']) {
  showDialog<Null>(
      context: context, //BuildContext对象
      barrierDismissible: false,
      builder: (BuildContext context) {
        return LoadingDialog(
          //调用对话框
          text: msg,
        );
      });
}

showAlert(BuildContext context, String title, String content, String txt1,
    Function onPre1,
    {String txt2 = "", Function onPre2,String txt3 = "", Function onPre3,}) {
  showDialog(
    barrierDismissible: false,
    context: context,
    builder: (context) => new AlertDialog(
      title: new Text('${title}'),
      content: new Text('${content}'),
      actions: <Widget>[
        txt3.length > 0
            ? new FlatButton(
          onPressed: onPre3,
          child: new Text("${txt3}"),
        )
            : Container(),
        txt2.length > 0
            ? new FlatButton(
          onPressed: onPre2,
          child: new Text("${txt2}"),
        )
            : Container(),
        new FlatButton(
          onPressed: onPre1,
          child: new Text('${txt1}'),
        ),
      ],
    ),
  );
}

dismissLoading(BuildContext context) {
  Navigator.pop(context);
}

randomInt(int min, int max) {
    return new Random().nextInt(max) % (max - min + 1) + min;
  }  

showPercent(BuildContext context,Function fun,Function funBack) {
  
  double percent = 0.0;
  double width = MediaQuery.of(context).size.width;
  showDialog<Null>(
      context: context, //BuildContext对象
      barrierDismissible: false,
      builder: (BuildContext context) {
        return StatefulBuilder(
            builder: (context, state) {
              String textss = '上传中，请稍后~';
              Color col = Colors.white;
              FastNotification.addListener("percent", (obj) {
                print(obj);
                print("-----------/*/");
                print(FlutterStars.SpUtil.getString('uploadName'));
                
                // if(FlutterStars.SpUtil.getString('uploadName') == ""){
                //   FlutterStars.SpUtil.putString('uploadName', obj["uploadName"]);
                  
                // }

                if(obj["success"] == true){
                  state(() {
                    percent = 1.0;
                    textss = "上传成功！";
                    print("这里是成功");
                    // col = Colors.green;
                  });
                  // FastNotification.push("percent_back",percent);
                  // funBack();
                  new Future.delayed(Duration(milliseconds: 1000), () {
                    funBack();
          
                  });
                }else {
                  state((){
                    textss = "上传失败！";
                    col = Colors.red;
                  });
                  // fun();
                  new Future.delayed(Duration(milliseconds: 1000), () {
                    fun();
          
                  });
                }
                
                print("complate-----------------------------------------------------");
              });

              Timer _timer;
              
              const period = const Duration(seconds: 3);
              _timer = Timer.periodic(period, (timer) {
                if(percent >= 0.9){
                  _timer.cancel();
                }else {
                  state(() {
                    percent = double.parse((0.1 + percent).toStringAsFixed(2));
                    print(percent);
                  });
              }});
             
                return Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      
                      Container(
                        child: 
                          new LinearPercentIndicator(
                            alignment:MainAxisAlignment.center,
                            width: 300,
                            animation: false,
                            animationDuration: 1000,
                            lineHeight: 20.0,
                            percent: percent,
                            center: Text(
                              (percent*100).toString()+'%',
                              style: new TextStyle(fontSize: 12.0,color:Colors.white)
                            ),
                            linearStrokeCap: LinearStrokeCap.roundAll,
                            backgroundColor: Colors.grey,
                            progressColor: Colors.blue,
                        )
                        ),
                      // Container(
                      //   padding: EdgeInsets.only(top:15),
                      //   child: Text(
                      //         textss,
                      //         style: new TextStyle(fontSize: 12.0,color:Colors.white,decoration: TextDecoration.none),
                      //       ),
                      // ),
                    ],
                  ),
                );
            });

        
      });
}
