import 'dart:async';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/bean/user_friend.dart';
import 'package:sport/bean/user_info.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/button_cancel.dart';
import 'package:sport/widgets/button_primary.dart';
import 'package:sport/widgets/misc.dart';

import '../space.dart';

class CustomAlertDialog extends StatelessWidget {
  final String title;
  final Widget child;
  final String textCancel;
  final String textOk;
  final Function ok;
  final bool addClose;
  final bool isLine;

  CustomAlertDialog({
    this.title,
    this.child,
    this.textCancel = "取消",
    this.textOk = "确定",
    this.ok,
    this.addClose = false,
    this.isLine = false,
  });

  @override
  Widget build(BuildContext context) {
    var _width =
        MediaQuery.of(context).size.width * (child != null ? 0.86 : 0.66);
    return Material(
      type: MaterialType.transparency,
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: Center(
          child: Container(
            width: _width,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(10.0)),
              color: Colors.white,
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                if (addClose)
                  Align(
                      alignment: Alignment.topRight,
                      child: IconButton(
                        icon: Image.asset("lib/assets/img/btn_close_big.png"),
                        onPressed: () => Navigator.pop(context, false),
                      )),
                if (title != null)
                  Stack(
                    children: <Widget>[
                      Padding(
                          padding: EdgeInsets.fromLTRB(
                              0,
                              child != null ? addClose ? 0 : 16.0 : 32.0,
                              0,
                              isLine ? 15.0 : 20),
                          child: Column(
                            children: <Widget>[
                              isLine
                                  ? Padding(
                                      child: Text(
                                        title,
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline3,
                                        strutStyle: fixedLine,
                                      ),
                                      padding: EdgeInsets.only(
                                          top: 5.0, bottom: 8.0),
                                    )
                                  : Text(
                                      title,
                                      style:
                                          Theme.of(context).textTheme.headline3,
                                      strutStyle: fixedLine,
                                    ),
                              if (isLine)
                                Divider(
                                  endIndent: 20.0,
                                  indent: 20.0,
                                )
                            ],
                          ))
                    ],
                  ),
//
                if (child != null)
                  ConstrainedBox(
                      constraints: BoxConstraints(
                          maxHeight: MediaQuery.of(context).size.height - 100),
                      child: SingleChildScrollView(
                        child: child,
                      )),
                Padding(
                  padding: const EdgeInsets.fromLTRB(16.0, 20.0, 16.0, 16.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Expanded(
                        child: CancelButton(
                            height: 35,
                            callback: () {
                              Navigator.of(context).pop(false);
                            },
                            content: textCancel),
                      ),
                      SizedBox(
                        width: 16,
                      ),
                      Expanded(
                          child: PrimaryButton(
                              height: 35, callback: ok, content: textOk))
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
