Skip to content

Rate 评分 ​

用星级或自定义字符收集和展示评分。

何时使用 ​

  • 商品评价、满意度反馈和质量评级。
  • 只读评分可禁用交互;数值精度要求高时使用输入框或滑块。

在线体验 ​

Rate 评分 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import "github.com/tangoui/tango/ui"

var score float64 = 3

func page(c *ui.Context) {
    ui.Rate(c, &score).A11yLabel("服务评分")
}

常见用法 ​

半星与清除 ​

go
func half(c *ui.Context, value *float64) {
    ui.Rate(c, value).AllowHalf().AllowClear(true)
}

文案和分数 ​

go
func labels(c *ui.Context, value *float64) {
    ui.Rate(c, value).Texts("很差", "较差", "一般", "满意", "惊喜").ShowScore()
}

自定义字符和只读 ​

go
func readOnly(c *ui.Context, value *float64) {
    ui.Rate(c, value).Count(10).Character("♥").Disabled(true)
}

主题与样式 ​

评分令牌控制星形尺寸、间距、选中色与未选中色;Size 和 Color 可对单个组件覆盖。

注意事项 ​

Changed 在用户修改本帧返回 true,应用应在这里保存分值。半星模式下值以 0.5 为步长;提供可读标签,确保屏幕阅读器能报告当前评分。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Rate 评分

gallery 演示 Rate 的 Render 函数,源码 page_entry_rate.go#L18。

加载中…

Rate 尺寸、分段颜色与分数格式

gallery 演示 RateAlign 的 Render 函数,源码 page_b1a.go#L149。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Rate ​

go
func Rate(c *Context, val *float64) *RateW

Rate adds a rating bound to *val (0..count, steps of 1 or 0.5).

返回 *RateW,可链式调用的方法见 RateW 方法。

RateW 方法 ​

ui.RateW:RateW is a star rating (Ant Rate / Element el-rate).

方法说明签名
A11yLabelA11yLabel sets the accessible name (default: the Locale's "rating"; inside a FormItem the item label is used).A11yLabel(s string) *RateW
AllowClearAllowClear lets a click on the current value reset it to 0 (default on).AllowClear(on bool) *RateW
AllowHalfAllowHalf enables half-star values.AllowHalf() *RateW
ChangedChanged reports a new value this frame.Changed() bool
CharacterCharacter replaces the star with a text character (e.g. "A", "好").Character(s string) *RateW
CharacterFuncCharacterFunc draws the character fn returns for star i (0-based; Ant character as a function of the index).CharacterFunc(fn func(i int) string) *RateW
ColorColor overrides the active color.Color(col theme.Color) *RateW
ColorsColors sets the active color by the value (Element colors): a value at or below the low threshold uses the first color, below the high threshold the second, from it on the third.Colors(cols ...theme.Color) *RateW
CountCount sets the number of stars (default 5).Count(n int) *RateW
DisabledDisabled makes the rating read-only.Disabled(on bool) *RateW
HoveredHovered returns the value previewed under the pointer (0 = none).Hovered() float64
IconsIcons replaces the star icons: on for the filled part, void for the empty one (Element icons / void-icon; nil keeps the star).Icons(on, void *icon.Icon) *RateW
KeyboardKeyboard enables or disables the arrow / Home / End keys (Ant keyboard, default on).Keyboard(on bool) *RateW
LargeLarge uses the large star size (Ant size="large": 25px; Element keeps 18px in a 40px row).Large() *RateW
ScoreFormatScoreFormat formats the score of ShowScore (Element score-template; by default the bare number).ScoreFormat(fn func(v float64) string) *RateW
ShowScoreShowScore shows the numeric value after the stars.ShowScore() *RateW
SizeSize sets the star size in px (Ant 20, Element 18).Size(px float32) *RateW
SmallSmall uses the small star size (Ant size="small": 15px; Element 14px).Small() *RateW
TextsTexts shows a description after the stars, one per star (Element show-text).Texts(t ...string) *RateW
ThresholdsThresholds sets the values that switch Colors (Element low-threshold / high-threshold; default 2 and 4).Thresholds(low, high float64) *RateW
TooltipsTooltips shows a tooltip over the star under the pointer, one text per star (Ant tooltips); "" entries show none.Tooltips(tips ...string) *RateW
VoidColorVoidColor sets the color of the empty stars (Element void-color / disabled-void-color).VoidColor(col theme.Color) *RateW

相关链接 ​

以 MIT 许可发布