import 'package:flutter/material.dart';
import 'LinearButton.dart';

//  void showSuccess(text) {

//   }


class MyDialog {
  static Future showDialog(context, text,
      {String title, bool isCancel = false, String submitText = '确认', List<Map> textList = const [], OnTapHomeMenu onsubmit, OnTapHomeMenu oncancel}) {
   
    return Navigator.of(context).push(
      PageRouteBuilder(
        opaque: false,
        transitionDuration: Duration(milliseconds: 300),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
          return FadeTransition(
            opacity: CurvedAnimation(parent: animation, curve: Curves.linear),
            child: child,
          );
        },
        pageBuilder: (BuildContext context, _, __) {
          return LoadingDialog(title: title, text: text, textList: textList, submitText: submitText, cancel: isCancel, onsubmit: onsubmit, oncancel: oncancel);
        },
      ),
    );
  }
}

typedef OnTapHomeMenu = void Function();

// ignore: must_be_immutable
class LoadingDialog extends Dialog {
  String title;
  String text;
  String submitText;
  bool cancel = false;
  OnTapHomeMenu onsubmit = () {};
  OnTapHomeMenu oncancel = () {};
  List<Map> textList = [];

  LoadingDialog({Key key, @required this.text, this.cancel, this.textList, this.onsubmit, this.oncancel, this.title, this.submitText}) : super(key: key);

  Widget _text(textList) {
    if (textList.length > 0) {
      List<TextSpan> _list = [];

      for (int i = 0; i < textList.length; i++) {
        if (textList[i]["isImportant"]) {
          _list.add(TextSpan(
              text: textList[i]['value'],
              style: TextStyle(
                color: Color(0xFFFFB726),
              )));
        } else {
          _list.add(TextSpan(
              text: textList[i]['value'],
              style: TextStyle(
                color: Colors.white,
              )));
        }
      }

      return Text.rich(TextSpan(children: _list),textAlign: TextAlign.center,);
    } else {
      return Container();
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        backgroundColor: Color(0xCC000000),
        body: Container(
          child: SizedBox.expand(
            child: UnconstrainedBox(
              child: Container(
                width: 270,
                // padding: EdgeInsets.symmetric(horizontal: 20, vertical: 25),
                decoration: BoxDecoration(color: Color(0xFF15151D), border: Border.all(width: 1, color: Theme.of(context).primaryColor)),
                child: Stack(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 20, vertical: 25),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(title ?? '提示', style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w600)),
                          Container(
                            height: 20,
                          ),
                          Text(text, style: TextStyle(color: Colors.white, fontSize: 14), textAlign: TextAlign.center),
                          _text(textList),
                          Container(
                            height: 30,
                          ),
                          cancel
                              ? Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Container(
                                      width: 100,
                                      child: LinearButton(
                                        btntext: '取消',
                                        btnHeight: 36.0,
                                        colorList: [Colors.white, Colors.white],
                                        textColor: Color(0xFF252532),
                                        onTapHomeMenu: () {
                                          Navigator.of(context).pop();
                                          oncancel();
                                        },
                                      ),
                                    ),
                                    Container(
                                        width: 100,
                                        child: LinearButton(
                                          btntext: submitText,
                                          btnHeight: 36.0,
                                          onTapHomeMenu: () {
                                            Navigator.of(context).pop();
                                            onsubmit();
                                          },
                                        )
                                        //  RaisedButton(
                                        //   textColor: Colors.white,
                                        //   child: Text(submitText),
                                        //   onPressed: () {
                                        //     Navigator.of(context).pop();
                                        //     onsubmit();
                                        //   },
                                        // ),
                                        )
                                  ],
                                )
                              : Container(
                                  width: double.infinity,
                                  child: LinearButton(
                                    btntext: submitText,
                                    btnHeight: 36.0,
                                    onTapHomeMenu: () {
                                      Navigator.of(context).pop();
                                      onsubmit();
                                    },
                                  )
                                  //  RaisedButton(
                                  //   textColor: Colors.white,
                                  //   child: Text(submitText),
                                  //   onPressed: () {
                                  //     Navigator.of(context).pop();
                                  //     onsubmit();
                                  //   },
                                  // ),
                                  ),
                          Container(
                            height: 5,
                          )
                        ],
                      ),
                    ),
                    Positioned(
                      top: 0,
                      left: 0,
                      child: Image.asset(
                        'images/tancuang_shang.png',
                        // width: 131,
                      ),
                    ),
                    Positioned(
                      bottom: 0,
                      right: 4,
                      child: Image.asset(
                        'images/tancuang_xia.png',
                        // width: 148,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
      onWillPop: () {
        return Future.value(false);
      },
    );
  }
}
