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

// 普通气泡弹框
// 使用：showToast("xxxxxxx");
void showToast(
  String text, {
  gravity: ToastGravity.CENTER,
  toastLength: Toast.LENGTH_SHORT,
}) {
  Fluttertoast.showToast(
    msg: text,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIosWeb: 3,
    backgroundColor: Colors.grey[600], // 灰色背景
    fontSize: 16.0,
  );
}

// 一个加载中的动画，不传text时，默认显示文字Loading...
// 使用：showLoading(context, 'xxxxxxx');
void showLoading(context, text) {
  text = text ?? "Loading...";
  showDialog(
    barrierDismissible: false,
    context: context,
    builder: (context) {
      return Center(
        child: Container(
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(3.0),
              boxShadow: [
                // 阴影
                BoxShadow(
                  color: Colors.black12,
                  //offset: Offset(2.0,2.0),
                  blurRadius: 10.0,
                )
              ]),
          padding: EdgeInsets.all(16),
          margin: EdgeInsets.all(16),
          constraints: BoxConstraints(minHeight: 120, minWidth: 180),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                height: 30,
                width: 30,
                child: CircularProgressIndicator(
                  strokeWidth: 3,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 20.0),
                child: Text(
                  text,
                  style: Theme.of(context).textTheme.bodyMedium,
                ),
              ),
            ],
          ),
        ),
      );
    },
  );
}

// 带有确定取消按钮的提示框，content是提示框的内容文字，confirmCallback是点击确定按钮的回调
// 使用：showConfirmDialog(context, '确认xxxxxxx吗？', () {print('xxxxxxx');});
void showConfirmDialog(
    BuildContext context, String content, Function confirmCallback) {
  showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text("提示"),
          content: Text(content),
          actions: <Widget>[
            FlatButton(
              onPressed: () {
                confirmCallback();
                Navigator.of(context).pop();
              },
              child: Text("确认"),
            ),
            FlatButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text("取消"),
            ),
          ],
        );
      });
}
