Flutter学习笔记

Flutter-五角星评分组件

2020-04-19  本文已影响0人  WinJayQ

评分展示: 我们需要根据不同的评分显示不同的星级展示,这里我封装了一个XFStarRating的小Widget来实现;

1.XFStarRating效果展示

目的:实现功能展示的同时,提供高度的定制效果

暂时实现上面的定制,后续有新的需求继续添加新的功能点~


image.png

2. 实现思路分析

理清楚思路后,你会发现并不是非常复杂,主要就是两点的展示:

问题一:选择StatelessWidget还是StatefulWidget?
考虑到后面可能会做用户点击进行评分或者用户手指滑动评分的效果,所以这里选择StatefulWidget

问题二:如何让选中的star和未选中的star重叠显示?

child: Stack(
        children: <Widget>[
          Row(children: getUnselectImage(), mainAxisSize: MainAxisSize.min,),
          Row(children: getSelectImage(), mainAxisSize: MainAxisSize.min,),
        ],
      ),

问题三:如何实现对选中的最后一个star进行裁剪?

定义CustomClipper裁剪规则:

class MyRectClipper extends CustomClipper<Rect> {
  final double width;

  MyRectClipper({this.width});

  @override
  Rect getClip(Size size) {
    return Rect.fromLTRB(0, 0, width, size.height);
  }

  @override
  bool shouldReclip(MyRectClipper oldClipper) {
    return width != oldClipper.width;
  }
}

使用MyRectClipper进行裁剪:

ClipRect(
      clipper: MyRectClipper(width: leftRatio*widget.size),
      child: widget.selectedImage,
    );

3.XFStarRating代码实现

import 'package:flutter/material.dart';

class XFStarRating extends StatefulWidget {

  final double rating;
  final double maxRating;
  final Widget unselectedImage;
  final Widget selectedImage;
  final int    count;
  final double size;
  final Color  unselectedColor;
  final Color  selectedColor;

 XFStarRating({
   @required this.rating,
   this.maxRating = 10,
   this.size = 20,
   this.unselectedColor = const Color(0xffbbbbbb),
   this.selectedColor = const Color(0xffe0aa46),
   Widget unselectedImage,
   Widget selectedImage,
   this.count = 5,
}):unselectedImage = unselectedImage ?? Icon(Icons.star,size: size,color: unselectedColor,),
  selectedImage = selectedImage ?? Icon(Icons.star,size: size,color: selectedColor,);

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

class _XFStarRatingState extends State<XFStarRating> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        children: <Widget>[
          Row(children: getUnselectImage(), mainAxisSize: MainAxisSize.min,),
          Row(children: getSelectImage(), mainAxisSize: MainAxisSize.min,),
        ],
      ),
    );
  }


  List<Widget> getUnselectImage() {
    return List.generate(widget.count, (index) {
      return widget.unselectedImage;
    });
  }

  List<Widget> getSelectImage() {
    //1.计算Star个数和剩余比例等
    double oneVale = widget.maxRating / widget.count;
    int entireCount = (widget.rating / oneVale).floor();
    double leftValue = widget.rating - entireCount * oneVale;
    double leftRatio = leftValue / oneVale;

    //2.获取Star
    List<Widget> selectedImages = [];
    for (int i = 0; i < entireCount; i++) {
      selectedImages.add(widget.selectedImage);
    }

    //3.计算
    Widget leftStar = ClipRect(
      clipper: MyRectClipper(width: leftRatio*widget.size),
      child: widget.selectedImage,
    );
    selectedImages.add(leftStar);
    return selectedImages;
  }

}

class MyRectClipper extends CustomClipper<Rect> {
  final double width;

  MyRectClipper({this.width});

  @override
  Rect getClip(Size size) {
    return Rect.fromLTRB(0, 0, width, size.height);
  }

  @override
  bool shouldReclip(MyRectClipper oldClipper) {
    return width != oldClipper.width;
  }
}

使用代码:

 XFStarRating(rating: 3.6,maxRating: 5,size: 40,selectedColor: Colors.red,unselectedColor: Colors.grey,),

学习内容来自Flutter从入门到实战

上一篇下一篇

猜你喜欢

热点阅读