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

class GoodsShareItem extends StatefulWidget {
  final Widget child;
  final String imgHeader;
  final String imgCheck;
  final String imgCheckSelected;
  final bool isSelected;
  final bool enabled;
  final ValueChanged<bool> onSelected;

  const GoodsShareItem({
    Key key,
    this.imgHeader = '',
    this.imgCheck = '',
    this.imgCheckSelected = '',
    this.isSelected = false,
    this.enabled = true,
    this.onSelected,
    this.child,
  }) : super(key: key);

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

class _GoodsShareItemState extends State<GoodsShareItem> {
  bool _isSelected;

  @override
  void initState() {
    _isSelected = widget.isSelected;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        setState(() {
          _isSelected = !_isSelected;
        });
        if (widget.onSelected != null) {
          widget.onSelected(_isSelected);
        }
      },
      child: Container(
        margin: EdgeInsets.only(left: 15, right: 15, top: 5, bottom: 5),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              width: 36,
              height: 36,
              child: ClipRRect(
                child: CachedNetworkImage(
                  imageUrl: widget.imgHeader,
                ),
                borderRadius: BorderRadius.circular(18),
              ),
            ),
            Expanded(
              child: widget.child ?? Container(),
            ),
            Container(
              width: 18,
              height: 18,
              child: Visibility(
                visible: widget.enabled,
                child: CachedNetworkImage(
                  imageUrl:
                      _isSelected ? widget.imgCheckSelected : widget.imgCheck,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
