flutterdartindexinglogicrating

Displaying rating with given value from model in list and painting colors dynamically


HELP!!! I need help implementing a logic that will paint the rating stars gold out of five stars, according to the model data I have. No user interactions , just Display ratings. I want to output the rating in my list of widgets.

My model class :-

class Trending {
  const Trending({
    required this.image,
    required this.title,
    required this.genre,
    required this.subtitle,
    required this.rating,
  });

  final String image;
  final String title;
  final Genre genre;
  final String subtitle;
  final int rating;
}

My DummyData:-

const trendingHome = <Trending>[
  Trending(
      image: 'assets/images/psimg.jpg',
      title: 'The Havoc Bringer',
      rating: 4,
      genre: Genre.action,
      subtitle:
          'In a small town where the youths have been manipulated by their rulers a courageous Man stands up for his people...'),
  Trending(
      image: 'assets/images/img0.jpg',
      title: 'Chainsaw Wreck',
      rating: 5,
      genre: Genre.rpg,
      subtitle: ''),
];

Solution

  • I used List.generate to build the stars, then used a Widget builder with int parameter as index to build the rating icons and conditionally render the colors dynamically.