import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/public/custom_button/custom_button_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class CustomButton extends StatelessWidget {
  final CustomButtonModel style;
  final VoidCallback onClick;

  const CustomButton(this.style, {Key key, this.onClick}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var decoration = BoxDecoration(
        color: HexColor.fromHex(style.bgColor ?? ''),
        borderRadius: BorderRadius.circular(50));
    if (style.bgType == 'image') {
      decoration = BoxDecoration(
          image: DecorationImage(
              image: CachedNetworkImageProvider(style.bgImage ?? ''),
              fit: BoxFit.cover),
          borderRadius: BorderRadius.circular(50));
    }
    return GestureDetector(
      child: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: decoration,
        child: Center(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              style.smallIcon == null || style.smallIcon == ''
                  ? Container()
                  : Container(
                      width: 14,
                      height: 14,
                      margin: EdgeInsets.only(right: 4),
                      child: CachedNetworkImage(
                        imageUrl: style.smallIcon,
                        fit: BoxFit.cover,
                      ),
                    ),
              Text(
                style.name,
                style: TextStyle(
                  color: HexColor.fromHex(style.textColor ?? ''),
                  fontSize: 14,
                ),
              )
            ],
          ),
        ),
      ),
      onTap: onClick,
    );
  }
}
