Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not change state #30

Open
Sameerkash opened this issue May 24, 2020 · 7 comments
Open

Does not change state #30

Sameerkash opened this issue May 24, 2020 · 7 comments

Comments

@Sameerkash
Copy link

I'm using this package to show the stars of a particular review in my app, I noticed that after I update the state with ChnageNotifer and update the values, the stars don't change state. I have to hot restart my app for new changes to be reflected.

@thangmam
Copy link
Owner

can you show me snippets of your code ?

@Sameerkash
Copy link
Author

can you show me snippets of your code?

I had to refactor my code to meet deadlines, I will surely reproduce it and show it to you soon.

@marhyno
Copy link

marhyno commented Jul 11, 2020

I can confirm this problem, I have readOnly page where user cannot change stars, then I have edit page, when I edit it, come back and use refreshIndicator, the new value is applied but stars are not filled. Only if I scroll all the way to the bottom, stars are redrawn.

@vientn1998
Copy link

I have a same error sir. After i loaded data from server, i was setState(() {
isReadOnly = false;
star = 3.5;
});
but SmoothStarRating not update star = 3.5, it still 0 star.
Plz help me found issue, i tried it many times but not success.
Looking forward to hearing from you soon.

@CameliaTripon
Copy link

Issue mentioned by @marhyno occurs for us too on the version 1.1.1. The scenario is that after updating starCount and rating to a higher value then when initialised, the rating value remains the same and the stars are not filled (just outlined). It could be the fact the currentRating from _SmoothStarRatingState is initialised only in initState() and does not change value when calling setState().

Any timeline when this could be fixed?

@2math
Copy link

2math commented Nov 27, 2020

I don't have time to push a PR, but here is a stateless widget that you can use to show just stars and will update on each rebuild

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

class StaticSmoothStarRating extends StatelessWidget {
  final int starCount;
  final double rating;
  final Color color;
  final Color borderColor;
  final double size;
  final bool allowHalfRating;
  final IconData filledIconData;
  final IconData halfFilledIconData;
  final IconData defaultIconData; //this is needed only when having fullRatedIconData && halfRatedIconData
  final double spacing;
  final double halfStarThreshold = 0.53; //half star value starts from this number

  const StaticSmoothStarRating({
    Key key,
    this.starCount = 5,
    this.spacing = 0.0,
    this.rating = 0.0,
    this.defaultIconData = Icons.star_border,
    this.color,
    this.borderColor,
    this.size = 25,
    this.filledIconData = Icons.star,
    this.halfFilledIconData = Icons.star_half,
    this.allowHalfRating = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Wrap(
          alignment: WrapAlignment.start,
          spacing: spacing,
          children: List.generate(starCount, (index) => buildStar(context, index))),
    );
  }

  Widget buildStar(BuildContext context, int index) {
    double currentRating = normalizeRating(rating);
    Icon icon;
    if (index >= currentRating) {
      icon = Icon(
        defaultIconData,
        color: borderColor ?? Theme.of(context).primaryColor,
        size: size,
      );
    } else if (index > currentRating - (allowHalfRating ? halfStarThreshold : 1.0) && index < currentRating) {
      icon = Icon(
        halfFilledIconData,
        color: color ?? Theme.of(context).primaryColor,
        size: size,
      );
    } else {
      icon = Icon(
        filledIconData,
        color: color ?? Theme.of(context).primaryColor,
        size: size,
      );
    }
    return icon;
  }

  double normalizeRating(double newRating) {
    var k = newRating - newRating.floor();
    if (k != 0) {
      //half stars
      if (k >= halfStarThreshold) {
        newRating = newRating.floor() + 1.0;
      } else {
        newRating = newRating.floor() + 0.5;
      }
    }
    return newRating;
  }
}

@takax2021
Copy link

worked! thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants