Rate 评分
用星级或自定义字符收集和展示评分。
何时使用
- 商品评价、满意度反馈和质量评级。
- 只读评分可禁用交互;数值精度要求高时使用输入框或滑块。
在线体验
Rate 评分 · 在线体验真实 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 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Rate
go
func Rate(c *Context, val *float64) *RateWRate 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).
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (default: the Locale's "rating"; inside a FormItem the item label is used). | A11yLabel(s string) *RateW |
AllowClear | AllowClear lets a click on the current value reset it to 0 (default on). | AllowClear(on bool) *RateW |
AllowHalf | AllowHalf enables half-star values. | AllowHalf() *RateW |
Changed | Changed reports a new value this frame. | Changed() bool |
Character | Character replaces the star with a text character (e.g. "A", "好"). | Character(s string) *RateW |
CharacterFunc | CharacterFunc 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 |
Color | Color overrides the active color. | Color(col theme.Color) *RateW |
Colors | Colors 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 |
Count | Count sets the number of stars (default 5). | Count(n int) *RateW |
Disabled | Disabled makes the rating read-only. | Disabled(on bool) *RateW |
Hovered | Hovered returns the value previewed under the pointer (0 = none). | Hovered() float64 |
Icons | Icons 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 |
Keyboard | Keyboard enables or disables the arrow / Home / End keys (Ant keyboard, default on). | Keyboard(on bool) *RateW |
Large | Large uses the large star size (Ant size="large": 25px; Element keeps 18px in a 40px row). | Large() *RateW |
ScoreFormat | ScoreFormat formats the score of ShowScore (Element score-template; by default the bare number). | ScoreFormat(fn func(v float64) string) *RateW |
ShowScore | ShowScore shows the numeric value after the stars. | ShowScore() *RateW |
Size | Size sets the star size in px (Ant 20, Element 18). | Size(px float32) *RateW |
Small | Small uses the small star size (Ant size="small": 15px; Element 14px). | Small() *RateW |
Texts | Texts shows a description after the stars, one per star (Element show-text). | Texts(t ...string) *RateW |
Thresholds | Thresholds sets the values that switch Colors (Element low-threshold / high-threshold; default 2 and 4). | Thresholds(low, high float64) *RateW |
Tooltips | Tooltips shows a tooltip over the star under the pointer, one text per star (Ant tooltips); "" entries show none. | Tooltips(tips ...string) *RateW |
VoidColor | VoidColor sets the color of the empty stars (Element void-color / disabled-void-color). | VoidColor(col theme.Color) *RateW |