import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/shared/shared_styles.dart';

/// A button that shows a busy indicator in place of title
class BusyButton extends StatefulWidget {

  const BusyButton({
    required this.title,
    required this.onPressed,
    this.busy = false,
    this.enabled = true,
    this.isCancel = false,
    this.isDanger = false,
  });
  final bool busy;
  final String title;
  final Function onPressed;
  final bool enabled;
  final bool isCancel;
  final bool isDanger;

  @override
  _BusyButtonState createState() => _BusyButtonState();
}

class _BusyButtonState extends State<BusyButton> {
  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      duration: const Duration(milliseconds: 300),
      decoration: BoxDecoration(
          gradient: widget.isCancel || widget.isDanger ? null : primaryGradient,
          color: widget.isCancel || widget.isDanger ? whiteColor : null,
          borderRadius: BorderRadius.circular(5),
          boxShadow: [buttonShadowBox]),
      child: Material(
        type: MaterialType.transparency,
        child: InkWell(
          onTap: () {
            if (!(widget.busy || !widget.enabled)) widget.onPressed();
          },
          child: AnimatedContainer(
            height: 40.h,
            duration: const Duration(milliseconds: 300),
            alignment: Alignment.center,
            child: !widget.busy
                ? Text(
                    widget.title,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontWeight: FontWeight.w400,
                        color: widget.isDanger ? dangerColor : blackColor,
                        fontSize: 12),
                    //minFontSize: 2,
                    maxLines: 1,
                  )
                : SizedBox(
                    width: 20.h,
                    height: 20.h,
                    child: const CircularProgressIndicator(
                        strokeWidth: 2,
                        valueColor:
                            AlwaysStoppedAnimation<Color>(Colors.white)),
                  ),
          ),
        ),
      ),
    );
  }
}
