import 'package:flutter/material.dart';

class ToggleButton extends StatefulWidget {
  final IconData activeIcon;
  final IconData unActiveIcon;
  final Color activeColor;
  final Color unActiveColor;
  final bool active;
  final double size;

  final void Function(bool status) onChange;

  ToggleButton(
      {this.activeIcon,
      this.unActiveIcon,
      this.activeColor,
      this.unActiveColor,
      this.onChange,
      this.size = 25,
      this.active = false});

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

class _ToggleButtonState extends State<ToggleButton> {
  bool isActive;
  @override
  void initState() {
    super.initState();
    isActive = widget.active;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        child: Stack(
          children: <Widget>[
            //左边选择，右边未选择
            Icon(widget.unActiveIcon,
                size: widget.size,
                color: isActive ? widget.unActiveColor : widget.activeColor),

            Icon(widget.activeIcon,
                size: widget.size,
                color: isActive ? widget.activeColor : widget.unActiveColor),
          ],
        ),
      ),
      onTap: () {
        setState(() {
          isActive = !isActive;
          widget.onChange(isActive);
        });
      },
    );
  }
}
