importing rating bar in flutter

580 views Asked by At

In my page, I want there to be a review section at the bottom where a user can submit a review. They would put their name, email, comment, and rate the item out of 5 stars (their email will not be displayed on the page). Right next to the reviews title, I want it to display the average number of stars by taking the average rated stars from the customer reviews.

I am not sure how to implement the feature for them to write their reviews, have it show up, and then take the average star ratings. What i am currently trying requires me to import the rating bar but I get this error.

 Target of URI doesn't exist: 'package:flutter_rating_bar/flutter_rating_bar.dart'

This is the code I currently have for the details page (for each of the item, it has its title (which classifies it into a group like drinks), its name, and its description):

class DetailsPage extends StatefulWidget {
  final String imageUrl;
  final String title;
  final String name;
  final String description;

  DetailsPage({
    required this.imageUrl,
    required this.title,
    required this.name,
    required this.description,
  });

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

class _DetailsPageState extends State<DetailsPage> {
  double rating = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: SafeArea(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Flexible(
                fit: FlexFit.loose,
                child: Container(
                  height: 300.0, // set a fixed height
                  margin: EdgeInsets.all(16.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(16.0),
                    image: DecorationImage(
                      image: AssetImage(widget.imageUrl),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      widget.name,
                      style: TextStyle(
                        fontSize: 24.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 8.0),
                    Text(
                      widget.description,
                      style: TextStyle(fontSize: 16.0),
                    ),
                    SizedBox(height: 16.0),
                    Text(
                      'Rate this item',
                      style: TextStyle(
                        fontSize: 18.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 8.0),
                    RatingBar.builder(
                      initialRating: rating,
                      minRating: 1,
                      direction: Axis.horizontal,
                      allowHalfRating: true,
                      itemCount: 5,
                      itemSize: 40.0,
                      itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
                      itemBuilder: (context, _) => Icon(
                        Icons.star,
                        color: Colors.amber,
                      ),
                      onRatingUpdate: (ratingValue) {
                        setState(() {
                          rating = ratingValue;
                        });
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
2

There are 2 answers

0
Faisal Faraj On

To fix the error Target of URI doesn't exist: This happens when flutter can't find the package path which may you forgot to download using the command flutter pub get, and also follow this steps:

  1. flutter clean
  2. flutter pub get
  3. flutter pub upgrade ( Optional - use if you want to upgrade packages)
  4. Restart Android Studio or Visual Studio(IDE or Text Editor you use)
0
Ydev On

run flutter pub add flutter_rating_bar in your terminal then restart your app and you're done.