
import 'package:flutter/material.dart';

import 'package:twong/utils/index.dart';

class RankStars extends StatefulWidget {
  final int max;
  final Color color;
  final Widget header;
  final int value;
  final Function(int) onChange;
  RankStars(this.max, {this.color = Colors.black, this.onChange,
    this.header, this.value = -1});

  @override
  State<StatefulWidget> createState() {
    return _RankStarsState();
  }
}

class _RankStarsState extends State<RankStars> {
  int current = 0;

  @override
  void initState() {
    current = widget.value;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> widgets = List<Widget>();
    if(widget.header != null) {
      widgets.add(widget.header);
    }

    for(var i = 0; i < widget.max; i ++) {
      widgets.add(InkWell(
        onTap: () => _onTap(i),
        child: Container(
          margin: EdgeInsets.only(left: 6.px),
          child: i > current ? Icon(Icons.star_border, color: widget.color)
            : Icon(Icons.star, color: widget.color),
        ),
      ));
    }
    return Row(children: widgets);
  }

  void _onTap(int idx) {
    setState(() {
      current = idx;
    });
    widget.onChange?.call(current);
  }
}